Learning outcomes
By the end of this lecture, you should be able to:- define tuple and explain immutability
- create tuples correctly
- unpack tuple values into variables
- compare tuple with list
What is a tuple?
- A tuple is an ordered, immutable collection.
- Immutable means values cannot be changed after creation.
Single-element tuple trap
Indexing and slicing
Immutability trap
Packing and unpacking
Tuple vs list
- tuple: fixed data, immutable
- list: changeable data, mutable
Exam hints and traps
- tuple uses
() - single-element tuple needs trailing comma
- unpack variable count must match tuple length
- tuple can store mixed types
Quick practice
- Create tuple of three cities.
- Unpack
(7, 8)intoaandb. - Explain why tuple is preferred for fixed coordinate pair.
