Introduction
Tuples are used to store a group of items together that should not change. They keep data safe and organized.
Jump into concepts and practice - no test required
Tuples are used to store a group of items together that should not change. They keep data safe and organized.
tuple_name = (item1, item2, item3)
colors = ('red', 'green', 'blue')
point = (10, 20)
empty = ()
single = ('hello',)This program uses a tuple to return two values from a function: the minimum and maximum of a list. The tuple keeps these two results together and unchanged.
def get_min_max(numbers): return (min(numbers), max(numbers)) values = [4, 7, 1, 9] result = get_min_max(values) print(f"Min: {result[0]}, Max: {result[1]}")
Tuples are faster than lists because they cannot change.
Use tuples when you want to protect data from accidental changes.
You can use tuples as keys in dictionaries, but lists cannot be used as keys.
Tuples store multiple items together that should not change.
They are useful for fixed collections of data.
Tuples help keep data safe and can be used as dictionary keys.
tuple_example = (1, 2, 3)coords = (10, 20)
try:
coords[0] = 15
except TypeError as e:
print(e)my_dict = {}
key = [1, 2]
my_dict[key] = "value"gps_data = { (40.7128, -74.0060): "New York", [34.0522, -118.2437]: "Los Angeles" }