{"id":"for-loops-range","slug":"range","title":"Counting with range","text":"Often you want to repeat something a given number of times, or go through numbers in order. The function **`range`** gives the numbers, and `for` goes through them. With one argument, `range(n)` gives the numbers from 0 to `n - 1`:\n\n```python\nfor i in range(4):\n    print(i)\n```\n\n```output\n0\n1\n2\n3\n```\n\nThat's 4 numbers, starting from 0: `range(4)` stops just before 4. So `for i in range(4):` repeats its block 4 times, which is often all you need.\n\n### Start, end and step\n\nWith two arguments, `range(start, end)` starts from `start` instead of 0. The end is still left out:\n\n```python\nfor number in range(1, 6):\n    print(number, \"squared is\", number * number)\n```\n\n```output\n1 squared is 1\n2 squared is 4\n3 squared is 9\n4 squared is 16\n5 squared is 25\n```\n\nTo go up to 5, the end must be 6. Forgetting that is the most common `range` mistake: the loop stops one number too early.\n\nA third argument is the **step**, how much the number changes each time:\n\n```python\nfor year in range(2000, 2025, 8):\n    print(year)\n```\n\n```output\n2000\n2008\n2016\n2024\n```\n\nThe step can also be negative, which counts down. Then the start is the greater number, and the end (left out again) is the smaller one:\n\n```python\nfor i in range(5, 0, -1):\n    print(i)\nprint(\"Liftoff!\")\n```\n\n```output\n5\n4\n3\n2\n1\nLiftoff!\n```\n\nCompare this with the `while` loop that would do the same: a counter, a condition and a `-= 1`. With `range`, all three are in one line.\n\nThe variable of the loop can be used like any other variable. Here a function uses it to build a line of numbers:\n\n```python\ndef number_line(n: int) -> str:\n    line = \"\"\n    for i in range(1, n + 1):\n        line += str(i) + \" \"\n    return line\n\n\nprint(number_line(5))\n```\n\n```output\n1 2 3 4 5\n```\n","task":"Fill in the blanks so the loop prints the even numbers from 2 to 10:\n\n```\n2\n4\n6\n8\n10\n```\n","kind":"fill","seed":"for number in range([[2]], [[11‖12]], [[2]]):\n    print(number)\n","solution":"for number in range(2, 11, 2):\n    print(number)\n","hints":["Start from 2 and take steps of 2. The end is left out, so it must be greater than 10.","`range(2, 11, 2)` gives 2, 4, 6, 8 and 10."],"tests":[{"name":"Output","stdin":"","expect":"2\n4\n6\n8\n10","hidden":false}],"require":[],"forbid":[],"more":[{"task":"Write a program that asks for a whole number and counts down from it to 1, one number per line, and then prints `Liftoff!`. Use `for` and `range`:\n\n```\nCount down from: 3\n3\n2\n1\nLiftoff!\n```\n","kind":"code","seed":"# Count down with for and range.\n","solution":"# Count down with for and range.\nstart = int(input(\"Count down from: \"))\nfor i in range(start, 0, -1):\n    print(i)\nprint(\"Liftoff!\")\n","hints":["Counting down needs a negative step: `range(start, ..., -1)`.","The end is left out, so to stop at 1 the end must be 0.","`for i in range(start, 0, -1):` prints `i`, and `Liftoff!` comes after the loop."],"tests":[{"name":"Test 1","stdin":"3\n","expect":"Count down from: 3\n3\n2\n1\nLiftoff!","hidden":false},{"name":"Test 2","stdin":"1\n","expect":"Count down from: 1\n1\nLiftoff!","hidden":true},{"name":"Test 3","stdin":"7\n","expect":"Count down from: 7\n7\n6\n5\n4\n3\n2\n1\nLiftoff!","hidden":true}],"require":[{"pattern":"\\brange\\s*\\(","message":"Count down with `for` and `range`."}],"forbid":[]},{"task":"Write the function `sum_of_squares(n: int) -> int`, which returns the sum 1 · 1 + 2 · 2 + ... + n · n. For example, `sum_of_squares(3)` returns `14`, because 1 + 4 + 9 is 14.\n","kind":"code","seed":"def sum_of_squares(n: int) -> int:\n    pass\n\n\nif __name__ == \"__main__\":\n    print(sum_of_squares(3))\n","solution":"def sum_of_squares(n: int) -> int:\n    total = 0\n    for i in range(1, n + 1):\n        total += i * i\n    return total\n\n\nif __name__ == \"__main__\":\n    print(sum_of_squares(3))\n","hints":["The numbers go from 1 to `n`, so the range is `range(1, n + 1)`.","Keep a helper variable `total` and add `i * i` to it in the loop.","Return `total` after the loop."],"tests":[{"name":"sum_of_squares(3)","stdin":"","call":"sum_of_squares(3)\n","expect":"14","hidden":false,"returns":true},{"name":"sum_of_squares(1)","stdin":"","call":"sum_of_squares(1)\n","expect":"1","hidden":true,"returns":true},{"name":"sum_of_squares(10)","stdin":"","call":"sum_of_squares(10)\n","expect":"385","hidden":true,"returns":true},{"name":"sum_of_squares(0)","stdin":"","call":"sum_of_squares(0)\n","expect":"0","hidden":true,"returns":true}],"require":[],"forbid":[]}]}