ProGrad Programs
About Us



Tuples in Python | Python Tuples with Examples

Published on 12 Mar 2020

Tuples in Python work similar to lists in Python, they are also used to store a sequence of elements/objects. But the major difference between a tuple and a list is that, a tuple is immutable. This means, once a tuple is created, the values inside the tuple cannot be modified or changed, unlike that of a List.

Let's now understand Tuples better by having a look at various Tuple operations.

How to Create a Tuple in Python

a) Creating a Tuple

A tuple can be created by placing all the comma-separated elements within round brackets ( ). These elements can be of different data types. Also, any set of unenclosed elements separated by commas, are treated as tuples by default.

tuples in python

Note: If a tuple consists of only one element, then a comma after the first element is mandatory. If a comma is not used, then the compiler considers that element as an integer instead of a tuple.

#creating an empty tuple
tuple1 = ()

#Creating a tuple with a single element
tuple1 = (20.34,)

#Creating a tuple with multiple elements
tuple1 = 20.34, 2, "Python"

#Creating a tuple with multiple elements
tuple1 = (20.34, 2, "Python")

(20.34, 2, 'Python')
(20.34, 2, 'Python')

Alternate Method: There is an alternate way to create tuples using the tuple () method in Python. The syntax for this method is tuple ((Elements separated by commas)).

#creating tuple using tuple ()
a = tuple ((38, 4.9, "FACE Prep"))
print (a)

(38, 4.9, 'FACE Prep')

b) Creating a Nested Tuple

A nested tuple is one where one of the elements of the tuple is, in turn, a tuple. A nested tuple can be created this way,

#creating a nested tuple
tuple1 = (10, (20, 30))

# creating a nested tuple with two individual tuples
tuple1 = (10, 20)
tuple2 = ("Python")
tuple3 = (tuple1, tuple2)

(10, (20, 30))
((10, 20), 'Python')

How to Access Elements in a Tuple

a) Accessing Single Element

An element in a tuple can be accessed using the index operator [ ]. Similar to a list, Tuples also supports both positive and indexing of elements. The positive index values start from 0 (used to access elements from the start of the tuple) & negative index values start from -1 (used to access elements from the end of the tuple). However, the index value used to access an element should only be an integer (be it +ve or -ve) and should be within the range.

In a similar fashion,Nested tuples can be accessed using the nested index value. For example,

#accessing an element of a tuple
tuple1 = ("Python", 10, 20.3)

tuple1 = (30, "Python", 30.34)

# accessing elements in a nested tuple using nested index values
tuple1 = (("Welcome", 10, 20.3), (20, "Hai"))


b) Accessing Multiple Elements

A range of elements in a tuple can be accessed using the slicing operator : and using the index values of starting, ending of the range to be sliced. For instance,

#accessing a range of elements
tuple1 = (30, "Python", 30.34, 60)

#accessing the entire tuple
tuple1 = (30, "Python", 30.34, 40, 20.24)
print(tuple1[1:]) # prints all the elements starting from the index value 1

('Python', 30.34)
('Python', 30.34, 40, 20.24)

Explanation: Here, the start index value 1 is inclusive and the end index value 3 is exclusive.

How to Change/Modify a Tuple

As we already know, an element in a tuple is unchangeable. So if we try to change the element of a tuple, the compiler will throw an error.('Python', 30.34, 40, 20.24)

#modifying an element in the tuple
tuple1 = (30, "Python", 30.34, 40, 20.24)
tuple1[2] = "Hai"

Traceback (most recent call last):
 File "/home/6ac30ed1ee84233b86d7f1c44b1a0f34.py", line 2, in <module>
 tuple1[2] = "Hai"
TypeError: 'tuple' object does not support item assignment

But then, if any of the elements of a tuple are of mutable data types, then those elements can be modified or changed. For example, we know that the list is a mutable data type. Now, let's say one of the elements of a tuple is a list, let's see what happens in this case.

#modifying an element of a tuple
tuple1 = (30, "Python", [30.34, 40, 20.24])
tuple1[2][0] = "Hai"

(30, 'Python', ['Hai', 40, 20.24])

How to Add Elements to a Tuple

Since tuples are immutable, adding elements to them is not possible.

How to Delete Elements of a Tuple

Like already said, elements of a tuple cannot be modified. This implies that they cannot be deleted too. However, you can delete the entire tuple using the del keyword as shown below.

#deleting a tuple
tuple1 = (30, "Python", [30.34, 40, 20.24])
del tuple1

Other Important Tuple Operations

a) Reassigning a Tuple

Python Tuples cannot be modified, but the entire Tuple can be reassigned with different elements as shown below.

#reassigning a tuple
tuple1 = (30, "Python", 30.34, 40, 20.24)
tuple1 = (20, 1.45, "FACE Prep")

(20, 1.45, "FACE Prep")

b) Concatenating Tuples

We know that the operator + is used for adding two values. Similarly, the same operator is used for joining elements of two tuples as shown below.

#adding tuples
tuple1 = (30, "Python", 30.34, 40, 20.24)
tuple2 = (20, 1.45, "FACE Prep")
print(tuple1 + tuple2)

(30, 'Python', 30.34, 40, 20.24, 20, 1.45, 'FACE Prep')

c) Tuple Repetition

The elements of a tuple can be repeatedly added to the tuple using * operator. For example,

#repeating elements oftuple
tuple1 = (30, "Python", 30.34, 40, 20.24)
print (tuple1 * 2)

(30, 'Python', 30.34, 40, 20.24, 30, 'Python', 30.34, 40, 20.24)

d) Tuple Membership Test

We can check if an element exists in a tuple or not, using the keywordin and not in

#adding tuples
tuple1 = (30, "Python", 30.34, 40, 20.24)
tuple2 = (20, 1.45, "FACE Prep")
print (30 in tuple1)
print ('faceprep' in tuple2)

e) Tuple Methods

Some useful Tuple methods available in Python are:

count(x)Returns the number of times element x is present in the tuple
Returns the index of the first element whose value is x

If you have any feedback about this article and want to improve this, please write to enquiry@faceprep.in
Explore 'python'