0

Why does

a = 3
b = 5
a,b = a + b,a   
print a,b
-------
8 3

differ from

a = 3
b = 5
a = a + b
b = a
print a,b
------
8 8

Can someone explain how Python interprets the assignments differently? Does Python still keep the old value of a in the first paragraph of code, when a is assigned to a + b? (a= a+b)

Also, does these assignment properties change from Python 2 to Python 3?

Xuan
  • 101
  • 9
  • expressions are evaluated from right to left. So `a` isn't assigned until the right `a` is evaluated. There is a duplicate of this somewhere. – Jean-François Fabre Sep 04 '17 at 12:15
  • Maybe https://stackoverflow.com/questions/24844487/swap-2-values-of-2-variables-without-using-a-third-variable-python or https://stackoverflow.com/questions/21047524/how-does-swapping-of-members-in-the-python-tuples-a-b-b-a-work-internally?noredirect=1&lq=1? – MSeifert Sep 04 '17 at 12:16
  • https://stackoverflow.com/questions/14836228/is-there-a-standardized-method-to-swap-two-variables-in-python I am not going to hammer but this seems like a dupe. – ayhan Sep 04 '17 at 12:16
  • you're not going to hammer... because I already did :) added your question as well. – Jean-François Fabre Sep 04 '17 at 12:17
  • 1
    COLDSPEED called me "the king of the dupehammer". I'm flattered, but now I have to make it worth :) – Jean-François Fabre Sep 04 '17 at 12:19
  • @Jean-FrançoisFabre sorry im new here but what are the repercussions of a dupe question, gotta be careful next time.. – Xuan Sep 04 '17 at 12:21
  • No repercussions unless a lot of questions you ask are heavily downvoted or duplicates. Asking the question to google often brings you to a SO answer. That avoids a lot of duplicates – Jean-François Fabre Sep 04 '17 at 12:43
  • you can read about account suspensions on meta: https://meta.stackoverflow.com/questions/250076/account-suspended-for-1-year-is-this-correct. Be careful, see [ask], and you won't have any problems! this isn't a site made of fascists :) – Jean-François Fabre Sep 04 '17 at 12:52

0 Answers0