Facebook Pixel

Tuples

Just like lists, Python also provides another way to store multiple items: tuples. Tuples look similar to lists, but have one important difference: they cannot be changed after creation.

Tuples are ordered collections of data items. They store multiple items in a single variable. Tuple items are separated by commas and enclosed within round brackets (). Tuples are unchangeable, meaning we cannot alter them after creation.

Note: It is the comma that creates a tuple, not the parentheses.

Example: a = (5) is not a tuple. It is just the number 5. To create a tuple with one item, you must add a comma. a = (5,)

Example 1:

tuple1 = (1, 2, 2, 3, 5, 4, 6)
tuple2 = ("Red", "Green", "Blue")
print(tuple1)
print(tuple2)

Output:

(1, 2, 2, 3, 5, 4, 6)
('Red', 'Green', 'Blue')

Example 2:

details = ("Abhijeet", 18, "FYBScIT", 9.8)
print(details)

Output:

('Abhijeet', 18, 'FYBScIT', 9.8)

Tuples are useful when you want to store fixed data that should not be modified, such as coordinates, dates, or constants.