{"assignment":{"_schema_version":2,"course_id":37,"date_created":"2022-06-28T19:00:00+00:00","date_modified":"2023-09-08T19:05:08.520008+00:00","extra_instructor_files":"","extra_starting_files":"","forked_id":null,"forked_version":null,"hidden":false,"id":929,"instructions":"## Reading and Writing Variables\n\n```python variable-reading-writing\n# Writing variables\nmy_int_variable = 5\nmy_str_variable = \"hello\"\nmy_bool_variable = True\n# Reading variables (and printing them)\nprint(my_int_variable)\nprint(my_str_variable)\nprint(my_bool_variable)\n# Read AND write variable\nmy_int_variable = my_int_variable + 3\n# Reading variables again (and printing again)\nprint(my_int_variable)\n```\n\nTo write to a variable, you put the variable's name on the left side of an _assignment statement_.\nTo read from a variable, you simply use the variable's name.\n\n## = vs. ==\n\n```python equality-assignment\n# Assigning to a variable\ncurrent_hour = 7\nprint(\"The current hour is\", current_hour)\n\n# Equality operation\nprint(current_hour == 12)\n\n# Assigning the result of an equality operation\nis_midnight = current_hour == 12\nprint(\"Is it midnight?\", is_midnight)\n```\n\nNotice that assignment statements use a single equal sign (`=`).\nWe mentioned before that the equality operator uses two equal signs.\nThey may appear to be visually similar, but they are different.\nAssignment statements update the value in a variable, while equality operators ask a question about values.\n\n## Lifetime\n\n```python variable-lifetime\nfootball_score = 0\nprint(football_score)\n\nfootball_score = 10\nprint(football_score)\n\nfootball_score = 27\nprint(football_score)\n```\n\nVariables have a lifetime.\nAt some point in the program, they are initialized, and then later on they are used.\nAt any given time, you can ask what the value of a variable is and change the value.\nIn the following program, the variable has a different variable each time the variable is printed because of the assignment statements.\n\n## Read after Write\n\n```python read-after-write\n# This will work\nalpha = 5\nprint(alpha)\n\n# This will NOT work!\nprint(beta)\nbeta = 5\n```\n\nA variable cannot be read until it has been written.\nIf you attempt to read an unwritten variable, an error will occur.\nMake sure that any time you are reading a variable, you have an assignment statement preceding it.\n\n## Update\n\n```python example-update\ncount = 0\ncount = count + 1\ncount = count + 1\n\nprint(count)\n```\n\nWe often want to increase or decrease a variable by a value.\nFor instance, when we're counting the number of things in a list, we need to add one to an existing variable.\nIn the following code, we have a variable on the left- and right-hand side of the assignment statement.\nThis really highlights the difference between math and coding.\nIn Algebra, it would be impossible to write that statement because you are solving for the variable.\nBut in computing, you are ASSIGNING to the variable.\nYou are overwriting whatever was there before.\nThat's why you should read this statement starting from the right side.\nThe expression literally means, \"Evaluate the expression on the right and update the variable on the left with the result.\"\n\n## Tracing\n\n```python example-tracing\n# Variable does not exist yet\ncount = 0\n# count is 0\ncount = count + 5\n# count is now 5\ncount = count + 2\n# count is now 7\n```\n\nIt is often useful to track the value of variables over time.\nWe call this \"tracing\" a variable.\nAlthough the computer only keeps track of the latest state of the variables, we humans want to think about the entire lifetime of the variables.\nOften, to debug a program, we will write down the name of each variable on a piece of paper.\nThen, we check each line of the program, one at a time, writing down the variable's value next to its name whenever the variable is assigned.\nIf the variable already has a value, we cross out the value and replace it with the new value.\n\n## Initializing and Updating\n\n```python initialize-update\n# Initialization\ncount = 0\n\n# Update\ncount = count + 5\n\n# Read\nprint(count)\n```\n\nThe first time a variable is written to, we say that the variable has been \"initialized\" or \"created.\"\nAny time after that, writing to the variable is referred to as being \"updated\" or \"changed.\"\n\n## Terminology\n\n* Write: Set, Store, Save\n* Initialize: Define, Create\n* Update: Change, Increment\n* Read: Use, Load\n\nBesides \"reading\" and \"writing\", there are many other synonyms for the concepts of manipulating a variable.\nYou should get a sense of the different words we use: write, set, store, save, initialize, define, create, update, change, increment, read, use, and load.\n\n## Summary\n\n- Variables can be defined and updated using an assignment statement.\n- Assignment statements use one equal sign (`=`), while equality comparisons use two equal signs (`==`).\n- The value of a variable can be \"traced\" over the lifetime of the program's execution, with its current value tracked at each step.\n- There are many synonyms for the verbs related to variables:\n  - Write: Set, Store, Save\n  - Initialize: Define, Create\n  - Update: Change, Increment\n  - Read: Use, Load\n","ip_ranges":"","name":"1B2) Tracing 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\": \"Tracing\",\n  \"slides\": \"bakery_intro_tracing.pdf\",\n  \"youtube\": {\n    \"Bart\": \"iZ1Y0iRmYnI\",\n    \"Amy\": \"92PerPC2fG4\"\n  },\n  \"video\": {\n    \"Bart\": \"https://blockpy.cis.udel.edu/videos/bakery_intro_tracing-Bart.mp4\",\n    \"Amy\": \"https://blockpy.cis.udel.edu/videos/bakery_intro_tracing-Amy.mp4\"\n  },\n  \"small_layout\": true\n}","starting_code":"","subordinate":true,"tags":[],"type":"reading","url":"bakery_intro_tracing_read","version":8},"ip":"216.73.216.157","submission":{"_schema_version":3,"assignment_id":929,"assignment_version":8,"attempts":0,"code":"","correct":false,"course_id":37,"date_created":"2026-05-20T23:50:40.060229+00:00","date_due":"","date_graded":"","date_locked":"","date_modified":"2026-05-20T23:50:40.060229+00:00","date_started":"","date_submitted":"","endpoint":"","extra_files":"","feedback":"","grading_status":"NotReady","id":2037103,"score":0.0,"submission_status":"Started","time_limit":"","url":"submission_url-0b9ecbca-98a3-422f-8845-5b294222bb83","user_id":2044772,"user_id__email":"","version":0},"success":true}
