Updating Tuples in Python

PythonServer Side ProgrammingProgramming

Tuples are immutable which means you cannot update or change the values of tuple elements. You are able to take portions of existing tuples to create new tuples as the following example demonstrates −

Example

 Live Demo

#!/usr/bin/python
tup1 = (12, 34.56);
tup2 = ('abc', 'xyz');
# Following action is not valid for tuples
# tup1[0] = 100;
# So let's create a new tuple as follows
tup3 = tup1 + tup2;
print tup3;

Output

When the above code is executed, it produces the following result −

(12, 34.56, 'abc', 'xyz')
raja
Published on 28-Jan-2020 12:40:59
Advertisements