{"id":"for-loops-lists","slug":"for-lists","title":"Going through a list with for","text":"Lists are made to be gone through: print every price, add up every score, check every name. Python has a loop made for exactly this job, the **`for`** loop:\n\n```python\nprices = [12, 5, 30]\nfor price in prices:\n    print(price)\n```\n\n```output\n12\n5\n30\n```\n\nRead it as \"for each price in prices\". In the first round, the variable after `for` (here `price`) is the first item, 12, and the indented block runs with it. In the next round `price` is 5, and in the last one 30. After the last item, the loop is over and the program goes on with the line after it.\n\nThe variable can have any name. Choose one that says what an item is: `for price in prices`, `for name in names`.\n\nWith only `while`, the same job takes a counter of your own:\n\n```python\nindex = 0\nwhile index < len(prices):\n    print(prices[index])\n    index += 1\n```\n\nThat's four lines instead of two, and three chances to slip: start the counter at the wrong number, write `<=` where `<` belongs, or forget `index += 1` and loop forever. A `for` loop has no counter, so none of those mistakes can happen.\n\n### Definite and indefinite loops\n\nA `while` loop repeats as long as its condition is true, and you can't always tell in advance how many rounds that will be: it might wait for the user to type `quit`. That's called **indefinite iteration**. A `for` loop goes through a collection that's already there, so the number of rounds is known when it starts: one per item. That's **definite iteration**.\n\n### Working with each item\n\nMost loops do more than print. A helper variable before the loop collects a result while the loop goes through the items:\n\n```python\nprices = [12, 5, 30, 8]\ntotal = 0\ncheap = 0\nfor price in prices:\n    total += price\n    if price < 10:\n        cheap += 1\nprint(\"Total:\", total)\nprint(\"Under 10:\", cheap)\n```\n\n```output\nTotal: 55\nUnder 10: 2\n```\n\n`sum(prices)` would give the total too, but the loop can do anything with each item: here it also counts the cheap ones, which no ready function does.\n\nInside a function, a `return` in the loop ends the whole function at once. Put the `return` after the loop (not indented under it) when the function needs to see every item first.\n","task":"Fill in the blanks so the loop prints every guest's name with a greeting:\n\n```\nWelcome, Ada!\nWelcome, Linus!\nWelcome, Grace!\n```\n","kind":"fill","seed":"guests = [\"Ada\", \"Linus\", \"Grace\"]\n[[for]] guest [[in]] guests:\n    print(f\"Welcome, {guest}!\")\n","solution":"guests = [\"Ada\", \"Linus\", \"Grace\"]\nfor guest in guests:\n    print(f\"Welcome, {guest}!\")\n","hints":["The loop line reads like \"for each guest in guests\".","The two missing words are the keywords `for` and `in`."],"tests":[{"name":"Output","stdin":"","expect":"Welcome, Ada!\nWelcome, Linus!\nWelcome, Grace!","hidden":false}],"require":[],"forbid":[],"more":[{"task":"Write the function `count_over(numbers: list, limit: int) -> int`, which returns how many numbers in the list are greater than `limit`. For example, `count_over([3, 12, 7, 20], 5)` returns `3`.\n","kind":"code","seed":"def count_over(numbers: list, limit: int) -> int:\n    pass\n\n\nif __name__ == \"__main__\":\n    print(count_over([3, 12, 7, 20], 5))\n","solution":"def count_over(numbers: list, limit: int) -> int:\n    count = 0\n    for number in numbers:\n        if number > limit:\n            count += 1\n    return count\n\n\nif __name__ == \"__main__\":\n    print(count_over([3, 12, 7, 20], 5))\n","hints":["Start a helper variable `count` at 0 before the loop.","Go through the list with `for number in numbers:` and add 1 to `count` when `number > limit`.","Return `count` after the loop, at the same indentation as the `for` line."],"tests":[{"name":"count_over([3, 12, 7, 20], 5)","stdin":"","call":"count_over([3, 12, 7, 20], 5)\n","expect":"3","hidden":false,"returns":true},{"name":"count_over([1, 2, 3], 10)","stdin":"","call":"count_over([1, 2, 3], 10)\n","expect":"0","hidden":true,"returns":true},{"name":"count_over([5, 6, 5, 6], 5)","stdin":"","call":"count_over([5, 6, 5, 6], 5)\n","expect":"2","hidden":true,"returns":true},{"name":"count_over([], 0)","stdin":"","call":"count_over([], 0)\n","expect":"0","hidden":true,"returns":true}],"require":[{"pattern":"\\bfor\\b","message":"Go through the numbers with a `for` loop."}],"forbid":[]},{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"numbers = [4, -2, 7, -5]\ntotal = 0\nfor number in numbers:\n    if number > 0:\n        total += number\n    print(number, total)\nprint(\"Done:\", total)\n","solution":"numbers = [4, -2, 7, -5]\ntotal = 0\nfor number in numbers:\n    if number > 0:\n        total += number\n    print(number, total)\nprint(\"Done:\", total)\n","hints":["The loop takes the numbers one at a time, in order, and prints a line for each of them.","Only positive numbers are added to `total`, but the `print` inside the loop runs for every number.","The last line is after the loop, so it's printed once, at the end."],"tests":[{"name":"Output","stdin":"","expect":"4 4\n-2 4\n7 11\n-5 11\nDone: 11","hidden":false}],"require":[],"forbid":[],"lines":["4 4","-2 4","7 11","-5 11","Done: 11"]}]}