Python Tuples: Syntax, Immutability, and Tuple Unpacking
Last updated on
What is a Python Tuple?

Tuples is a built-in data structure that used used to store multiple items, e.g 10.0, 20.0, in a single variable e.g coordinates.
You define a tuple by separating the items with a trailling comma, and then adding optional paranthesis around them.
coordinates = (10.0, 20.0) # Correct
# or
coordinates = 10.0, 20.0 # Correct
Single element tuples require a trailling comma, because otherwise it will be considered an int or any other value type within paranthesis.
It’s the trailling comma that tells the tuples not the paranthesis!
single = (42,)
Characteristics:
Tuples in Python have the following characteristics:
Tuples Maintain items Order: as you can see from the image above, the items of a tuple are ordered by indices starting from 0.
In contrast with lists, tuples are immutable, which means that once a tuple is created, you can’t neither add, update, nor remove elements from it.
Basically once created, it acts as a constant that holds multiples values.
For example if you try to update the first coordinate of the tuple located at index 0 by assigning a new value 5.0 into it, a TypeError will gets raised.
coordinates = (10.0, 20.0)
coordinates[0] = 5.0 # TypeError: 'tuple' object does not support item assignment
The fact that tuples are immutable makes it possible for Python to optimize them, so in practice they are way faster than lists.
Tuples Allow Duplicates: Tuples allow duplicates or identical items, for example we can normally create a tuple of coordinates with two identical values.
coordinates = (10.0, 10.0) # Correct, no error gets raised
Operations on Tuples: Only Read
As tuples are immutable the only allowed CRUD operation is the R or reading.
The same indexing syntax used with lists can be used with tuples.
coordinates = (10.0, 20.0)
print("First coordinate: ",coordinates[0])
print("Last coordinate: ",coordinates[-1])
Slicing works perfectly on tuples, we covered slicing in details in the previous article of the series , you may consider checking it out if you haven’t read it after this one!
Tuple Unpacking
We saw that it’s possible to read the individual items of a variable using the indexing syntax.
coordinates = (10.0, 20.0)
print("First coordinate: ",coordinates[0])
print("Last coordinate: ",coordinates[-1])
In addition to that, Python makes it possible to unpack, or capture the values of the tuple in order into individual variables.
For example, let’s capture the first coordinate coordinates[0], and second one coordinates[1] into two variables x and y.
coordinates = (10.0, 20.0)
x, y = coordinates
print(x) # 10.0
print(y) # 20.0
Many Python features such as swapping two variables without the need of temporary one are made possible thanks to tuple upacking.
Let’s start by exploring the swapping feature!
Swapping two variables using Typle Unpacking
Tuples can be used to swap between multiples variables
a = 1
b = 2
print("a",a) # a 1
print("b",b)# b 2
print("After Swap")
a,b = b,a
print("a",a) # a 2
print("b",b) # b 1
This removes the need for a temporary variable tmp, thanks to Python’s unique tuple unpacking.
a = 1
b = 2
tmp = a
a = b
b = tmp
print(a) # 2
print(b) # 1
Returning Multiple values from a Function using Tuple Unpacking.
Later when we will learn about functions in this tutorial, you’ll see that tuples unlock a really cool feature in Python, which is the ability to return more than two values from a single function!
def min_max(nums):
return min(nums), max(nums)
min, max = min_max([1,2,3])
print(min) # 1
print(max) # 3
Under the hood Python automatically packs the multiple returned values into a tuple, which makes it possible to later use tuple upacking to access the returned values individually min and max.
Enjoyed this article?
I'm currently open to new roles — remote-first or international. If something resonated or you'd like to collaborate, I'd love to hear from you.
This article was originally published on https://sidaliassoul.com/blog/python-tuples-syntax-immutability-and-tuple-unpacking/. It was written by a human and polished using grammar tools for clarity.