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

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.

15 Likes

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!

2.

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)

4 Likes

Your code is the same as mine essentially.

quilt_width = 8
quilt_length = 12
print(quilt_width*quilt_length)

#updated quilt length
quilt_length = 8
print(quilt_width*quilt_length)

What errors are you getting?

1 Like

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

19 Likes

*length = MTF is right. Spelling caused the error

5 Likes

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?

1 Like

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

1 Like

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.

5 Likes

What is garbage collection?

Roughly speaking it’s a way of cleaning up of objects from memory when they are no longer referenced in a program. Memory allocation and deallocation in python is almost entirely automated.

Let’s say you assigned a string to the name word and in the next line assigned a new string to that same name- word. The original object (the string object) is no longer referenced in the program and can be removed from memory.

The basic memory management process simply counts the number of references an object has and preapres them for garbage collection when this count reaches zero (like the reassignment example above). This process is automated by the in-built garbage collector and will simply free this memory for you at a certain point in the future. Note that this is not immediate though you can request this to occur.

word = "first test"
word = "second test"  # we have lost our reference to the original object
# it is now open to garbage collection

It’s a bit more complex than that when it comes to cylic references and such (objects that reference one another) and you’d have to nose though the docs to work out how it goes about trying to remove these-
https://devguide.python.org/garbage_collector/

4 Likes

Can someone please explain why:

Blockquote
quilt_width = 8
quilt_length = 12
required_squares = (quilt_width * quilt_length)
print(required_squares)
quilt_length = 8
print(required_squares)
Blockquote

incorrectly results in:
96
96

Whereas:

Blockquote
quilt_width = 8
quilt_length = 12
required_squares = (quilt_width * quilt_length)
print(required_squares)
quilt_length = 8
required_squares = (quilt_width * quilt_length)
print(required_squares)
Blockquote

Correctly results in:
96
64

In the first example, why is required_squares not updated to reflect the updated quilt_length variable on which it depends?

Thanks

6 Likes

Because required_squares doesn’t depend on quilt_length.

required_squares just remembers the number it has been assigned.

1 Like

Ok, thanks for the explanation.

In that case, what would be the most efficient way to make required_squares dynamic - so that it automatically updates the number assigned to it, each time quilt_width or quilt_length are updated?

Is there an easy way to make this relationship dynamic? Or must required_squares be redefined each time to update it, as in the second example code? This seems quite laboured?

Not that I know. Besides, change the width might also mean you intent to make a new quilt.

31 year old complete beginner here - This is my first language, I am a real mild user of computers and am terrible at math. I am looking to change all that and start a new career.

My very first question is -

Does Python always know to refer to most recent update of the variable?

At first I needed 12 but then I needed 8, rather than simply deleting the original variable or reassigning it, I have created a new variable with 8 assigned for both width and length.

quilt_width = 8
quilt_length = 12
print(quilt_width * quilt_length) - Displays 96
quilt_length = 8
print(quilt_width * quilt_length) - Displays 64

Will it always print both variables now or will it only refer to the most recent assignment?

I am so sorry for the format of the question, I don’t know what I don’t know and I don’t know how to ask about it.

Thank you all in advance.

1 Like

There is only one value to choose from, the most recent.

1 Like

Thank you for your reply, I really appreciate it.

So realistically, If I wanted to keep my code clean I could just adjust the original line and it would still be accurate?

1 Like

I’d be less concerned about clean code and more about code that works. We need that as a starting point in the clean up process.

1 Like

Thank you for your reply, you are a legend.

Build the thing, then polish the thing! haha.

2 Likes

Bingo!

No matter how basic something can appear on the surface, there may be many underlying facets we do no see. Peel back EVERY layer of the onion if you wish to see the actual core.

1 Like