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


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


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.


Hello, please what am i doing wrong.

This is the question:
You’ve decided to get into quilting! To calculate the number of squares you’ll need for your first quilt let’s create two variables: quilt_width and quilt_length . Let’s make this first quilt 8 squares wide and 12 squares long. Print out the number of squares you’ll need to create the quilt!


It turns out that quilt required a little more material than you have on hand! Let’s only make the quilt 8 squares long. How many squares will you need for this quilt instead?

My solution:
quilt_width = 8
quilt_lenght = 12
print (quilt_width * quilt_lenght)
quilt_lenght = 8
print (quilt_width * quilt_lenght)

1 Like

Your code is the same as mine essentially.

quilt_width = 8
quilt_length = 12

#updated quilt length
quilt_length = 8

What errors are you getting?

Check the spelling. That’s likely the cause of any errors.


*length = MTF is right. Spelling caused the error

Can someone give an example why we would change a variable with the same name like in this exercise? Why leave the old value? Why not just change the value at the original variable?

because this example is over simplified. Now the value is hard-coded within the python script, while in reality this value will more likely come from a different source (user input for example), if the user enters a too large number, you will have to prompt again, given you just can’t change the number

Let’s say we have bread loafs coming down the line and we need to be sure they weigh at least twenty ounces, else they are slid off to side for another market.

We get one loaf at a time, we can call it loaf. Being an object it could have a weight attribute…

if loaf.weight < 20: reject()

Or if no attributes, at least a way of determining weight, weigh it. That gives us a value, weight = None where None is replaced with the actual weight value.

In the latter circumstance, with thousands of loaves per hour coming down the line, the value for weight will be changing very rapidly, but at each interval we have a chance to test it against the relational operation, less than twenty, and determine a course of action in that case. It was not necessary to create a new variable in each of these instances. The one is quite enough to satisfy our requirements.