May 2017
Intermediate to advanced
280 pages
6h 2m
English
In this section, we will learn how to unpack the tuple variable. Let's learn by example:
>>> tup1 = (1,2,3)>>> a,b,c = tup1>>> a1>>> b2>>> c3
In the preceding example, tuple's items have been assigned to a, b, and c variables correspondingly.
What happens if you use more number of variables than the number of items in a tuple. Let's see more examples:
>>> tup2 = (5,6)>>> x,y,z = tup2Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> x,y,z = tup2ValueError: need more than 2 values to unpack
So, interpreter throws a ValueError.
What happens if you use less number of variables than the number of items in a tuple. Let's see more examples:
>>> tup2 = (5,6)>>> x,y,z = tup2Traceback ...
Read now
Unlock full access