Flash Sale: Flat 70% + Addl. 25% Off on all Courses | Use Coupon DS25 in Checkout | Offer Expires In

  Enroll Now

Tuple Assignment

You can have tuple on the left-hand side of the assignment as well.

l = [1,2,3]
(a,b,c) = l

Here we assigned a list with values 1, 2 and 3. And, then we assign the list to a tuple containing 3 elements. As a result, a gets assigned with l[0], b with l[1] and c with l[2].

We can also write it without any parenthesis (brackets) and it is equally valid like this,

l = [1,2,3]    
a, b, c = l

It helps us in swapping elements in a pretty way,

a , b =  b, a

Both sides of this statement are tuples, but the left side is a tuple of variables. The right side is a tuple of expressions. Each value on the right side is assigned to its respective variable on the left side.

All the expressions on the right side are evaluated before any of the assignments.

The number of variables on the left and the number of values on the right must be the same.

If you are wondering why we used the only list on the right-hand side here. So, for your information, the right side can be any kind of sequence (string, list, or tuple).


No hints are availble for this assesment

Answer is not availble for this assesment

Loading comments...