Chapter 7 Tuple
Chapter 7 Tuple
Chapter 7 Tuple
Tuple is an immutable collection of heterogeneous comma separated values that can be put in
parenthesis. It is just like list. We can change list but we can't change tuple.
Empty tuple:-
t1 = ()
t2 = tuple()
Single element:-
t1 = (1,)
type(t1) <class tuple>
t2 = 1,
type(t2) <class tuple>
t3 = (1) # it will not create tuple
type(t3) <class int>
Multiple element:-
t1=(1,2,3)
t1=tuple([1,2,3])
t1=tuple((1,2,3))
t1=tuple(1,2,3) #Wrong
t1=eval(input("Enter a number")) #if 1,2,3 is entered then it will be (1,2,3)
Tuple is hetrogeneous:-
t1=(1,5.4,"abc",[1,2,3],(1,2,3))
Nested tuple:-
t1 = ((1,2),(3,4),(5,6))
Two non empty tuples with same values share different id:-
t1=(1,2,3)
t2=(1,2,3)
t1 is t2 #False
Matrix Computers (Page 2 of 4)
10/564 Kaveri Path, Mansarovar Jaipur-302020 Rajasthan
9414752750, 9414930104, 9414244351, 0141-2399633
Tuple is immutable but it can contain both mutable and immutable elements: -
t1=(1,"abc",5.4, (1,2,3), [1,2])
del t1
Any set of multiple objects, comma-separated, written without brackets, parentheses etc., default to
tuples: −
print('abc', -4.24e93, 18+6.6j, 'xyz');
x,y=1,2;
tuple unpacking:-
t=(1,2)
a,b=t
l[1]=20
t=tuple(l)