Tuple in python is very similar to lists, however, unlike lists, tuples are immutable means they can not be changed. You would use a tuple to present things that should not be changed. Tuples are a data structure that stores an ordered sequence of values. Tuples are defined with parenthesis.
Constructing or Creating Tuple in Python The construction of a tuple use() with elements separated by commas.
Please read the previous topic for more understanding about Control Statements
Boolean in Python
Python Operators
Control Statements in Python
List in Python
Contents
Constructing / Create Tuple in Python
The construction of tuples uses () with elements separated by commas.
# Empty Tuples
t1 = ()
print(t1)
# Tuple with integers element
t2 = (1, 20, 30, 40)
print(t2)
# Tuple with string element
t3 = ('Hello', 'World')
print(t3)
# Tuple with mixed element
t4 = (10, 20, 'Hello', 'World')
print(t4)
#Nested Tuples
T = (t2, t3)
print(T)
O/P –
() (1, 20, 30, 40) ('Hello', 'World') (10, 20, 'Hello', 'World') ((1, 20, 30, 40), ('Hello', 'World'))
Check type in Tuple
# Tuple with more than one element
t1 = (10, 20)
print(type(t1))
# Tuple with single element
t2 = (10)
print(type(t2))
# the above declaration will consider as integer but you need to follow the below code to create single element tuple in python
t3 = (10, )
print(type(t3))
O/P –
<class 'tuple'> <class 'int'> <class 'tuple'>
Convert to Tuple
The tuple method is used to convert into a tuple. This method accepts other type values as an argument and returns a tuple type value.
Lst = [10, 20, 30]
T = tuple(Lst)
print(T)
O/P-
(10, 20, 30)
print(type(T))
O/P –
<class 'tuple'>
Mixed Element with complex object
L1 = [90, 80, 70]
T1 = (10, 20, 30)
T = (T1, L1)
print(T)
O/P-
((10, 20, 30), [90, 80, 70])
Repetition in Tuples
You can repeat the elements in a tuple for a given number of times using the * operator.
t = ("Hello ", )
print(t * 4)
O/P-
('Hello ', 'Hello ', 'Hello ', 'Hello ')
Access Tuple Elements
There are various ways in which we can access the elements of a tuple like List in Python.
- Indexing (Positive and Negative Indexing)
- Slicing
Indexing – Positive
- We can use the index operator by using the square/[] bracket. The index value will start from zero like the list.
- If tuple has 4 elements then the index will start from 0 to 3. If someone access outside this index then it will throw the IndexError.
- The index must be an integer, so we cannot use float or other types. This will result in TypeError.
T = (10, 20, 30, 40)
print(T[0])
print(T[3])
print(T[1])
O/P-
10 40 20
# Try to access outside index in Tuple
T[5]
O/P –
Access element of nested Tuple in Python
T = (('Hello', 'World'), (10, 20, 30, 40))
print(T[0][1])
print(T[1][3])
O/P –
World 40
Similarly, we will get IndexError outside the index value.
print(T[0][2])
O/P –
Indexing – Negative
- Python allows negative indexing for its sequences.
- The index of -1 refers to the last item.
T = (10, 20, 30, 40)
print(T[-1])
print(T[-3])
O/P –
40 20
T = (('Hello', 'World'), (10, 20, 30, 40))
print(T[-2][-2])
O/P –
Hello
# Access outside index Tuple in Python
T[-3][-2]
O/P –
Slicing – Tuple in Python
We can access a range of items in a tuple by using the slicing operator colon :.
T = (10, 20, 30, 40)
#Positive index Slicing
print(T[0:2])
print(T[0:3:2])
#Negative index Slicing
print(T[-1:-5:-1])
O/P –
(10, 20) (10, 30) (40, 30, 20, 10)
Changing a Tuple in Python
It can’t be stressed enough that tuples are immutable. This means we can not change the value in an element like List But, if the element is itself a mutable data type as a list, its nested items can be changed.
T = (10, 20, 30, 40)
t[2] = 200
O/P –
# Created a new Tuple in Python
T = (10, 20, 30, 40)
print(T)
# Reassigned value into Tuple
T = ("Welcome", "To", "InfoSyntax")
print(T)
O/P –
(10, 20, 30, 40) ('Welcome', 'To', 'InfoSyntax')
L1 = [90, 80, 70]
T1 = (10, 20, 30)
T = (T1, L1)
print(T)
O/P –
((10, 20, 30), [90, 80, 70])
We can modify the element from a complex tuple if the complex object has that capability.
Please see the below example
We have one tuple and it has two elements as the first one is the tuple and the second element is a list. The second element is a List. The list has the capability to change its element value in python so it’s possible to change the value in the list through tuple.
T[1][2] = 200
print(T)
O/P-
((10, 20, 30), [90, 80, 200])
Deleting a Tuple in Python
As discussed above, we cannot change the elements in a tuple but delete a tuple completely by using the del keyword.
T = (10, 20, 30, 40)
# Try to delete element in Tuple.
del T[2]
O/P –
# Deleting tuple entirely
del T
If you try to access the same tuple then we will get an error.
print(T)
O/P –
Methods in Tuple
Count Method
The count() method returns the number of times a specified value appears in the tuple.
Get the frequency of a particular element in a tuple.
T = (10, 40, 30, 90, 30, 120)
print(T.count(30))
O/P –
2
print(T.count(100))
O/P –
0
Index in Tuple
It returns the index of the first element.
T = (10, 40, 30, 90, 30, 120)
# available item in tuple
print(T.index(30))
O/P –
2
Item does not exist in tuple then the index method will raise the ValueError.
print(T.index(100))
O/P –
Length method
Len() is used to find out the length of the tuple in python.
T = (10, 40, 30, 90, 30, 120)
print(len(T))
O/P –
6
Sort in Tuple
It takes elements in the tuple and returns a new sorted list. It does not sort the tuple itself.
T = (89, 12, 76, 43, 0, 23, 78)
ST = sorted(T)
print(T)
print(ST)
print(type(T))
print(type(ST))
O/P –
(89, 12, 76, 43, 0, 23, 78) [0, 12, 23, 43, 76, 78, 89] <class 'tuple'> <class 'list'>
Sum Method
Sum() is used to the addition of all the elements.
T = (89, 12, 76, 43, 0, 23, 78)
print(sum(T))
O/P –
321
Similarly, we are using max and min methods for getting maximum and minimum values from tuples in python.
O/P –
print(max(T))
print(min(T))
O/P-
89 0