+ 1

What's the difference between a, b=0,1 and a=0 b=1 in Python?

It gives me different result when i write it in function

17th Oct 2019, 12:31 PM
rabab mohamed
rabab mohamed - avatar
4 Answers
+ 4
`a = 0 b = 1` being written on the same line wouldn't work. You either write `a = 0` and `b = 1` on different line, or put a semicolon in between, as follows: `a = 0; b = 1`
17th Oct 2019, 12:45 PM
Ipang
+ 4
if you like to have it in one line, you can do it like that: a, b = 0, 1 # or a, b, c, d = 0, 1, 0, 5 it is a positiontional dependensy between left and right side.
17th Oct 2019, 2:03 PM
Lothar
Lothar - avatar
+ 3
a, b = b, a is used with the popular variable switching trick, it would not work with a = b; b = a: if a = 7, and b = 64. a, b = b, a ---> a = 64, b = 7 a = b; b = a ---> a = 64, b = 64 I think this is possible because b, a (on right side) is a tuple (tuples does not always need parentheses), whose values can be shared for variables with some special ways. When the variable values changed it does not change the values in the tuple.
17th Oct 2019, 2:05 PM
Seb TheS
Seb TheS - avatar
0
vdjdnc
17th Oct 2019, 3:18 PM
Wa HA
Wa HA - avatar