0% found this document useful (0 votes)
4 views

Chapter 7 Tuple

Uploaded by

jhavidya563
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter 7 Tuple

Uploaded by

jhavidya563
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Matrix Computers (Page 1 of 4)

10/564 Kaveri Path, Mansarovar Jaipur-302020 Rajasthan


9414752750, 9414930104, 9414244351, 0141-2399633

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)

Parenthesis are optional:-


t1=("a","b","c","d")
#or
t1="a","b","c","d"

Tuple is hetrogeneous:-
t1=(1,5.4,"abc",[1,2,3],(1,2,3))

Nested tuple:-
t1 = ((1,2),(3,4),(5,6))

Two empty tuples share common id:-


t1=()
t2=()
t1 is t2 #True

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 supports indexing:-


t1=(10,20,30)
print(t1[0]) #10
print(t1[3]) #error

Tuple supports slicing:-


t1=(10,20,30)
print(t1[0:]) #(10,20,30)
print(t1[0:2]) #(10,20)

Tuple is immutable but it can contain both mutable and immutable elements: -
t1=(1,"abc",5.4, (1,2,3), [1,2])

t1[0]=10 #not allowed


del t1[4] #not allowed
#but
del t1[4][1] #is allowed
del t1[4][0]
Matrix Computers (Page 3 of 4)
10/564 Kaveri Path, Mansarovar Jaipur-302020 Rajasthan
9414752750, 9414930104, 9414244351, 0141-2399633

del t1

Tuple supports ordering: -


(1,2,3) == (3,1,2) #False

Basic Tuples Operations: -


tuple+complex #error
tuple+number #error
tuple+string #error
tuple+list #error
(1,3)+(4) #error

(1,3)+(4,) #Concatenation is allowed


(1,2)*4 (1,2,1,2,1,2) Repetition
3 in (1, 2, 3) True Membership
for x in (1,2,3):
print x, 1 2 3 Iteration

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;

Built-in Tuple Functions


Sr.No. Function Description
1 len(tuple) Gives the total length of the tuple.
2 max(tuple) Returns item from the tuple with max value.
3 min(tuple) Returns item from the tuple with min value.
4 sum(tuple) Returns sum.
5 tuple(seq) Converts a list into tuple.
5 sorted(seq,
[reverse=True/False]) Returns a sorted list.

Python includes following list methods(method argument is case sensitive)


Sr.No. Methods Description
1 tuple.count(obj) Returns count of how many times obj occurs in tuple
2 tuple.index(obj) Returns the lowest index in list that obj appears

tuple unpacking:-
t=(1,2)
a,b=t

Tuple can be modified by converting it in list: -


t=(1,2)
l=list(t)
l[0]=10
Matrix Computers (Page 4 of 4)
10/564 Kaveri Path, Mansarovar Jaipur-302020 Rajasthan
9414752750, 9414930104, 9414244351, 0141-2399633

l[1]=20
t=tuple(l)

Copy a tuple to another:-


A. If a tuple contains all immutable elements then only reference copy:-
t1=(10,20,30)

t2=t1 #Reference Copy


t1 is t2 #True

t2=t1[0:] #Reference Copy


t1 is t2 #True

t2=copy.deepcopy(t1) #Reference Copy


t1 is t2 #True

B. If a tuple contains mutable elements then:-


t1=(10,[20,30],40)

t2=t1 #Reference Copy


t1 is t2 #True

t2=t1[0:] #Shallow Copy


t1 is t2 #False
t1[1][0]=21 #This will also change in t2

t2=copy.deepcopy(t1) #Deep Copy


t1 is t2 #False
t1[1][0]=21 #This will not change in t2

You might also like