Tuple in Python PDF
Tuple in Python PDF
▪ Tuple data type is exactly same as list data type except that it is
immutable, we cannot change values.
▪ It is fixed-length and immutable.
▪ The values in the tuple cannot be changed nor the values be added
to or removed from the tuple
▪ Tuple elements can be represented within parenthesis.
▪ Tuple Can be made using the parentheses.
▪ Lists are enclosed in brackets [ ] and their elements and size can be
changed, while tuples are enclosed in parentheses ( ) and cannot be
updated. Tuples are immutable.
Difference Between List and Tuple
List Tuple
List is used for holding multiple Tuple is also used for holding
element. multiple element.
heterogeneous objects are allowed heterogeneous objects also are
in List allowed in Tuple.
my_tuple=10,20,30
print(my_tuple)#The Output (10, 20, 30)
Unpacking Tuples
▪ To unpack values from a tuple and do multiple
assignments use
# tuple unpacking is also possible
a,b,c=(10,20,30)
print(a)#The Output 10
print(b)#The Output 20
print(c)#The Output 30
Note:-
▪ The symbol _ can be used as a disposable variable name if one only
needs some elements of a tuple, acting as a placeholder.
a = 1, 2, 3, 4
_, x, y, _ = a
print(_)# The output 4
print(x)# The output 2
print(y)# The output 3
print(_)# The output 4
my_tuple=(10)
print(my_tuple)#The Output 10
print(type(my_tuple))#The Output <class 'int'>
my_tuple=('hello')
print(my_tuple)#The Output hello
print(type(my_tuple))#The Output <class 'str'>
Note:-
▪ A single value in parentheses is not a tuple
▪ Keeping only one value is not enough within the tuple We will
need a trailing comma to indicate tuple.
▪ you have to include a final comma
my_tuple=(10,)
print(my_tuple)#The Output (10,)
print(type(my_tuple))#The Output <class 'tuple'>
my_tuple=('hello',)
print(my_tuple)#The Output ('hello',)
print(type(my_tuple))#The Output <class 'tuple'>
y=1,
print(y)#The Output (1,)
print(type(y)) #The Output <class 'tuple'>
▪ IndexError:
Index out of range. This error occurs when we mention the index
which is not in the range. For example, if a tuple has 5 elements
and we try to access the 7th element then this error would occurr.
nums=(10,20,30,40,50)
print(nums[-1])#The Output 50
print(nums[-2])#The Output 40
print(nums[-3])#The Output 30
print(nums[-4])#The Output 20
print(nums[-5])#The Output 10
▪ You can also get the position of each item at the same time Using
enumerate function:
nums=(10,20,30,40,50)
for (index, item) in enumerate(nums):
print('The item in position {} is: {}'.format(index, item))
Output:
The item in position 0 is: 1
The item in position 1 is: 2
The item in position 2 is: 3
The item in position 3 is: 4
The item in position 4 is: 5
The item in position 5 is: 6
The * Operator
▪ The * operator creates multiple copies of a string.
▪ Replication – multiplying an existing tuple by an integer will produce a
larger tuple consisting of that many copies of the original.
tuple1=(10,20,30)
print(tuple1*3)
tuple2=('a','b','c')
print(tuple2*3)
Output:
(10, 20, 30, 10, 20, 30, 10, 20, 30)
('a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c')
The in Operator
Checking Membership(Tuple Contains):
▪ By using in operator, we can check whether an item is in a tuple or not.
tuple1=(10,20,30)
print(10 in tuple1)# The Output True
print(10 not in tuple1)# The Output False
print(40 not in tuple1)# The Output True
tuple2=('a','b','c')
print('a' in tuple2)# The Output True
print('a' not in tuple2)# The Output False
print('d' not in tuple2)# The Output True
tuple1=(10,20,30,40,50)
tuple1[0]=60
print(tuple1)
tuple1[0] =60
TypeError: 'tuple' object does not support item assignment
Important Ponts:
tuple2=tuple1.append(60)
AttributeError: 'tuple' object has no attribute 'append'
tuple1=(10,20,30)
tuple2=(40,50,60)
tuple3=tuple1.extend(tuple2)
print(tuple3)
tuple3=tuple1.extend(tuple2)
AttributeError: 'tuple' object has no attribute 'extend'
my_tuple = (10,20,30,40,50)
del my_tuple[3]
print(my_tuple)
del my_tuple[3]
TypeError: 'tuple' object doesn't support item deletion
my_tuple = (10,20,30,40,50)
# Can delete an entire tuple
del my_tuple
# NameError: name 'my_tuple' is not defined
print(my_tuple)
print(my_tuple)
NameError: name 'my_tuple' is not defined
Tuple Method
tuple1=([10,20],[30,40],[10,20],[50,60])
list1=eval(input('enter list for count'))
c=tuple1.count(list1)
print(list1,' occur' ,c,'times')
Tuple Length
▪ The function len returns the total length of the tuple
tuple1= (10,20,30,40,50)
print('Length of Tuple=',len(tuple1))#The Output Length of Tuple= 5
Max of a tuple
▪ The function max returns item from the tuple with the max value
tuple1= (10,20,30,40,50)
print ('Max of Tuple= ‘, max(tuple1)) #Max of Tuple= 50
Min of a tuple
▪ The function min returns the item from the tuple with the min
value.
tuple1= (10,20,30,40,50)
print ('Min of Tuple= ‘, min(tuple1)) #Min of Tuple= 50
Convert a list into tuple
▪ The built-in function tuple converts a list into a tuple.
list1=[10,20,30,40,50]
tuple1=tuple(list1)
print(list1)
print(tuple1)
#The Output
[10, 20, 30, 40, 50]
(10, 20, 30, 40, 50)
tuple[begin:end:step]
▪ step value can be either +ve or -ve.
▪ if +ve then it should be forward direction (Left to Right).
▪ If -ve then it should be backward direction (Right to Left).
▪ If +ve forward direction from begin to end-1.
▪ if -ve backward direction from begin to end+1.
❖ If you omit the first index, the slice starts at the beginning of the
tuple. Thus, tuple[:m] and tuple[0:m] are equivalent:
tuple[:endindex]
tuple1=(10,20,30,40,50,60,70)
print(tuple1[:2])#The OutPut (10, 20)
print(tuple1[:3]);#The OutPut (10, 20, 30)
❖ if you omit the second index as in s[n:], the slice extends from the
first index through the end of the list. This is a nice, concise
alternative to the more cumbersome s[n:len(s)]:
tuple[beginindex:]
tuple1=(10,20,30,40,50,60,70)
print(tuple1[5:])#The OutPut (60, 70)
print(tuple1[6:]);#The OutPut (70,)
▪ If we want to append the value at the end of the tuple, then first we
have to make copy of existing tuple and then add new element to it
using + operator.
▪ And then we will put the address of the newly created tuple in
the original reference
▪ hence it will give an effect that new element is added to existing
tuple.
print(my_tuple)
Program
my_tuple=(10,20,30,40)
print(my_tuple)#The Output (10, 20, 30, 40)
#Appending 50 at the end of tuple
my_tuple=my_tuple+(50,)
print(my_tuple)#The Output (10, 20, 30, 40, 50)
# Create a tuple
my_tuple = (10,20,30,50)
n=3
# Insert 40 in tuple at index 2
my_tuple = my_tuple[ : n ] + (40 ,) + my_tuple[n : ]
# Create a tuple
my_tuple = (10,20,30,40,50)
n=3
# Insert 40 in tuple at index 2
my_tuple = my_tuple[ : n ] + (40 ,) + my_tuple[n+1 : ]
# Create a tuple
my_tuple = (10,20,30,50)
n=3
# Insert 40 in tuple at index 2
my_tuple = my_tuple[ : n ] + my_tuple[n+1 : ]