What are tuples?

Tuples in Python are similar to lists in a way that they are used to store a set of elements under a single variable name. However, unlike lists, they cannot be changed once created (in other words, they are immutable). You can thought of tuples as read-only lists. They are used to represent fixed collections of elements, such as days in the week.

Elements of a tuple are defined inside the parentheses and separated by commas. For example, the my_numbers = (2,4,2,5) will create a tuple with four elements. To access the first element, we can use the my_numbers[0] statement:

my_numbers = (2, 4, 2, 5)

print(my_numbers[0])

>>>
2
>>>

Since tuples are immutable, if we try to change an item in a tuple, we will receive an error:

my_numbers[0] = 10

>>>
Traceback (most recent call last):
File "tuple.py", line 5, in <module>
my_numbers[0] = 10
TypeError: 'tuple' object does not support item assignment
>>>
Geek University 2022