0

Python variables in order?

Is it not true that when you define a variable once, but then you define it again, that the variable will take the latest definition when it is called? For example, if I define x=1 but then afterward say x=2 and then print(x), won't it output 2 instead of 1?

31st Jan 2017, 7:10 PM
Christina Phillips
Christina Phillips - avatar
2 Réponses
+ 2
yes x should print 2 in that case
31st Jan 2017, 7:39 PM
sebipincha
sebipincha - avatar
+ 1
Yes, except if you don't do the assignment in same scope: def func(): x=2 x=1 print(x) # output 1 func() print(x) # still output 1 ^^
31st Jan 2017, 10:21 PM
visph
visph - avatar