0

this is my first question on the side as a beginner who is trying to learn how to code.

Here the issue (Python):

x = 50000
count = 0

number = x
while  x > 1:
    x = x/10
    count = count+1

print(x)

If I print x I get 0.5, as expected. However if i print number I get 50000, instead of 0.5. Why is that the case, considering that I´ve initialised the number variable with the x variable, that now has the updated value of 0.5 after having iterated through the loop?

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
  • 9
    Required reading for all new Python programmers: [Python Names and Values](https://nedbatchelder.com/text/names1.html). Also worth reading: [Mutability & Immutability in Python](https://pythonsimplified.com/mutability-immutability-in-python/). – jarmod Jan 30 '22 at 16:57
  • 3
    Why would you expect `number` to change? When you do `number = x` you say: "The object with the name `x` now gets the additional name `number`". This object is the integer value `50000`. Now you do `x = x/10` where you say: "The object I get from the division `x/10` gets the name `x` now." There's no reason `number` should be changed too. – Matthias Jan 30 '22 at 16:59
  • To restate what some prior comments have also said: Copying the value pointed to by `x` into `number` at _one_ point in time doesn't make `number` point to `x` in the future as well. – Charles Duffy Jan 30 '22 at 17:02
  • ...a language that behaved the way the question anticipates would be very different. What would `x = x / 10` even mean, if assignments are setting up relationships between numbers instead of performing operations at the immediate point in time? (If you change `x`, it's no longer 1/10th of its prior value, so the relationship no longer holds; in Python-as-it-actually-exists that's fine; in a language where code described relationships instead of operations, you've instead created a paradox). – Charles Duffy Jan 30 '22 at 17:03

2 Answers2

3

You can imagine computer memory as a chest of drawer. Each memory cell is a kind of drawer.

In Python when you do an instruction like x = 5, you put 5 in a drawer (memory cell) and you put the label x on the drawer, x is a reference.

When you do number = x, you add a label number to the drawer with 5 inside. So x and number are two labels on the same drawer with 5 inside.

When you do the instruction x = x/10, Python compute x/10 and put the result 500 in a new drawer (it creates a new reference) and remove the label x from the first drawer with 5 inside and put it on the new drawer with 500 inside. The label number doesn't move, it is always on the drawer with 5 inside.

-1

You need to say number = x after all the calculations for x because number = the x before the calculations making it 50000.

pokefalls
  • 53
  • 9
  • Why is this downvoted? I get it is a simple question but isn't this a suitable answer? – Arson 0 Jan 30 '22 at 17:10
  • Thank you Arson 0. I just wanted to add a simple answer to help the person understand. – pokefalls Jan 30 '22 at 17:12
  • Yeah, I understand, from seeing the answer from Larry I feel this one does miss the approach necessary to make a beginner understand the underlying mechanisms of the language. Nevertheless, it is factually correct. – Arson 0 Jan 30 '22 at 17:14
  • 2
    It's correct, but I don't see how much good it does to help the person understand _why_ the change is needed. – Charles Duffy Jan 30 '22 at 17:17
  • I added this answer because the person might want a answer which is simpler and then figure out why the change is needed. – pokefalls Jan 30 '22 at 17:24
  • Also if I were to explain why the change Is needed I would also add the fact that python reads from top to bottom which I think Is something Larry could off added. – pokefalls Jan 30 '22 at 17:32