{"assignment":{"_schema_version":2,"course_id":37,"date_created":"2022-06-28T19:00:00+00:00","date_modified":"2023-10-04T15:20:05.075101+00:00","extra_instructor_files":"{\"&grades.txt\": \"95\\n94\\n93\\n100\\n87\\n45\\n87\\n98\"}","extra_starting_files":"","forked_id":null,"forked_version":null,"hidden":false,"id":1086,"instructions":"## Using Indexes\n\n```python\nvalues = [7, 4, 6, 5, 3, 9]\nprint(values)\n\n# Update value at index\nvalues[1] = 7\nprint(values)\n```\n\nLists have been a powerful addition to our toolkit, along with loops for processing them.\nBut back when we first started with lists, we only learned how to index specific elements.\nIt turns out that in addition to reading values in lists, we can also update those values.\nMuch like updating dataclass instances, the indexed list goes on the left-hand side of the assignment statement.\nThis mutates the list itself, once again similar to mutating a dataclass.\n\n## Index Iteration\n\n```python index-iteration\nvalues = [7, 4, 6, 5, 3, 9]\n\n# Iterate through indexes\nfor index in range(len(values)):\n    print(index, values[index])\n\n# Iterate through indexes AND values\nfor index, value in enumerate(values):\n    print(index, value)\n\n# Update indexes in place\nfor index in range(len(values)):\n    values[index] = values[index] * 2\nprint(values)\n```\n\nSometimes, we need to iterate through indexes instead of values.\nThis is useful for finding the location of an item in a list, perhaps to find an adjacent element.\nOr, you might want to actually update the original list, instead of creating a new list.\nMutating a list, however, means that other parts of the program that were using that list now have to be kept in mind.\nTherefore, we will typically avoid mutation in favor of creating new lists.\nStill, you should become familiar with the two approaches to iterating through indexes shown here, using the `enumerate` function and the `range` and `len` functions.\n\n## String Iteration\n\n```python\nvalues = \"AB, CD, EF\"\n\n# Each character on its own line!\nfor value in values:\n    print(value)\nprint(\"*******\")\n# [\"AB\", \" CD\", \" EF\"]\nfor value in values.split(\",\"):\n    print(value)\nprint(\"*******\")\n# [\"AB,\", \"CD,\", \"EF\"]\nfor value in values.split():\n    print(value)\n\n```\n\nStrings are a primitive type, but have some interesting features.\nMuch like how you can index both strings and lists, you can iterate through strings too.\nIf you iterate through a string directly, then it gives you each character one-by-one, just like if you indexed the string.\nHowever, you can use the `split` method to iterate through chunks of the string.\nWithout arguments, the string is split on whitespace.\nIf you provide a string as an argument, however, you can iterate based on a specific character.\nOne of the most common ways to iterate is by splitting on a comma, if the string has comma-separated values.\n\n## Filesystems and Files\n\n```python file-reading-example\ngrade_file = open('grades.txt')\ncontents = grade_file.read()\ngrade_file.close()\n\nprint(contents)\n```\n\nIn the second half of this chapter, we will learn more about the filesystem of your computer.\nYour computer is a collection of files organized into a filesystem.\nEach file has a name with an extension.\nFiles are grouped together into folders, also known as directories.\nFolders can also have other folders inside of them, leading to a hierarchy of files and folders.\nEach file across an entire filesystem can be uniquely identified by its path, which is the sequence of folders holding the file from the root to the file itself.\nBy referencing the path of a file, Python is able to read and write data to files.\nIn this example, we *open* the file, *read* its data as a string, and then *close* the file.\nYou must always remember to close files once you are done with them, to avoid memory leaks.\n\n## File Iteration\n\n```python file-iteration-example\ngrade_file = open('grades.txt')\n\ntotal = 0\ncount = 0\nfor line in grade_file:\n    grade = int(line.strip())\n    total += grade\n    count += 1\n\ngrade_file.close()\n\nprint(total/count)\n```\n\nYou might think of a file as a string or a list, since they share some characteristics.\nBut make no mistake, files are their own type, and they are critical for making sophisticated programs.\nBy letting us store data between executions and read data from other programs, we can solve a lot of new interesting problems.\nIn this example program, we iterate through a file line-by-line.\nSince each line of the file is an integer, we can grab the text of each line, convert the line to an integer, and then combine those integers to calculate an average.","ip_ranges":"","name":"7) 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  \"header\": \"Sequences\",\n  \"youtube\": {\n    \"Bart\": \"squyTXGm70g\",\n    \"Amy\": \"qjqdHfWtNbA\"\n  },\n  \"summary\": \"\",\n  \"small_layout\": true,\n  \"video\": {\n    \"Bart\": \"https://blockpy.cis.udel.edu/videos/bakery_sequences_primer-Bart.mp4\",\n    \"Amy\": \"https://blockpy.cis.udel.edu/videos/bakery_sequences_primer-Amy.mp4\"\n  }\n}","starting_code":"","subordinate":true,"tags":[],"type":"reading","url":"bakery_sequences_primer_read","version":9},"ip":"216.73.216.157","submission":{"_schema_version":3,"assignment_id":1086,"assignment_version":9,"attempts":0,"code":"","correct":false,"course_id":37,"date_created":"2026-05-20T14:01:47.590586+00:00","date_due":"","date_graded":"","date_locked":"","date_modified":"2026-05-20T14:01:47.590586+00:00","date_started":"","date_submitted":"","endpoint":"","extra_files":"","feedback":"","grading_status":"NotReady","id":2036915,"score":0.0,"submission_status":"Started","time_limit":"","url":"submission_url-a3b69534-73c5-412e-822f-df1aff79accf","user_id":2044668,"user_id__email":"","version":0},"success":true}
