Pb-U7.4 Tuple
Pb-U7.4 Tuple
Tuples
Introduction :
o TUPLE is a collection of items and each item has its own index value.
o Tuple is a standard data type of Python. It is a sequence which can store values of any kind.
o Tuple is represented by round brackets / parentheses “ ( ) “
o Tuple is an immutable data type which means we cannot change any value of tuple.
o In Python Tuple is a sequence of items and each item can be individually access using index.
From beginning the first item in Tuple is at index 0 and last will be at len-1. From backward
direction last item will be at index -1 and first item will be at –len.
Forward indexing
0 1 2 3 4 5 6 7 8 9 Positive Index
70 10
10 20 30 40 50 60 80 90 Values
0
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 Negative Index
Backward indexing
Creating & Initializing Tuple:
Creating a tuple
Tuples are enclosed in parentheses () and each item is separated by a comma.
Syntax: TupleName = ( )
o Creating tuple from existing sequences (i.e. Using tuple() function…
Syntax: TupleName = tuple(sequence)
Code: Output:
>>>Mytuple = tuple(‘Hello’) (‘H’,’e’,’l’,’l’,’o’)
>>>Mytuple
Code: Output:
>>>Mytuple = [‘H’,’e’,’l’,’l’,’o’] (‘H’,’e’,’l’,’l’,’o’)
>>>Mytuple1 = tuple(Mytuple)
>>>Mytuple1
Initializing a tuple
Passing value in tuple while declaring tuple is initializing of a tuple.
Syntax: TupleName = ( value1, value2,…..)
0 1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90 100
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Code: Output:
>>> n=( 10,20,30,40,50,60,70,80,90,100)
>>>n[1] 20
>>>n[5] 60
>>>n[-3] 80
>>>n[15] Index Error
>>>n[3:8] (40,50,60,70)
o It means accessing the individual element of tuple i.e. from first element to last element.
o Every element in tuple is at different index position i.e. from 0 to size-1.
o Traversal of a tuple is very simple with for loop.
Code: Output:
tpl = (‘I’,’N’,’D’,’I’,’A’) I-N-D-I-A-
for i in tpl :
print(i,end=’-‘)
Code: I
tpl = (‘I’,’N’,’D’,’I’,’A’) N
for i in tpl: D
print(i) I
A
Code: Output:
tpl = (‘I’,’N’,’D’,’I’,’A’) Index 0 and the element at index -5 is I
l=len(lst) Index 1 and the element at index -4 is
for i in range(0,l): N
print(“Index “,i,” and the element at index“, Index 2 and the element at index -3 is
(i-l),”is”, tpl (i)) D
Index 3 and the element at index -2 is I
Index 4 and the element at index -1 is
A
Tuple operators
o There are many operations that can be performed with Tuple which makes it one of the most
used data types in Python.
o Two basic operators + and * are allowed
+ is used for concatenation (joining)
* Is used for replication (repetition)
Syntax:
<Tuple1> + <Tuple2>
Example:
(10,20,30) + (40,50) (10,20,30,40,50)
(‘I’) + (“Love”) + (“India”) (‘I’,“Love”,“India”)
Code: Output:
>>> T1=( 10,20,30,40,50)
>>>T2=(60,70,80,90,100) (10,20,30,40,50,60,70,80,90,100)
>>>T3 = T1 + T2
>>>T3
Note: you can only add tuple with another tuple not with int, float, complex, or string
type.
(10,20,30) + 5 Invalid
Tuple Slicing
o As we know slice means “part of”, so slicing is a process of extracting part of tuple.
o OR Slicing is a part of a tuple containing some continuous character from the tuple.
o The tuple are sliced using a range of indices (Tuple characters have their unique index
position from 0 to length-1 and -1 to –length(backward)).
o Syntax: seq = tuple ( start : stop ) OR seq = tuple ( start : stop : step )
o Example1: Tpl1 = ( 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 )
Tpl1[: ] / Tpl1[ : :] / Tpl1 ( 10, 20, 30, 40, 50, 60, 70, 80, 90 )
Tpl1[ 2 : 5 ] ( 30, 40, 50 )
Tpl1[ 3 : -3 ] ( 40, 50, 60 )
Tpl1[ 0 : 10 : 2 ] ( 10, 30, 50, 70, 90 )
Tpl1[ : : 3 ] ( 10, 40, 70 )
Tpl1[ : : -1 ] ( 90, 80, 70, 60, 50, 40, 30, 20, 10 )
index(substring) :
o This function returns index position of substring.
o If substring not it returns error ‘substring not found’
o OR This function is used to get the index of first matched item from the tuple. It returns
index value of item to search
o Syntax : <Tuple_Object>.index(substring) :
o Example:
Code: Output:
Tpl=(10,20,30,40,50,60,70,80,90,100) 4
print(Tpl. index (50))
Code: Output:
Tpl=(10,20,30,40,50,60,70,80,90,100) ValueError: tuple.index(x): x
print(Tpl. index (200)) not in tuple.
o Note: if we pass any element which is not in the tuple then index function will return an
error.
len(string) :
o This function returns the number of characters in any tuple including spaces.
o Syntax : len( <Tuple_Object> )
o Example:
Code: Output:
Tpl=(10,20,30,40,50,60,70,80,90,100) 10
print(len(Tpl))
Code: Output:
Tpl=(‘I’,’N’,’D’,’I’,’A’) 5
print(len(Tpl))
o Example:
Code: Output:
Tpl = ( 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 )
del Tpl(2) TypeError: 'tuple' object doesn't
print(Tpl) support item deletion.
Error shown because deletion of a
single element is also possible
Tpl = ( 10, 20, 80, 90) Deletes all elements…
del Tpl NameError: name 'Tpl' is not
print(Tpl) defined
Complete tuple has been deleted. Now
error shown on printing of tuple.
count()
o This function returns the count of the item that you passed as an argument. If the given item is
not in the tuple, it returns zero. OR The count() method returns the number of times element
appears in the tuple.
o Syntax : < Tuple_object >.count(element)
Here, element - the element to be counted.
o Example: Tpl=(10, 20, 30, 10, 50, 10, 60, 10, 10)
Code: Output:
5
print(Tpl.count(10))
print(Tpl.count(20)) 1
print(Tpl.count(100)) 0
sorted()
o This function sorts the items of the tuple, by default increasing order. This is done «in place»
i.e. It does not create new tuple.
o Syntax : <Tuple1>.sorted(Tuple2) // By default increasing order (Ascending order)
Code: Output:
T1= (10, 50, 40, 30, 20)
print(T1) (10, 50, 40, 30, 20)
T2.sorted(T1)
print(T2) (10, 20, 30, 40, 50)
min()
o Returns item from the tuple with min value.can be done via list
o Syntax : min(Tuple_Seq)
o Example:
Code: Output:
T=(10, 20, 30, 40, 30, 50, 30) 10
print(min(T))
max()
o Returns item from the tuple with max value.can be done via list.
o Syntax : max(Tuple_Seq)
o Example:
Code: Output:
T=(10, 20, 30, 40, 30, 50, 30) 50
print(max(T))
tuple()
o Converts a list into tuple.
o Syntax : tuple(Seq)
o Example:
Code: Output:
T=[10, 20, 30, 10, 50, 10, 60, 10] (10, 20, 30, 10, 50, 10, 60,10)
T1=tuple(T)
print(T1)
Str=”INDIA” ('I', 'N', 'D', 'I', 'A')
s=tuple(Str)
print(s)
sum()
o sum of tuple elements can be done via list
o Syntax : sum(list(Seq))
o Example:
Code: Output:
T=(10, 20, 30]) Sum= 60
T1= sum(list(T)
print(“Sum =”,T1)