eolas/zk/Tuples_in_Python.md

103 lines
1.7 KiB
Markdown

---
tags: [python, data-structures]
---
Tuples are one of the main data-structures or containers in Python. Tuples are
useful in cases where you want to group related data and ensure that it will not
change.
Tuples have the following properties:
- They are **ordered**
- They have a **fixed size**
- They are **immutable** and cannot be modified
- **Allow duplicate** members
- They are **indexed**
> In essence a tuple is a list that is immutable.
As with all containers in Python they permit any data type.
> Tuples are denoted with `(...)`
## Basic usage
```python
tup1 = (1, 3, 5, 7)
print(tup1[2])
# 5
```
> Really you should know in advance how long your tuple is going to be but if
> you just do `tup = ('shoe')` this will be processed by Python as a single
> string. Instead if you want to indicate that the tuple may be expanded later
> use `tup = ('shoe',)`. The comma converts it from a string to a tuple.
## Slicing
```python
tup1 = (1, 3, 5, 7)
print(tup1[1:3])
print(tup1[:3])
print(tup1[1:])
print(tup1[::-1])
"""
(3, 5)
(1, 3, 5)
(3, 5, 7)
(7, 5, 3, 1)
"""
```
## Looping
```python
tup3 = ('apple', 'pear', 'orange', 'plum', 'apple')
for x in tup3:
print(x)
"""
apple
pear
orange
plum
apple
"""
```
## Useful methods and predicates
```python
tup3 = ('apple', 'pear', 'orange', 'plum', 'apple')
# Count instances of a member
print(tup3.count('apple'))
# 2
# Get index of a member
print(tup3.index('pear'))
# 1
# Check for membership
if 'orange' in tup3:
print('orange is in the Tuple')
# orange is in the Tuple
```
## Nest tuples
```python
tuple2 = ('John', 'Denise', 'Phoebe', 'Adam')
tuple3 = (42, tuple1, tuple2, 5.5)
print(tuple3)
# (42, (1, 3, 5, 7), ('John', 'Denise', 'Phoebe', 'Adam'), 5.5)
```
// TODO: How to flatten a tuple?