3.4. Typing Iterable

  • Since Python 3.9: PEP 585 -- Type Hinting Generics In Standard Collections

  • Before Python 3.9 you need from typing import List, Tuple, Set, Frozenset

3.4.1. Tuple

>>> data: tuple = (1, 2, 3)
>>> data: tuple[int,int,int] = (1, 2, 3)
>>> data: tuple[int,...] = (1, 2, 3)
>>> data: tuple[int,float,str] = (1, 2.0, 'three')

3.4.2. List

>>> data: list = [1, 2, 3]
>>> data: list[int] = [1, 2, 3]
>>> data: list[int|float|str] = [1, 2.0, 'three']

3.4.3. Set

>>> data: set = {1, 2, 3}
>>> data: set[int] = {1, 2, 3}
>>> data: set[int|float|str] = {1, 2.0, 'three'}

3.4.4. Frozenset

>>> data: frozenset = frozenset({1, 2, 3})
>>> data: frozenset[int] = frozenset({1, 2, 3})
>>> data: frozenset[int|float|str] = frozenset({1, 2.0, 'three'})

3.4.5. Use Case - 1

>>> data: tuple[str,str,int] = ('Mark', 'Watney', 41)

3.4.6. Use Case - 2

>>> data: tuple[float,float,float,float,str] = (5.1, 3.5, 1.4, 0.2, 'setosa')

3.4.7. Further Reading

3.4.8. References

3.4.9. Assignments

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Typing Annotations Tuple
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2

# %% English
# 1. Add type annotations to variables
# 2. Run doctests - all must succeed

# %% Polish
# 1. Dodaj adnotacje typów do zmiennych
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> import importlib
>>> from typing import get_type_hints
>>> module = importlib.import_module(__name__)
>>> annotations = get_type_hints(module)

>>> assert annotations['a'] == tuple
>>> assert annotations['b'] == tuple[int,...]
>>> assert annotations['c'] == tuple[int,float,str]

>>> assert a == (), \
'Do not modify variable `a` value, just add type annotation'
>>> assert b == (1, 2, 3), \
'Do not modify variable `b` value, just add type annotation'
>>> assert c == (1, 2.0, 'three'), \
'Do not modify variable `c` value, just add type annotation'
"""

# Add type annotations to variables
a = ()
b = (1, 2, 3)
c = (1, 2.0, 'three')


# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Typing Annotations List
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2

# %% English
# 1. Add type annotations to variables
# 2. Run doctests - all must succeed

# %% Polish
# 1. Dodaj adnotacje typów do zmiennych
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> import importlib
>>> from typing import get_type_hints
>>> module = importlib.import_module(__name__)
>>> annotations = get_type_hints(module)

>>> assert annotations['a'] == list
>>> assert annotations['b'] == list[int]
>>> assert annotations['c'] == list[int|float|str]

>>> assert a == [], \
'Do not modify variable `a` value, just add type annotation'
>>> assert b == [1, 2, 3], \
'Do not modify variable `b` value, just add type annotation'
>>> assert c == [1, 2.0, 'three'], \
'Do not modify variable `c` value, just add type annotation'
"""

# Add type annotations to variables
a = []
b = [1, 2, 3]
c = [1, 2.0, 'three']


# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Typing Annotations Set
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2

# %% English
# 1. Add type annotations to variables
# 2. Run doctests - all must succeed

# %% Polish
# 1. Dodaj adnotacje typów do zmiennych
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> import importlib
>>> from typing import get_type_hints
>>> module = importlib.import_module(__name__)
>>> annotations = get_type_hints(module)

>>> assert annotations['a'] == set
>>> assert annotations['b'] == set[int]
>>> assert annotations['c'] == set[int|float|str]

>>> assert a == set(), \
'Do not modify variable `a` value, just add type annotation'
>>> assert b == {1, 2, 3}, \
'Do not modify variable `b` value, just add type annotation'
>>> assert c == {1, 2.0, 'three'}, \
'Do not modify variable `c` value, just add type annotation'
"""

# Add type annotations to variables
a = set()
b = {1, 2, 3}
c = {1, 2.0, 'three'}