+ 1
what does (x , y) = (4 , 5) signify in Python? Is it a variable declaration or object. Please clarify
I am stuck in this. I am not able to understand this.
2 Respostas
+ 7
It's the same as doing this:
x = 4
y = 5
+ 2
It's a fancy way to declare two variables with one line.
This:
t = (4, 5)
gives you a normal tuple and you can get the first value with t[0] and the second value with t[1].
Now python has a feature called "destructuring" for extracting multiple things out of a tuple (or a list), so writing
(x, y) = t
assigns t[0] == 4 to x, and t[1] == 5 to y.
So when you write
(x, y) = (4, 5)
it's all mashed into a single line. It's the same as
x = 4
y = 5