Tuples in Python with Examples
Tuples in Python: A tuple is a collection of elements or objects which is ordered and they are immutable (cannot be changed). It is same as that of lists, but there are some little differences between tuple and lists. Lists are mutable but tuples are immutable.
Lists are represented by square brackets [] whereas tuples are represented using round brackets (). The elements of tuples are separated by comma (,) same as that in lists.
Syntax:
# Syntax-1 tuple1 = ('english','java','python') #Syntax-2 tuple2 = (1,2,3,4) #Syntax-3 tuple3 = "a","b","c"
1). Accessing Tuple Values: To access the values or elements of tuples we can use square brackets [], same as that we used in lists. We will use the concepts of indexing along with square brackets similarly we did in lists.
Example:
#Accessing values in tuple tuple1 = ("Apple","Banana","Mango") tuple2 = (23,34,56) print("Element at index 1 is: ",tuple1[1]) print("Element at index 2 is: ",tuple2[2]) #output #Element at index 1 is: Banana #Element at index 2 is: 56
2). Updating Values in Tuples: As we already discussed that tuples are immutable, which means we cannot change the values or elements of tuples. We can create a tuple by using the other tuples:
Example:
tuple1 = ("Apple","Banana","Mango") tuple2 = (1,2,3)
3). Deleting Tuple Elements: We can delete the complete tuple. But deleting or removing individual elements of a tuple is not allowed. Let’s have an example to understand it better.
Example 1: For deleting complete tuple.
#Deleting Tuple tuple1 = (1,2,3,4,5) print(tuple1) # output # (1,2,3,4,5) # deleting tuple del(tuple1) print(tuple1) # Traceback (most recent call last): # File "test.py", line 9, in <module> # print tuple1; # NameError: name 'tuple1' is not defined
Example 2: For deleting elements of a tuple.
x = ("Learn","Python",23, 24) del(x[2]) # output Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> del(x[2]) TypeError: 'tuple' object doesn't support item deletion
4). Operations on Tuples: The Arithmetic operation for + and * in a tuple is very similar to strings. It means concatenation and repetition here too. The output or result is always a new tuple, not a string.
Expression | Result | Description |
len((3,2,1,0)) | 4 | Length |
(3,2,4) + (3,5,7,8) | (3, 2, 4, 3, 5, 7,8) | Concatenation |
(‘Hello!’,) * 2 | (‘Hello!’, ‘Hello!’) | Repetition |
5 in (1, 2, 3) | False | Membership |
for x in (4, 2, 1): print x, | 4,2,1 | Iteration |
5). Slicing, Matrices and Indexing Operations on Tuples:
Tuples support some other operations also that we used to perform on lists. These operations include Indexing, Slicing and Matrices operations.
Example 1:
Let’s suppose we have a tuple:
tuple1 = ("India","Australia","Germany")
The below table shows the operations we can perform on a tuple:
Expression | Results | Description |
tuple1[1] | ‘Australia’ | Offsets start at zero |
tuple1[1:] | [‘Australia’, ‘Germany’] | Slicing fetches sections |
tuple1[-1] | ‘Germany’ | Negative: count from the right |
6). Some Built-in Tuple functions:
Python comes with some built-in useful tuple functions that reduce the programming efforts for programmers and increases the scope of tuples:
Sr no. | Function | Description |
1 | max(tuple) | It returns item from the tuple with max value. |
2 | tuple(seq) | It converts a list into tuple. |
3 | cmp(tuple1, tuple2) | Compares elements of both tuples. |
4 | min(tuple) | Returns item from the tuple with min value. |
5 | len(tuple) | It returns the total length of the tuple. |