Remember
A variable name refers to an object. Python evaluates the right-hand side before binding or rebinding the name; assignment does not copy an object.
A variable name refers to an object. Python evaluates the right-hand side before binding or rebinding the name; assignment does not copy an object.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
total = 10
total = total + 2
shopping_list = ["tea"]
basket = shopping_list
basket.append("milk")
print(total)
print(shopping_list)Top-level scope
12Object 3 · list['tea', 'milk']A variable name refers to an object. Python evaluates the right-hand side before binding or rebinding the name; assignment does not copy an object.
(no output)
What do you expect this program to print?
shopping_list ----+
|
+--> ["tea", "milk"]
| ONE LIST
basket -----------+Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
total = 10
total = total + 2
shopping_list = ["tea"]
basket = shopping_list
basket.append("milk")
print(total)
print(shopping_list)Top-level scope
12Object 3 · list['tea', 'milk']The example has finished. Compare the final output with the values and references above.
12 ['tea', 'milk']
What do you expect this program to print?
total = 10
total = total + 2
shopping_list = ["tea"]
basket = shopping_list
basket.append("milk")
print(total)
print(shopping_list)A variable name refers to an object.
A name lets you refer to an object, such as a number or a string.
For example, you can create a variable called name and refer to your name with it:
name = "Arjun"
Here, name is the variable, and "Arjun" is the string object that the name refers to.
Let's save a number in a variable:
age = 25
Now, the name age refers to the integer 25.
After you create a variable, you can use it whenever you want. For example:
name = "Arjun"
age = 25
print(name)
print(age)
Arjun
25
_ instead. (Example: my_age)price is better than p.)Connect what you noticed in the example with the idea below.
A variable name refers to an object. Python evaluates the right-hand side before binding or rebinding the name; assignment does not copy an object.
Assigning a list to another name makes an independent copy.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.