What are tuples?
Tuples are like lists, but they can’t be changed once created. They’re immutable (unchangeable) sequences.
Use tuples for data that shouldn’t change:
Coordinates (x, y)
RGB colors (255, 0, 0)
Database records
Function return values
Creating tuples
# Empty tuple
empty = ()
# Tuple with items
point = ( 3 , 5 )
colors = ( "red" , "green" , "blue" )
# Single item tuple needs comma!
single = ( 42 ,) # Note the comma
not_tuple = ( 42 ) # This is just 42 in parentheses
# Without parentheses (implicit)
coordinates = 10 , 20
A single-item tuple needs a comma: (42,) not (42). Without the comma, Python thinks it’s just parentheses around a number!
Accessing items
Just like lists, tuples use indexing:
point = ( 3 , 5 )
colors = ( "red" , "green" , "blue" )
# Get items
print (point[ 0 ]) # 3
print (colors[ - 1 ]) # "blue"
# Slicing works too
print (colors[ 0 : 2 ]) # ("red", "green")
Tuple unpacking
Python’s coolest tuple feature:
# Unpack values
point = ( 3 , 5 )
x, y = point # x = 3, y = 5
# Multiple assignment
a, b, c = 1 , 2 , 3 # Same as (1, 2, 3)
# Swap variables elegantly
x, y = y, x # Swaps values!
Common mistakes
Forgetting comma in single tuple
# Wrong - not a tuple
single = ( 42 )
print ( type (single)) # <class 'int'>
# Right - include comma
single = ( 42 ,)
print ( type (single)) # <class 'tuple'>
# Wrong - tuples are immutable
point = ( 3 , 5 )
point[ 0 ] = 4 # TypeError!
# Right - create a new tuple
point = ( 4 , point[ 1 ])
# Or convert to list, modify, convert back
temp = list (point)
temp[ 0 ] = 4
point = tuple (temp)
What’s next?
Finally, let’s explore sets - collections of unique items!