Understanding Python Tuples
Tuples are a core data structure in Python. They let you store an ordered sequence of items. For example, a tuple may be used to store a list of employee names. You could use a tuple to store a list of ice cream flavors stocked at an ice cream shop. A tuple is a comma-separated sequence of items. This sequence is surrounded by parenthesis (). Let’s create a tuple:
this_tuple = ('Chocolate', 'Vanilla', 'Mint', 'Strawberry', 'Choc-Chip')
The code above returns:
('Chocolate', 'Vanilla', 'Mint', 'Strawberry', 'Choc-Chip')
Features of Tuple
1. Tuples are used to store multiple items in a single variable.
2. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage.
3. A tuple is a collection which is ordered and unchangeable.
4. Tuples are written with round bracket.
Tuple Items:
Individual values in a tuple are called items. The following are features of tuple items.
1. Tuple items are ordered: When we say that tuples are ordered, it means that the items have a defined order, and that order will not change.
2. Unchangeable: meaning that we cannot change, add or remove items after the tuple has been created.
3. Indexing: Tuple items are indexed, the first item has index [0], the second item has index [1] etc.
4. Allow duplicate values: Since tup