
Python
Learning Level
Tuples are like lists but immutableβonce created, you cannot change them. This makes them perfect for protecting data.
Tuples use parentheses `()` instead of square brackets.
# Create tuples
colors = ("red", "green", "blue")
single_item = ("apple",) # Note the comma!
coordinates = (10, 20, 30)
# Tuples are immutable
coordinates[0] = 15 # β TypeError: 'tuple' object does not support item assignmentImmutable means you can't change it after creation. This is useful for:
# A tuple makes a safe "fixed" coordinate
position = (10, 20)
# This works - create a new tuple
position = (15, 25)
# This fails - can't modify
position[0] = 15 # β Error# Store city coordinates (latitude, longitude)
cities = {
"New York": (40.7128, -74.0060),
"London": (51.5074, -0.1278),
"Tokyo": (35.6762, 139.6503)
}
# Access coordinates safely
ny_coords = cities["New York"]
print(f"New York: {ny_coords}") # (40.7128, -74.0060)
# Tuple unpacking
latitude, longitude = cities["London"]
print(f"Latitude: {latitude}, Longitude: {longitude}")| Feature | Tuple | List |
|---|---|---|
| Brackets | `()` | `[]` |
| Mutable | β No | β Yes |
| Fast | β Faster | Slower |
| Dict Key | β Can use | β Cannot |
| Memory | Less | More |
Ready to practice? Challenges | Quiz
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep β’ Build Real β’ Verify Skills β’ Launch Forward