Learn programming concepts interactively
✓ Can be modified, added to, removed from
✗ Cannot be modified after creation
Tuples cannot be changed after creation. This makes them hashable and usable as dictionary keys.
Tuples excel at packing multiple values and unpacking them into variables in a single operation.
Perfect for coordinates, RGB values, database records, and any fixed-structure data.
# Python Tuple Operations Example# Tuples are immutable sequences that store ordered collections of items# Creating tuples (multiple ways)my_tuple = ("apple", 5, True)# Alternative tuple creation methods:tuple_with_constructor = tuple(["apple", 5, True])single_element_tuple = ("item",) # Note the comma!empty_tuple = ()# Tuple properties and informationprint(f"Tuple length: {len(my_tuple)}")print(f"Tuple type: {type(my_tuple)}")# Comparison with lists (mutable vs immutable)comparison_list = ["apple", 5, True]print("List (mutable):", comparison_list)print("Tuple (immutable):", my_tuple)# Demonstrating mutability difference:comparison_list[0] = "modified" # This works# my_tuple[0] = "modified" # This would raise TypeError!# Accessing tuple elements (same as lists)# my_tuple[0] = "apple"# my_tuple[1] = 5# my_tuple[2] = True# Tuple methods (limited compared to lists)# my_tuple.count("apple") - count occurrences# my_tuple.index("apple") - find first index# Common tuple use cases:coordinates = (10, 20) # x, y coordinatesrgb_color = (255, 128, 0) # red, green, blueperson_data = ("Alice", 25, "Engineer") # name, age, jobdatabase_record = ("user123", "alice@email.com", True)
No operations yet
Start by adding or modifying elements