A
Aditi Sharma
Guest

β’ Tuples are ordered and immutable collections.
β’ They can store multiple items like lists, but unlike lists, they cannot be modified (no append, remove, etc.).
β’ Defined using parentheses ().
fruits = ("apple", "banana", "cherry")
print(fruits[0]) # apple






β’ Safer than lists if data shouldnβt change
β’ Faster than lists (performance benefit)
β’ Can be used as dictionary keys (since immutable)

Accessing Elements
numbers = (10, 20, 30, 40)
print(numbers[1]) # 20
Tuple Packing & Unpacking
person = ("Alice", 25, "Engineer")
name, age, profession = person
print(name) # Alice
Concatenation & Repetition
t1 = (1, 2)
t2 = (3, 4)
print(t1 + t2) # (1, 2, 3, 4)
print(t1 * 2) # (1, 2, 1, 2)
Nesting Tuples
nested = (1, (2, 3), (4, 5))
print(nested[1][1]) # 3

Tuples may look similar to lists, but their immutability makes them useful in situations where data must remain constant.

Continue reading...