# 5.2. Membership¶

## 5.2.1. Rationale¶

• in checks whether value is in sequence

• works with str, list, tuple, set, frozenset, dict

• Computational complexity for checking if sequence "contains":

• O(n) - in str

• O(n) - in list

• O(n) - in tuple

• O(1) - in set

• O(1) - in frozenset

• O(1) - in dict

## 5.2.2. Contains¶

• in - Contains

Listing 5.9. str
'x' in 'Python'                 # False
'P' in 'Python'                 # True
'p' in 'Python'                 # False

('x') in 'Python'               # False
('P') in 'Python'               # True
('p') in 'Python'               # False

('x',) in 'Python'              # TypeError: 'in <string>' requires string as left operand, not tuple
('P',) in 'Python'              # TypeError: 'in <string>' requires string as left operand, not tuple
('p',) in 'Python'              # TypeError: 'in <string>' requires string as left operand, not tuple

'Python' in 'Python'            # True
'Py' in 'Python'                # True

Listing 5.10. list
1 in [1, 2]               # True
2 in [1, 2]               # True
3 in [1, 2]               # False

[1] in [1, 2]             # False
[2] in [1, 2]             # False
[3] in [1, 2]             # False

[1,] in [1, 2]            # False
[2,] in [1, 2]            # False
[3,] in [1, 2]            # False

[1, 2] in [1, 2]          # False
[3, 4] in [1, 2, [3, 4]]  # True

Listing 5.11. tuple
1 in (1, 2)               # True
2 in (1, 2)               # True
3 in (1, 2)               # False

(1) in (1, 2)             # True
(2) in (1, 2)             # True
(3) in (1, 2)             # False

(1,) in (1, 2)            # False
(2,) in (1, 2)            # False
(3,) in (1, 2)            # False

(1, 2) in (1, 2)          # False
(3, 4) in (1, 2, (3, 4))  # True

Listing 5.12. set
1 in {1, 2}               # True
2 in {1, 2}               # True
3 in {1, 2}               # False

{1} in {1, 2}             # False
{2} in {1, 2}             # False
{3} in {1, 2}             # False

{1,} in {1, 2}            # False
{2,} in {1, 2}            # False
{3,} in {1, 2}            # False

{1, 2} in {1, 2}          # False
{3, 4} in {1,2, {3, 4}}   # True

crew = {
'commander': 'Melissa Lewis',
'botanist': 'Mark Watney',
'chemist': 'Alex Vogel'}

'commander' in crew             # True
'pilot' in crew                 # False


## 5.2.3. Missing¶

• not in - Missing

Listing 5.13. str
'P' not in 'Python'             # False
'p' not in 'Python'             # True
'py' not in 'Python'            # True
'Py' not in 'Python'            # False

Listing 5.14. list
1 not in [1, 2]           # False
3 not in [1, 2]           # True

[2] not in [1, 2]         # True
[1, 2] not in [1, 2]      # True

Listing 5.15. tuple
1 not in (1, 2)           # False
3 not in (1, 2)           # True

(2) not in (1, 2)        # False
(1, 2) not in (1, 2)     # True

Listing 5.16. set
1 not in {1, 2}           # False
3 not in {1, 2}           # True

{2} not in {1, 2}         # True
{1, 2} not in {1, 2}      # True

crew = {
'commander': 'Melissa Lewis',
'botanist': 'Mark Watney',
'chemist': 'Alex Vogel'}

'commander' not in crew         # False
'pilot' not in crew             # True


## 5.2.4. Assignments¶

Todo

Create assignments