{"assignment":{"_schema_version":2,"course_id":37,"date_created":"2022-06-28T19:00:00+00:00","date_modified":"2023-09-10T14:04:57.453293+00:00","extra_instructor_files":"","extra_starting_files":"","forked_id":null,"forked_version":null,"hidden":false,"id":1061,"instructions":"## Doing Things Multiple Times\n\n```python for-loop-example\ncosts = [100, 25, 25, 50, 10]\n\n# Without FOR loops\nprint(costs[0])\nprint(costs[1])\nprint(costs[2])\nprint(costs[3])\nprint(costs[4])\n\n# With FOR loops!\nfor a_cost in costs:\n    print(a_cost)\n```\n\nThe program shown here prints out a list of numbers \u2014 each number on its own line\u2014in two different ways.\nIn the first way, we had to write a print statement 5 times, once for each number.\nIn the second way, however, we only had to write a pair of statements by using a `for` loop.\nThe `for` loop is an amazingly useful tool that lets us perform an action on each element of a list.\n\n## For Loop Syntax\n\n![Annotations point out each part of the loop definition.](bakery_loops_for_syntax.png)\n\nUsing the previous example, this is how we write a FOR loop in more detail:\n\nFirst, we write the word `for`.\nThen we make a new variable named the \"iteration variable\".\nWe'll come back to this later.\nNext, we write the word `in`.\nThen, we put the list variable or value that we want to iterate over.\nAfter that, we need to put a colon (`:`).\nFinally, we put statements inside the body of the loop, indented with 4 spaces.\nThe first line of the `for` loop (the parts before the body) is called the _loop header_.\nThe diagram shown labels all the parts.\n\n## The Iteration List\n\n![An arrow points from `a_list` to `item` in a for loop. An annotation points out that each element in turn is assigned to the iteration variable. An additional annotation shows a table of values that are stored in the list.](bakery_loops_for_iteration_variable.png)\n\nA crucial element of any `for` loop is the iteration list.\nThis is the data that we want to iterate over.\nWhen the loop executes, each item of the list will be assigned to the iteration variable in turn.\nIt doesn't matter if the list has no items, ten items, or a thousand items; Python will still process each one in turn.\n\n## The Iteration Variable\n\n```python\nfor a_temperature in temperatures:\n    pass\n\nfor a_book in books:\n    pass\n\nfor a_name in names:\n    pass\n\nfor a_fruit in fruits:\n    pass\n```\n\nOne of the hardest things to understand about `for` loops is the iteration variable.\nThe iteration variable represents the generalized version of each item in the list.\nIf you have a list of temperatures, it is \"a temperature\".\nIf you have a list of books, it is \"a book\".\nBy performing operations on this generalized version of a list item, you can work on the entire list while only having to think about one element at a time.\nThe iteration variable is sometimes also referred to as the _iteration target_.\n\nHere are two critical facts to remember about the _iteration variable_.\nFirst, the iteration variable is created by the `for` loop when it executes.\nSecond, the type of the iteration variable is the same type as the element type of the list.\nThese are some examples of `for` loops with well-named iteration variables.\n\n\n## The Body\n\n```python body-example\nprices = [10, 20, 15]\n\nfor a_price in prices:\n    adjusted = a_price * .9\n    print(adjusted)\n# Body ends\n```\n\nJust like an `if` statement, we can put statements inside of a `for` loop.\nEach statement is indented with four spaces, and each statement will be executed one by one, from top to bottom.\nThe first unindented line marks the end of the body, just like with functions and `if` statements.\nHowever, unlike those other bodies, these statements will be repeated again and again, once for each element of the list.\n\n## The Flow of a For Loop\n\n![An arrow points from the first line down to the third line of a `for` loop, and then curves back up (forming a loop).](bakery_loops_for_loop_back.png)\n\nPreviously, we said that a program was like a river, running from the top to the bottom.\nIf statements made the stream split into two directions.\nFor loops also make the stream split, but one of the new streams will move back up.\nThis is the crucial idea, and it's why we call it a loop.\nThe program will LOOP until each element of the list has been assigned to the iteration variable.\n\n## The Scope of a For Loop\n\n```python loop-scope\nnumbers = [1, 5, 2]\nmessage = \"The number is\"\nfor number in numbers:\n    print(message, number)\nprint(\"The last number was\", number)\n```\n\nUnlike functions, `for` loops do not have their own scope.\nVariables defined outside of a `for` loop can be used inside the loop body.\nThe iteration variable can be used after the loop has finished.\nNothing special must be done with regards to scope with loops.\n\n\n#### Tracing a For Loop\n\n![Code is shown on the left, and then the final trace of the program is shown on the right.](bakery_loops_for_stack_heap.png)\n\nLet's look at a loop in practice.\nThe following Stack/Heap diagram shows an iteration over a list, adding each number to a running sum which is finally printed in the end.\nThe crossed-out boxes in the stack indicate how the values stored in the `total` and `a_cost` variables change over time.\nThe `total` variable was initially set to `0` before the loop began.\nThen, the fourth line (the loop body) is executed 4 times, once for each element of the list.\nAt the start of each loop iteration, the `a_cost` iteration variable is updated to reflect the value of the next element of the list (starting with `5`, then moving on to `3`, then `2`, and finally `4` on the last iteration).\nInside the loop, the `total` variable is updated by taking the current value of `total` and adding the current value of `a_cost`.\nSo, the value changes from `0` to `5` to `8` to `10` and then finally `14`.\nAfter the loop is over, all three variables are still in scope (since `for` loops do not have scope).\nTherefore, the final value of `total` is `14` and the final value of `a_cost` is `4`.\n\nIf you are having trouble understanding how the code changes over time, you should consider running this same code using BlockPy's View Trace feature.\nThe View Trace will allow you to step through the code one line at a time.\nYou will also be able to see the value of each variable at each step.\nVisually, the current line of code will seem to \"jump upwards\" to revisit the start of the loop, once for each element of the list.\n\n## Summary\n\n- A `for` loop allows you to repeat a body of statements once for each element of a list.\n- A `for` loop has two major parts: the header and the body of statements.\n- The header of the loop has two keywords (`for` and `in`) along with the iteration list and the iteration variable.\n- During each iteration of the loop, each element of the iteration list will be assigned to the iteration target, one at a time.\n- The iteration target is a new variable created by the `for` loop whose type will be based on the element type of the iteration list.\n- The body of the `for` loop has one or more statements which will be executed repeatedly during the loop.\n- After each loop iteration, while there are still more elements to process, the execution will jump back to the start of the loop body and assign the next value of the iteration list to the iteration target.\n- Although a `for` loop has a body, it does not have a separate scope: variables created inside the loop are available after the loop ends.\n- Tracing a `for` loop is tricky, but a stack/heap diagram can show how the values of the variables change over each iteration.\n\n","ip_ranges":"","name":"6A1) For Loops Reading","on_change":"","on_eval":"","on_run":"","owner_id":1,"owner_id__email":"acbart@udel.edu","points":0,"public":true,"reviewed":false,"sample_submissions":[],"settings":"{\n  \"header\": \"For Loops\",\n  \"youtube\": {\n    \"Bart\": \"uj-LbEsEr-g\",\n    \"Amy\": \"U8HzuPmCgp0\"\n  },\n  \"slides\": \"bakery_for_loops.pdf\",\n  \"summary\": \"In this lesson, you will learn about FOR loops. The FOR loop allows you to perform actions on each element of a list, so they go hand-in-hand with lists. FOR loops are one of the most difficult topics for beginners, so expect to spend a lot of time with them. However, they are essential for doing useful things with lists, so you should be sure to learn them.\",\n  \"small_layout\": true,\n  \"video\": {\n    \"Bart\": \"https://blockpy.cis.udel.edu/videos/bakery_for_loops-Bart.mp4\",\n    \"Amy\": \"https://blockpy.cis.udel.edu/videos/bakery_for_loops-Amy.mp4\"\n  }\n}","starting_code":"","subordinate":true,"tags":[],"type":"reading","url":"bakery_for_loops_read","version":7},"ip":"216.73.216.157","submission":{"_schema_version":3,"assignment_id":1061,"assignment_version":7,"attempts":0,"code":"","correct":false,"course_id":37,"date_created":"2026-05-20T12:41:57.376033+00:00","date_due":"","date_graded":"","date_locked":"","date_modified":"2026-05-20T12:41:57.376033+00:00","date_started":"","date_submitted":"","endpoint":"","extra_files":"","feedback":"","grading_status":"NotReady","id":2036752,"score":0.0,"submission_status":"Started","time_limit":"","url":"submission_url-c8926c8e-f9a4-4f74-a542-616f91fac0ee","user_id":2044660,"user_id__email":"","version":0},"success":true}
