What is the main difference between a list [1, 2] and a tuple (1, 2)?
Options
- Lists can hold only numbers; tuples can hold any type
- Lists can change after creation; tuples cannot be changed (Correct option)
- Tuples cannot be indexed; lists can be indexed by position
- There is no real difference; they are two names for one type
Answer
Lists are mutable (can change); tuples are immutable (cannot change after creation)
Why
Both are ordered collections and you can read items by position (x[0]).
A list can change after creation: append, remove or replace items. A tuple is fixed once created.
Use tuples for fixed groups of values, like coordinates (x, y). Because they cannot change, tuples can also be dictionary keys.
Remember
List = changeable. Tuple = fixed.