Python - Introduction to Tuples
Code Life
By Pierre Marsaa
What are Tuples?
A tuple is an ordered, immutable collection of elements. In simple terms, it's like a list that cannot be changed after creation.
Tuples are created using parentheses () or the tuple() constructor. Here are some examples:
↗️
Loading editor...
↶
↷
Creating a tuple with one element
To create a tuple with just one element, you must include a trailing comma after the element. Without the comma, Python treats it as a regular value in parentheses.
Example: single_element = (42,) # This is a tuple
Not a tuple: just_a_number = (42) # This is just an integer
↗️
Loading editor...
↶
↷
Verify