Tuples and Immutability
Understand tuples, their immutability, and when to prefer them over lists.
Introduction
Tuples are ordered, immutable sequences. Once created, their elements cannot be changed.
Creating Tuples
t = (1, 2, 3) or t = 1, 2, 3 (parens optional). A single-element tuple needs a trailing comma: t = (1,).
t = (1, 2, 3)
singleton = (42,)
print(t, singleton)All lessons in this course
- Creating and Accessing Lists
- Modifying Lists
- Tuples and Immutability
- Nested Lists and Iteration