
A python tuple is the optimal solution for keeping a collection of objects where the values are arranged in a certain sequence. A tuple is immutable in nature. We use tuples to hold heterogeneous (different) types of data.
The certain sequence means that the elements are arranged in a same order as we defined during creation which will not change.
For the tuple we use parenthesis or round brackets ( and ) and we separate the objects by using comma (,).
The important point to remember is that we can add duplicate values to a tuple, and the items of tuples are indexes, which means we can use the index to retrieve the tuple’s items. The first item’s index is [0], the second item’s index is [1], and so on.
Table of Contents
Python tuple example
Here is a basic example of a python tuple. We will go through the tuple operations like create, access, delete in detail one by one.
tpl = ("Virat", "Sachin", "AB") print(tpl)
Creating a Tuple in Python
How to create a tuple in python? We can create a tuple with and without parenthesis. The use of parentheses is optional in python. You must use a comma to separate all of the items (,).
tp_empty = ()
tp1 = (10, "Smith", 20)
tp2 = ("India", "US", "SA")
tp3 = 100, 200, 300, 400
print(tp_empty)
print(tp1)
print(tp2)
print(tp3)
Output: () (10, 'Smith', 20) ('India', 'US', 'SA') (100, 200, 300, 400)
Access tuple values in Python
If you want to access the tuple’s values, use square brackets. If you want to access several values from the tuple, you can use semicolons(:).
Here’s an example that will show you how to get elements and values from a tuple.
tp1 = ('India', 'UK', 'US', 10, 20);
tp2 = (10, 20, 30, 40, 50, 60, 70);
print ("Value of tp1[0]: ", tp1[0]);
print ("Value of tp2[1:4]: ", tp2[1:4]);
print ("Value of tp2[2:4]: ", tp2[3:5]);
Output: Value of tp1[0]: India Value of tp2[1:4]: (20, 30, 40) Value of tp2[3:5]: (40, 50)
Slicing in Tuples
tuple1 = (10 ,11, 12, 13)
print(tuple1[0:])
print(tuple1[1:])
print(tuple1[::-2])
print(tuple1[3:4])
Output: (10, 11, 12, 13) (11, 12, 13) (13, 11) (13,)
Update Values of Tuple
A tuple, as previously stated, is immutable in nature. We can not update the tuple’s values. However, we can create a new tuple by taking the values of existing tuple.
Let’s look at an example to help you understand the concept.
tp1 = (10, 20, 30, 40, 10.32, 20.22);
tp2 = ("Apple", "Orange", "Banana");
tp3 = tp1 + tp2;
print (tp3);
Output: (10, 20, 30, 40, 10.32, 20.22, 'Apple', 'Orange', 'Banana')
tp1 = (10, 20, 30, 40, 10.32, 20.22); # Following action invalid for tuples # tp1[0] = 10;
Delete values of Tuple
We can’t remove values from a tuple since it’s immutable but we can remove the tuple completely. We use the del keyword with the tuple name to remove a tuple.
tp1 = (10, 20, 30, 40, "Apple", "Orange");
del tp1[1];
print (tp1);
Output: Traceback (most recent call last): File "\geekcer\python\4.py", line 2, in <module> del tp1[1]; TypeError: 'tuple' object doesn't support item deletion
tp1 = (10, 20, 30, 40, "Apple", "Orange");
print (tp1);
del tp1;
print (tp1);
Output: (10, 20, 30, 40, 'Apple', 'Orange') Traceback (most recent call last): File "E:\PythonExercise\30042022\4.py", line 4, in <module> print (tp1); NameError: name 'tp1' is not defined
In the above output you can see the statement “NameError: name ‘tp1’ is not defined“, it means that the tuple has been deleted successfully.
Nesting of Tuple in Python
Here example code for creating nested tuples where two tuple are being taken for nesting.
tp1 = (10, 20, 30, 40);
tp2 = ("Apple", "Orange", "Banana");
tp3 = (tp1, tp2)
print (tp3);
Output: ((10, 20, 30, 40), ('Apple', 'Orange', 'Banana'))
Basic Operations in Tuple in Python
For tuple you can apply many operations like + for concatenation, * for iteration, in for membership etc. One thing to note is that the result will be the new tuple.
Repeating tuples in Python
When we need to repeat a tuple multiple times, we use the repetition operator (*).
<tuple-name> * N
tp1 = (10 ,11, 12)
print(tp1 * 3)
Output: (10, 11, 12, 10, 11, 12, 10, 11, 12)
Concatenation of Tuples in Python
We use the + operator to concatenate tuples in Python.
tp1 = (10 ,11, 12)
tp2 = ("A", "B", "C")
tp3 = tp1 + tp2;
print(tp3)
Output: (10, 11, 12, 'A', 'B', 'C')
Iteration of Tuple in Python
We iterate over the tuple elements using a for loop. Here we have an example of iteration of tuple elements.
tp1 = (10 ,11, 12, 13, 14)
for itr in tp1:
print(itr)
Output: 10 11 12 13 14
Find the length of tuple in python
The len() function returns the length of the tuple. We use the len() function returns the number of elements from a tuple.
tp1 = (10 ,11, 12, 13, 14, "A", "B")
l = len(tp1);
print("Length is", l)
Output: Length is 7
Read Also : Basic Operation of Python List