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.
In addition to this tutorial. we've also made a video explaining tuples in Python - you can watch it below or just scroll down if you prefer reading.
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.
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.
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.
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].
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.
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.
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.
We can print the two variables separately to check the outcome:
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.
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 Introduction to Python course.