What happens when you are updating the value of some variable?


#1

Question

In the context of this exercise, what actually happens when you change a variable to some other value in Python?

Answer

In Python, variables can be seen as references to some object, which can include numbers, strings, and other values.

When you assign a variable to some value, it will reference that value, and become bound to it. So in this exercise, when the variable quilt_width is assigned to the value 8, it references and becomes bound to that value. And, if that variable is used within some calculation, it would be treated the same as the number it references.

Example code

# This variable is assigned to the value 20.
value = 20

# When used in a calculation, the value it references is used.
total = value + 30 # 50

When you reassign a variable to another value, it simply changes the reference to that new value and becomes bound to it.