Python Tuple

A Python tuple is a collection of objects which is ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed ie immutable and the list is mutable, uses parentheses, whereas lists use square brackets.

Python Tuples

Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these comma-separated values between parentheses also. For example −

tup1=('rakesh','umesh','suresh')
tup2 = (10,20,30,40)
tup3=(10.34,'rakesh',40,60)
tup4= "a","b","c","d"

Empty tuple is created using only two circular brackets containing nothing-

tup1=()

To create a tuple containing only a single value, you have to include a command, even though it is containing only a single value –

tup1 = (10,)

Like list and string, tuple indexes also start from 0 and can be accessed using these indexes, tuples can also be sliced like string and list.

Accessing values in Python Tuple

To access a value of tuple, use the square bracket for slicing with the indexes or index value to access the individual value.

tup1= ('rakesh',10,10.45,'python',2000)
tup2 =(10,20,30,40,50,60,70)
print(tup1[3])              
print(tup2[2:6])            

When the above code is executed, it produces the following results

tup1[3]       : Python
tup2[2:6]     : 30 40 50 60

Updating Python Tuple

Python tuples are immutable ie you can not change the contents of a tuple during the execution of the program but you can slide a tuple or reassign a tuple during the runtime.

tup1 = (10,20,30,40)
tup2 = ('python', 'is', 'fun')

tup1[2]=100 
# it will produce an error as you are trying to update the contents of the tuple 

tup1 = tup1[1:4] 
# this is allowed as you are re-assigning the same tuple

tup3 = tup1+ tup2

Delete Tuple elements

being an immutable data type Python tuples do not allow us to delete individual element from its collection, However, you can delete the whole tuple collection using the del command

>>> tup1 = (1,2,3,4,5,6,7,8)
>>> del tup1[3]
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in 
    del tup1[3]
TypeError: 'tuple' object doesn't support item deletion
>>> del tup1
>>> type(tup1)
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in 
    type(tup1)
NameError: name 'tup1' is not defined

Tuple Functions and Methods

for all the following functions and methods we are going to use the following tuple

tup1=(1,2,3,4,5,6,7,8,9)
FunctionDescription
len( ) Len ( ) function find out the total number of elements in that tuple

n = len(tup1)

print(n)

# output: 9

sum ( )sum( ) function find out the sum of tuple elements

result = sum(tup1)

#result : 45

max( )Max( ) function find out the largest element in that tuple

result = max(tup1)

#result  : 9

min ( )min( ) function find out the lowest number in that tuple

result = min(tup1)

#result : 1

tuple( )This function converts a list into a tuple

Basic Tuple operations

Tuple response to + and * is exactly the same as a string and list ie + means concatenation and * mean repetition of the object. The only difference is – the result is a tuple not a string.

tup1 =('Hello',)
print(tup1*4)

output : HelloHelloHelloHello

tup2= (1,2,3,4,5)
tup3 = tup1+tup2
print (tup3)

output : 1,2,3,4,5,'Hello'

if 2 in tup2:
   print('found')

output : found


for x in tup2: 
    print(x)

1
2
3
4
5

from the above example, you can see tuple respond exactly the same way as a Python string and both are immutable.

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.