Introduction to Python – Data Structures (Tuples)
You can follow along with the code from Github.
- A tuple consists of a number of values separated by commas.
Tuple Packing and Unpacking
- In Python we can pack the tuple by putting values into a new tuple
- Unpacking a tuple is when we extract its values into a single variable.
List Vs Tuples
- Lists are mutable – They can be changed or modified after creation according to the developers needs.
- Tuples are immutable – they can’t be changed or modified after creation.
- List have more operations (functions) available to them.
- Syntax is different
- A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses).