A sensor reading pairs latitude and longitude values that belong together.
What You Need to Understand
Tuples are ordered sequences whose element references cannot be reassigned. Parentheses are common notation, but the comma creates a tuple in most expressions.
When to Use It
A sensor reading pairs latitude and longitude values that belong together.
Step-by-Step Python Example
location = (32.08, 34.78)
print(location[0])
print(len(location))
print(type(location).__name__)
How the Example Works
Indexing works like a list, but assigning to location[0] would raise TypeError.
Example Output
32.08
2
tuple
120 80 30
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
rgb = (120, 80, 30)
red, green, blue = rgb
print(red, green, blue)
Important Behavior and Edge Cases
Tuple immutability is shallow: a mutable object stored inside a tuple can still change.
Try It Yourself
Store a fixed RGB color as a tuple and unpack it into three variables.
Common Beginner Mistakes
- Tuple immutability is shallow: a mutable object stored inside a tuple can still change.
- Assuming every sequence operation modifies the original data or that a tuple makes nested mutable objects immutable.
Key Takeaways
- Tuples are ordered sequences whose element references cannot be reassigned.
- Indexing works like a list, but assigning to
location[0]would raiseTypeError. - Tuple immutability is shallow: a mutable object stored inside a tuple can still change.