{"assignment":{"_schema_version":2,"course_id":37,"date_created":"2022-06-28T19:00:00+00:00","date_modified":"2023-09-16T20:16:39.021644+00:00","extra_instructor_files":"","extra_starting_files":"","forked_id":null,"forked_version":null,"hidden":false,"id":1058,"instructions":"## Introduction to For Loops\n\n```python simple-loop\nnames = [\"Ada\", \"Babbage\", \"Captain\"]\n\nfor name in names:\n    print(name)\n```\n\nWriting the same code over and over again, in order to do something multiple times, is tedious and likely to lead to mistakes.\nThat's why we have loops to let us do an action repeatedly.\nIn Python, the most common looping construct is the `for` loop, which works directly with lists.\nUsing a `for` loop, each element of the list is processed one at a time.\nA `for` loop has three major components: the iteration variable, the iteration list, and the body of the loop.\nThe iteration list is the variable following the `in`, and represents the data we are going to iterate over.\nThe variable before the `in` is the iteration target, and it will be a newly created variable that did not exist prior to the loop.\nThe iteration variable is key to understanding the `for` loop, since it represents each element of the list, but only one at a time.\nThat iteration variable is available inside the body of the `for` loop, which will be executed one time for each element of the list.\nEach time the loop executes, the iteration variable gets set to a new value from the list, until the list is empty.\n\n## Loop Patterns\n\n![Two blocks of code side by side. The left side shows the Sum Loop Pattern, and the right side shows the actual code that would be based off that pattern.](bakery_loops_primer_use_template.png)\n\nBecause there are so many ways to use `for` loops, we focus on covering loop *patterns*, which are reusable code templates to\nsolve commonly occuring problems involving loops.\nTo use the loop pattern, you copy and paste the pattern into your program and fill in the blanks.\nYou are always going to have certain blanks to fill in, like the fact that there's an original list you are iterating over, an iteration variable, and some kind of resulting value from the loop.\nHowever, past that, the actual patterns vary quite a bit between each other.\nSometimes, you have to make alterations to the template, or use more than one line to capture the original idea.\nAt the end of the day, the patterns are only guidelines, not concrete rules.\nWe use the patterns as a teaching guide to help you get started with loops, although they do reflect the kinds of loops that professional programmers write all the time.\n\n## All the Patterns\n\n* Count: Count how many elements are in a list\n* Sum: Add up all the numbers in a list\n* Accumulation: Combine elements into a single value\n* Map: Modify all the elements of a list\n* Filter: Remove elements from a list\n* Find (First and Last): Get an element based on its value\n* Take: Remove elements past a certain point in the list\n* Minimum/Maximum: Get the highest or lowest value in a list\n\nThe complete list of Loop Patterns covered in this chapter is shown here.\nAs you can see, there are quite a few patterns.\nLet's quickly run through each one.\nThe Count pattern is used to determine how many elements are in a list, and works on any kind of list.\nThe Sum pattern only works on lists of numbers, in order to add them all together.\nHowever, the Sum pattern is related to the Accumulation pattern, which generalizes summing to other types like strings and booleans.\nThe Map pattern is more distinctive, letting you create a new list based on an old list, modifying each value along the way.\nThe Map pattern might be used to convert elements from one type to another, but it might also be used to just change a value without changing its type.\nThe Filter pattern is used to create a new list by only keeping certain elements, through the use of an `if` statement embedded inside of the loop.\nThe Find pattern also uses an `if` statement, but only determines a specific element rather than producing an entire list.\nThe third related pattern is Take, which also produces a list based off the original list, but this time removes all the elements until a certain condition is encountered.\nNote that Find and Take are different from indexing and subscripting, since they are based on the actual values of the list rather than their positions.\nFinally, there is the Minimum and Maximum patterns, which find the lowest and highest values in the list, respectively.\n\n## Loop Composition\n\n```python loop-composition\ndef remove_dollars(amounts: list[str]) -> list[str]:\n    new_amounts = []\n    for amount in amounts:\n        new_amounts.append(amount[1:])\n    return new_amounts\n\ndef sum_amounts(amounts: list[str]) -> int:\n    total = 0\n    for amount in amounts:\n        total = total + int(amount)\n    return total\n\ndollars = [\"$1\", \"$5\", \"$10\", \"$1\"]\nprint(sum_amounts(remove_dollars(dollars)))\n```\n\nWorking with loops can be quite tricky, even with the patterns helping you.\nWhen you need to do multiple loop patterns all at once, you can sometimes get overwhelmed with the logic of the loops.\nThe final part of this chapter describes how you can compose loops using functions, where each function only implements\none loop pattern.\nAlthough this increases the number of functions you have to write and test, there can be some huge advantages when you start working with more complex programs.\nFirst, each function can be tested in isolation, to make sure that it works.\nSecond, the pieces of the functions are reusable, so you might save yourself some work later.\nThird, the final problem becomes easier to think about, as demonstrated by the final line of code here, where we can simply compose a few function calls to achieve our purpose rather than having to look at a single big ugly `for` loop.\nEach individual function only does one thing, and does it well, so we can determine any mistakes in that function more easily.","ip_ranges":"","name":"6) Primer Reading","on_change":"","on_eval":"","on_run":"","owner_id":1,"owner_id__email":"acbart@udel.edu","points":4,"public":true,"reviewed":false,"sample_submissions":[],"settings":"{\n  \"small_layout\": true,\n  \"youtube\": {\n    \"Bart\": \"CxOl_iwL1Wg\",\n    \"Amy\": \"vQHeZcG7OVc\"\n  },\n  \"header\": \"For Loops\",\n  \"summary\": \"Note that there is a second page in this Primer, containing a reference sheet for the Loop Patterns! Make sure you download a copy of that reference sheet!\",\n  \"video\": {\n    \"Bart\": \"https://blockpy.cis.udel.edu/videos/bakery_for_primer-Bart.mp4\",\n    \"Amy\": \"https://blockpy.cis.udel.edu/videos/bakery_for_primer-Amy.mp4\"\n  }\n}","starting_code":"","subordinate":true,"tags":[],"type":"reading","url":"bakery_for_primer_read","version":7},"ip":"216.73.216.157","submission":{"_schema_version":3,"assignment_id":1058,"assignment_version":7,"attempts":0,"code":"","correct":false,"course_id":37,"date_created":"2026-05-20T14:01:41.286823+00:00","date_due":"","date_graded":"","date_locked":"","date_modified":"2026-05-20T14:01:41.286823+00:00","date_started":"","date_submitted":"","endpoint":"","extra_files":"","feedback":"","grading_status":"NotReady","id":2036901,"score":0.0,"submission_status":"Started","time_limit":"","url":"submission_url-5001a049-0da3-4a69-9468-88c81bf94c45","user_id":2044668,"user_id__email":"","version":0},"success":true}
