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.
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?
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.
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/
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?
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.
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.