Tuple data type is exactly same as List data type but it is immutable i.e once you creates Tuple object, you cannot make any changes in that object. Hence Tuple is Read Only version of List. Tuple elements can be represented within parenthesis. If our data is fixed and never changes then we should go for Tuple.
In tuples, insertion Order is preserved, Duplicates are allowed, Heterogeneous objects are allowed. We can preserve insertion order and differentiate duplicate objects by using index. Hence index will play very important role in Tuple also. Tuple support both positive and negative index positive index means forward direction (from left to right) and negative index means backward direction(from right to left). We can represent Tuple elements within Parenthesis and with comma separator. Parenthesis are optional but recommended to use.
t = 5,10,15,20
print(t)
print(type(t))
Output
(5,10,15,20)
<class 'tuple'>
t=()
print(type(t))
Output
<class 'tuple'>
Note : We have to take special care about single valued tuple.compulsary the value should ends with comma,otherwise it is not treated as tuple.
t=(5)
print(t)
print(type(t))
Output
5
<class 'int'>
t=(5,)
print(t)
print(type(t))
Output
(10,)
<class 'tuple'>
Create a Tuple
- create an empty tuple
You can create an empty tuple as follows,
t=()
- Create a Single Value Tuple
While creating of single valued tuple, you should end tuple with comma. parenthesis are optional.
t=(10,) t=10,
- Create a Multi Value Tuple
You can create multi values tuples as follows and like we said earlier parenthesis are optional.
t=10,20,30 t=(10,20,30)
- By using tuple() function
Tuples can be created using tuple functions as follows.
list=[10,20,30] t=tuple(list) print(t) output (10,20,30) t=tuple(range(10,20,2)) print(t) Output (10, 12, 14, 16, 18)
Accessing Items of Tuple
You can access tuple either by indexing or by slice operator.
By using indexing
Tuple support both positive and negative index positive index means forward direction (from left to right) and negative index means backward direction(from right to left). You can access tuple using index values as follows,
t=(10,20,30,40,50,60) print(t[0]) print(t[-1]) print(t[100]) Output 10 60 IndexError: tuple index out of range
By using Slice Operator
You can specify a range of indexes, where to start and where to end the range. When specifying a range, the return value will be a new tuple with the specified items. You can access tuple using range of indexes as follows,
t=(10,20,30,40,50,60) print(t[2:5]) print(t[2:100]) print(t[::2]) Output (30, 40, 50) (30, 40, 50, 60) (10, 30, 50)
Tuple vs immutability
Once you create tuple, you cannot change its content. Hence tuple objects are immutable.
t=(10,20,30,40)
t[1]=70
TypeError: ‘tuple’ object does not support item assignment
Mathematical operators for Tuple
We can apply + and * operators for tuple.
Joining two Tuples
Two tuples can be joined and made an another tuple as follows,
t1=(10,20,30) t2=(40,50,60) t3=t1+t2 print(t3) Output (10,20,30,40,50,60)
Multiply two Tuples
A tuples can be multiplied with any number and hence formed an another tuple as follows,
t1=(10,20,30) t2=t1*3 print(t2) Output (10,20,30,10,20,30,10,20,30)
Important functions of Tuple
1. len() functions
To return number of elements present in the tuple
t=(10,20,30,40) print(len(t)) Output 4
2. count() functions
To return number of occurrences of given element in the tuple
t=(10,20,10,10,20) print(t.count(10)) Output 3
3. index() functions
returns index of first occurrence of the given element. If the specified element is not available then we will get ValueError.
t=(10,20,10,10,20) print(t.index(10)) print(t.index(30)) Output 0 ValueError: tuple.index(x): x not in tuple
4. sorted() functions
To sort elements based on default natural sorting order
t=(40,10,30,20) t1=sorted(t) print(t1) print(t) Output [10, 20, 30, 40] (40, 10, 30, 20)
We can sort according to reverse of default natural sorting order as follows
t1=sorted(t,reverse=True) print(t1) Output [40, 30, 20, 10]
5. min() and max() functions
These functions return min and max values according to default natural sorting order.
t=(40,10,30,20) print(min(t)) print(max(t)) Output 10 40
Read Also : Python Lists
1 thought on “Python Tuple”