Learning outcomes
By the end of this lecture, you should be able to:- use tuple methods and nested tuples
- perform tuple unpacking cleanly
- explain tuple use in safe fixed records
Tuple methods
Tuples have fewer methods than lists because they are immutable.Swapping values with tuples
Nested tuples
Tuple in dictionary keys / set elements
Because tuples are immutable, they are often used where mutable lists cannot be used.Tuple from iterable
Exam hints and traps
- tuple methods are mainly
countandindex - tuple unpacking is common in MCQs
- tuple can contain mutable items, but tuple structure itself stays fixed
Quick practice
- Swap
x = 5,y = 9without third variable. - Count how many times
4occurs in(4, 1, 4, 4, 2). - Explain why tuple can be a dict key but list cannot.
