It’s Ordered (Indexed)
Elements in a tuple are stored in order – stored with an index or position – their position in the collection is fixed, and they can be found by their position.
It’s Unchangeable (Or Immutable)
Once a tuple is created, elements in it cannot be moved, removed, added, or altered.
Duplicates are Allowed
Duplicate values are allowed as tuples are indexed, so multiple elements with the same value can be stored at different indexes without conflict.
It’s Used for Storing an Unchanging Collection of Data
Tuples are defined using a comma-separated list of values in brackets.
Here’s an example of a tuple in use:
zoo = ("ape", "parrot", "giraffe","penguin", "penguin") print(zoo)
The variable zoo is assigned the value of a tuple containing the zoo animals – there are two penguins, as duplicates are allowed.
Other Collection Types
Python has several built-in variable types for handling collections to cover different scenarios:
- Tuple (you’re reading about it here!)
…which one you will use for any given variable will depend on what kind of variables the collection will hold, whether it will be changing and what you’ll be using it for.