Working with Tuples in Python

Python Tutorials 4 min read
tuples in python
Blog / Python Tutorials / Working with Tuples in Python

Tuples in Python are a type of data sequence, just like lists, with the difference being that they are immutable (you cannot add or delete elements). If you know how to use lists, working with tuples in Python should be a piece of cake. However, if you feel that you have some Python knowledge gaps, you can learn everything about Python is our super-detailed Python Programming Guide.

What are Tuples in Python?

Tuples cannot be changed or modified. The syntax that indicates you have a tuple and not a list is that the tuple’s elements are placed within parentheses, not brackets.

Tuple's elements are placed within parentheses

By the way, a tuple is the default sequence type in Python, so if we enlist three values, as shown in the picture below, the computer will perceive the new variable as a tuple. We could also say the three values will be packed into a tuple.

The three values were packed into a tuple, tuples in python

For the same reason, we can assign a number of values to the same number of variables. On the left side of the equality sign, we add a tuple of variables, and on the right, a tuple of values. That’s why the relevant technical term for this activity is tuple assignment.

Tuple assignment, tuples in python

How to Obtain Elements from a Tuple?

In the same way, we did for lists, we can index values by indicating their position in brackets. We can simply get the first number from the tuple x by writing x[0].

The first number is 40, tuples in python

How to Create a List from Tuples?

In addition, we can also place tuples within lists. And then, each tuple becomes a separate element within the list, as you can see in the picture below.

each tuple becomes a separate element, tuples in python

data science training

What are the Differences between Lists and Tuples in Python?

Tuples are similar to lists, but there are some subtle differences we should not overlook. They can be quite useful when dealing with different comma-separated values. For example, let’s say we have age and years_of_school as variables, and we have the respective numbers in a string format separated by a comma.

age and years of school are 30 and 17, tuples in python

The .split() Method

The .split() method with the proper indication within the parentheses will assign 30 as a value for age and 17 as a value for years_of_school.

Split assigns the numbers, tuples in python

We can print the two variables separately to check the outcome:

Print the variables separately, tuples in python

Functions with Tuples in Python

Lastly, functions can provide tuples as return values. This is useful because a function, which can only return a single value otherwise, can produce a tuple holding multiple values. Let’s create the following function, where A is the area and P is the perimeter:

def square_info(x):

When we only input the length of the side of a square, the output of the square_info() function will be a tuple. It will tell us the area and the perimeter of the square.

the area and the perimeter of the square are 9 and 12

When Do We Need to Use Tuples in Python?

In summary, whenever you have to store a certain number of elements that will not be changed in the future, you can use tuples in Python. Working with them could be extremely useful when a function must return more than one value. If you found this article interesting, you can jump into the unknown concept of storing a set of values in, what is called, a dictionary.


If you’re enthusiastic about boosting your Python knowledge, check out our super practical tutorials!

Earn your Data Science Degree

Expert instructions, unmatched support and a verified certificate upon completion!

Leave a Reply

Your email address will not be published.

Ready to Learn Data Science?
50% OFF