Jump to content

Why does this print this!

Go to solution Solved by Guest,
4 minutes ago, UnspeC5d said:

So the interpreter ignores the first assignment or...? Thanks a lot for the reply

I think it's reassigning it 'cause you're trying to assign it twice on the same line/at the same time, so it overwrites it like how @C2dan88 illustrated.

x, y = 20, 60
y, x, y = x, y-10, x+10
print(x, y)

It returns 50, 30 which means the second line is as good as

x, y = y-10, x+10

Why is the first assignment useless?

 

Won't python first assign y = x, then do the operations and then finally gets reassigned?

PC

 

CPU : Ryzen 3 2200G

RAM: G SKILL Ripjaws 8GB 2400Mhz

MoBo: GigaByte A320M - HD2

Storage: WD Green SSD

Case: Cooler Master 

PSU: Cooler Master Elite 400W

Link to comment
https://linustechtips.com/topic/1243548-why-does-this-print-this/
Share on other sites

Link to post
Share on other sites

2 minutes ago, C2dan88 said:

Because you are overwriting the variables. Inline assignment is the same as this


x = 20
y = 60

y = x
x = y-10
y = x+10 <--- this will overwites the first assignment y=x

 

So the interpreter ignores the first assignment or...? Thanks a lot for the reply

PC

 

CPU : Ryzen 3 2200G

RAM: G SKILL Ripjaws 8GB 2400Mhz

MoBo: GigaByte A320M - HD2

Storage: WD Green SSD

Case: Cooler Master 

PSU: Cooler Master Elite 400W

Link to post
Share on other sites

1 minute ago, lewdicrous said:

Why are you assigning y to x, but then assigning y to x + 10?

It's printing x = 60 - 10, y = 30 + 10, hence the 50 30.

It's a question in my CS textbook

PC

 

CPU : Ryzen 3 2200G

RAM: G SKILL Ripjaws 8GB 2400Mhz

MoBo: GigaByte A320M - HD2

Storage: WD Green SSD

Case: Cooler Master 

PSU: Cooler Master Elite 400W

Link to post
Share on other sites

4 minutes ago, UnspeC5d said:

So the interpreter ignores the first assignment or...? Thanks a lot for the reply

I think it's reassigning it 'cause you're trying to assign it twice on the same line/at the same time, so it overwrites it like how @C2dan88 illustrated.

Link to post
Share on other sites

4 minutes ago, UnspeC5d said:

So the interpreter ignores the first assignment or...? Thanks a lot for the reply

No a computer works line by line.

 

x = 20

y = 60

 

y = x //y is now 20

x = y-10 //x is now 10

y = x+10 //y is now 20

 

Anything you assign to a variable overwrites the previous assigned value.

Link to post
Share on other sites

1 hour ago, jaslion said:

No a computer works line by line.

 

x = 20

y = 60

 

y = x //y is now 20

x = y-10 //x is now 10

y = x+10 //y is now 20

 

Anything you assign to a variable overwrites the previous assigned value.

Doesn't work that way when I run it 🤷‍♂️

PC

 

CPU : Ryzen 3 2200G

RAM: G SKILL Ripjaws 8GB 2400Mhz

MoBo: GigaByte A320M - HD2

Storage: WD Green SSD

Case: Cooler Master 

PSU: Cooler Master Elite 400W

Link to post
Share on other sites

The byte code makes it quite clear:

  1           0 LOAD_CONST               0 ((20, 60))
              2 UNPACK_SEQUENCE          2
              4 STORE_NAME               0 (x)
              6 STORE_NAME               1 (y)

  2           8 LOAD_NAME                0 (x)
             10 LOAD_NAME                1 (y)
             12 LOAD_CONST               1 (10)
             14 BINARY_SUBTRACT
             16 LOAD_NAME                0 (x)
             18 LOAD_CONST               1 (10)
             20 BINARY_ADD
             22 ROT_THREE
             24 ROT_TWO
             26 STORE_NAME               1 (y)
             28 STORE_NAME               0 (x)
             30 STORE_NAME               1 (y)
             32 LOAD_CONST               2 (None)
             34 RETURN_VALUE

This is perfectly normal and what we expect. When doing an assignment, it will fully evaluate the right hand side before storing/setting the variables.

 

The code that @jaslion is running is not in any way equivalent, hence the different output.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×