Python Tuple Data Type

Python Tuple Data Type


Python tuple data type is similar to the Python list in various ways. Like lists, you can use tuples to store different types of data. The only thing that differs is that tuples are immutable that means once a tuple is created you can’t update data inside it.

The items or elements of a tuple are generally enclosed within the parentheses but it is not always necessary.

In this article, you will learn about Python tuples.

Examples of Python tuples

The following are some of the examples of tuples –

# Example of empty tuple
# Tuple contains only numbers
# Given tuple contains string elements
days=('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')
# Given tuple has mixed elements
mixed_tuple = (1, 2, 'Hi', 3,'abc')

Creating tuple in Python

A tuple is created when you assign elements to a variable. Using parentheses is optional but this is a good practice to use them.

Creating empty tuple

my_tuple = ()

Creating tuple without parentheses

my_tuple = 1, 2, 'Hi'

Creating tuple with parentheses

my_tuple = (1, 2, 3, 'Hi')

Creating a tuple with a single element

While creating a tuple that has a single element always use a comma otherwise it will not be considered as tuple type.

my_tuple= ("Hi",)

Now when you execute this program you will see –

Tuple unpacking

You can unpack or extract the elements of a tuple into variables. Remember always to use variables equal to the number of tuple elements otherwise, you will be prompted with an error.

For example –


Here 5, 8, 9 will be assigned to variables a, b, c respectively.

Looping in tuple

Using in operator with for loop you can loop in a tuple. By using len() function you can find the length of a tuple then you can use a loop that iterates from the index 0 to the length-1.

x=(1, 2, 3, 4, 5) 
for i in x:

OR use while loop, as given below, to iterate through the tuple –

x=(1, 2, 3, 4, 5)
while i < len(x):

Accessing tuple elements

You can access the tuple element using positive or negative indexing.

For example –


To print the element at index 1 we will use –


OR print the element at index -1 with the following statement –


If you want to access a range of items of a tuple then use slicing. For example, to print elements from index 1 to 3 use –


Here, the tuple element at index 4 will be excluded. When you run this program you will see the following output –

Tuple methods

Python tuples are immutable so there is no method to add or remove elements in the tuple. It provides the following two methods –

Using count() method –

You can use this method to know how many times an element occurs in a tuple. For example –


You can see the output in the given image –

Using index() method –

The index method returns the index of an element if an element occurs multiple times it returns the index of element which occurs first.

In the above example you can check the index of an element for example you can check 5 or 7 as given in the image –


Since tuples are immutable, operations such as iteration through a tuple are faster than with a list. So you get a performance boost.

Now if you have a query on the given topic then write us in the comments below.


Previous Python List Data Type
Next Python Dictionary Data Type

About author

You might also like

Programming 0 Comments

Using Python pass keyword

The Python pass keyword which is a statement in itself basically does nothing. It can be used as a placeholder for future code. As empty code is not allowed in

Python Programming 0 Comments

Python Boolean Data Type

Boolean or bool is a built-in data type in the python programming language. It can have two possible values –   True  False These are equivalent to 1 and 0

Programming 0 Comments

Python Frozensets

Python frozensets are same as the set data type in Python the only difference is that frozensets are immutable which means you can’t add or remove the elements once it


No Comments Yet!

You can be first to comment this post!

Leave a Reply