Tuple
In python tuple class is a predefined class used to represent an ordered collection/ sequence of similar or
dissimilar types of objects. In a tuple we can store duplicate elements. To create the object of list class, the
syntax is,
tuple() -> empty tuple
tuple(iterable) -> tuple initialized from iterable's items
Let’s go for an example to understand, how to create the object of tuple class,
obj = tuple ()
print ( obj , type ( obj ))
obj = tuple ( ( 10, 20, 30 ) )
print ( obj , type ( obj ))
obj = ( 10, 20, 30, 40 )
print ( obj , type ( obj ))
obj = 10 , 20 , 30 , 40 , 50
print ( obj , type ( obj ))
obj = 10, 1.2 , 1 +2j , "india"
print ( obj , type ( obj ))
[litindia@localhost demo]$ python [Link]
() <class 'tuple'>
(10, 20, 30) <class 'tuple'>
(10, 20, 30, 40) <class 'tuple'>
(10, 20, 30, 40, 50) <class 'tuple'>
(10, 1.2, (1+2j), 'india') <class 'tuple'>
From the above example we can identify that, if multiple values are given separated by comma ( , ) that
will be treated as tuple object in python.
We have to take special care about single valued tuple. To create a tuple object with one value, the value
should ends with comma (,) otherwise it will be not treated as tuple. Let’s go for an example, how to
create a single valued tuple,
obj = ( 10 )
print ( obj , type ( obj ))
obj = ( 1.2 )
print ( obj , type ( obj ))
obj = ( 10 , )
print ( obj , type ( obj ))
obj = ( 1.2 , )
print ( obj , type ( obj ))
obj = 100 ,
print ( obj , type ( obj ) )
[litindia@localhost demo]$ python [Link]
10 <class 'int'>
1.2 <class 'float'>
(10,) <class 'tuple'>
(1.2,) <class 'tuple'>
(100,) <class 'tuple'>
To get how many number of elements are available in a tuple, len() function we can use.
obj = tuple ()
print ( obj , len ( obj ))
obj = tuple ( ( 10, 20, 30 ) )
print ( obj , len ( obj ))
obj = ( 10, 20, 30, 40 )
print ( obj , len ( obj ))
obj = 10 , 20 , 30 , 40 , 50
print ( obj , len ( obj ))
obj = 10, 1.2 , 1 +2j , "india"
print ( obj , len ( obj ))
[litindia@localhost demo]$ python [Link]
() 0
(10, 20, 30) 3
(10, 20, 30, 40) 4
(10, 20, 30, 40, 50) 5
(10, 1.2, (1+2j), 'india') 4
To perform read operation in a tuple object we can use index. Tuple supports both +ve index and –ve
index. The valid index for tuple a object starts form – len ( TupleObject ) to len ( TupleObject ) – 1.
TupleObject = ( 10, 20, 30 )
print ( TupleObject [ 0 ], TupleObject [ -3 ])
print ( TupleObject [ 1 ], TupleObject [ -2 ])
print ( TupleObject [ 2 ], TupleObject [ -1 ])
[litindia@localhost demo]$ python [Link]
10 10
20 20
30 30
Difference between List and Tuple:
Tuple is exactly same as list except that, list is a mutable object but tuple is an immutable object. The
object which does not support item assignment that object is known as immutable object. Once we
created tuple object, we can not perform any modification/ changes in that object.
Let’s go for an example to understand, what is the difference between mutable object ( list ) and
immutable object ( tuple ),
ListObject = [ 10, 200, 300 ]
print ( 'Before Modification : ' , ListObject )
ListObject [ 0 ] = 100
print ( 'After Modification : ' , ListObject )
TupleObject = ( 10, 200, 300 )
print ( 'Before Modification : ', TupleObject )
TupleObject [ 0 ] = 100 # Error Because tuple object can not be modified
print ( 'After Modification : ', TupleObject )
[litindia@localhost demo]$ python [Link]
Before Modification : [10, 200, 300]
After Modification : [100, 200, 300]
Before Modification : (10, 200, 300)
Traceback (most recent call last):
File "[Link]", line 9, in <module>
TupleObject [ 0 ] = 100 # Error Because tuple object can not be modified
TypeError: 'tuple' object does not support item assignment
From the above program we can identify that, we can modify a list object at any moment, but we can’t
modify a tuple object after creation. If we try to modify a tuple object it will raise TypeError Exception.
Tuple Operators:
We can implement some operators with tuple object. The supported operators with tuple objects are,
1. arithmetic operator [+,*]
2. relational operator [ ==, >=, >, <=, <, != ]
3. membership operator [ in, not in ]
4. slicing operators [ start index : stop index ], [start : stop : step]
Except these operators, we can’t use any other operators with tuple object.
Let’s go for an example how to use operators with tuple object in python,
TupleObject = ( 10 , 20 )
print ( TupleObject + ( 30, 40 )) # concatenation of tuple using + operator
print ( TupleObject * 3 ) # repetition of tuple using * operator
print ( TupleObject < ( 30, 40 )) # use of relation operator on tuple
print ( 100 in TupleObject ) # user of member ship operator with tuple
print ( TupleObject [ 0 : 2: 1]) # use of slicing operator on tuple
print ( TupleObject [ : : -1]) # using slicing to reverse a tuple
[litindia@localhost demo]$ python [Link]
(10, 20, 30, 40)
(10, 20, 10, 20, 10, 20)
True
False
(10, 20)
From the above program we can identify that, the behavior of these operators with tuple object is same as
list object. Arithmetic + operator used for concatenation , * used for repetition. Relational operator in
tuple object, compare element by element to identify True or False.
Tuple Functions:
We can use some function for tuple objects. The important functions what we can use for tuple objects
are,
1. sum
2. max
3. min
Let’s go for an example how to use these function with list object,
TupleObject = ( 5, 1, 3, 2, 4 )
summation = sum (TupleObject )
maximum = max (TupleObject )
minimum = min (TupleObject )
asort = sorted (TupleObject )
dsort = sorted (TupleObject , reverse = True )
print (TupleObject )
print ('The sum of the elements present in Tuple is : ', summation )
print ('The max of the elements present in Tuple is : ', maximum )
print ('The min of the elements present in Tuple is : ', minimum )
print ('Tuple sorting according to ascending order is : ', asort )
print ('Tuple sorting according to descending order is : ',dsort )
(5, 1, 3, 2, 4)
The sum of the elements present in Tuple is : 15
The max of the elements present in Tuple is : 5
The min of the elements present in Tuple is : 1
Tuple sorting according to ascending order is : [1, 2, 3, 4, 5]
Tuple sorting according to descending order is : [5, 4, 3, 2, 1]
From the above program we can identify that how to work with sum, max, min, and sorted function with
tuple object. These functions are the predefined functions in built-ins module.
Sorted function does not returns a tuple object, it returns list object.
Tuple Methods:
We know that, tuple in python is nothing but a pre defined class. In this tuple class some predefined
methods are available which we can access through the object of tuple class.
The methods available in list class are,
Slno method name Return Type Task
1 count integer return number of occurrence of value
2 index integer return first index of value.
Raise ValueError if the value is not present.
Index Method:
We can use index method with tuple object to get the index of a value. Index method returns the first
index of value. Let’s go for an example to understand how to use index method with tuple object,
TupleObject = ( 11, 22, 100, 200, 33, 44, 300, 400 )
print ( TupleObject )
data = int ( input ( 'Enter data to get index : ' ) )
dataIndex = TupleObject . index ( data )
print ( data , 'avilable at index : ', dataIndex)
[litindia@localhost demo]$ python [Link]
(11, 22, 100, 200, 33, 44, 300, 100)
Enter data to get index : 100
100 avilable at index : 2
[litindia@localhost demo]$ python [Link]
(11, 22, 100, 200, 33, 44, 300, 100)
Enter data to get index : 300
300 avilable at index : 6
[litindia@localhost demo]$ python [Link]
(11, 22, 100, 200, 33, 44, 300, 100)
Enter data to get index : 500
Traceback (most recent call last):
File "[Link]", line 7, in <module>
dataIndex = ListObject . index ( data )
ValueError: 500 is not in list
From the above program we can identify that, 100 is present two times in that tuple but index method
only returns index of first occurrence and index method Raises ValueError exception if the value is not
present in tuple object.
While fetching the index of an element in a tuple object, we can provide start and stop parameter in index
method.
[Link](value, [start, [stop]])
Let’s go for an example how to use start and stop parameter with index method,
TupleObject = ( 100, 20, 100 , 20, 100, 30 )
print (TupleObject )
print ( 'In index 0 to 5 first index of value 100 is : ', TupleObject . index ( 100, 0, 5 ))
print ( 'In index 1 to 5 first index of value 100 is : ', TupleObject . index ( 100, 1, 5 ))
print ( 'In index 3 to 5 first index of value 100 is : ', TupleObject . index ( 100, 3, 5 ))
[litindia@localhost demo]$ python [Link]
(100, 20, 100, 20, 100, 30)
In index 0 to 5 first index of value 100 is : 0
In index 1 to 5 first index of value 100 is : 2
In index 3 to 5 first index of value 100 is : 4
Count Method:
We can get the no of occurrence of a value in a tuple using count method. Let’s go for an example to
understand how to use count method with tuple object,
TupleObject = ( 100, 10, 100 , 20, 100, 30 )
print (TupleObject )
print (TupleObject . count ( 100 ), 'times 100 present in Tuple')
print (TupleObject . count ( 10 ), 'times 10 present in Tuple')
print (TupleObject . count ( 1 ), 'times 1 present in Tuple')
[litindia@localhost demo]$ python [Link]
(100, 10, 100, 20, 100, 30)
3 times 100 present in Tuple
1 times 10 present in Tuple
0 times 1 present in Tuple