19.2. Typing Basic — Python
19.2.1. Int
Used to inform static type checker that the variable should be
int
>>> a: int = 1 >>> b: int = 0 >>> c: int = -1 >>> >>> d: int = int() >>> e: int = int(1)
19.2.2. Float
Used to inform static type checker that the variable should be
float
>>> a: float = 1.0 >>> b: float = 0.0 >>> c: float = -1.0 >>> >>> d: float = float() >>> e: float = float(1.0)
19.2.3. Complex
Used to inform static type checker that the variable should be
complex
>>> a: complex = 1 + 2j >>> b: complex = 1.0 + 2.0j >>> >>> c: complex = complex() >>> d: complex = complex(1, 2) >>> e: complex = complex(1.0, 2.0)
19.2.4. Bool
Used to inform static type checker that the variable should be
bool
>>> a: bool = True >>> b: bool = False >>> >>> c: bool = bool() >>> d: bool = bool(True) >>> e: bool = bool(False)
19.2.5. None
Used to inform static type checker that the variable should be
NoneNoneis notbool
19.2.6. Str
Used to inform static type checker that the variable should be
str
>>> a: str = '' >>> a: str = 'a' >>> b: str = 'alice' >>> >>> c: str = str() >>> d: str = str('a') >>> e: str = str('alice')
19.2.7. Bytes
Used to inform static type checker that the variable should be
bytesImmutable sequence of bytes
Hashable
>>> a: bytes = b'' >>> b: bytes = b'a' >>> c: bytes = b'alice' >>> >>> d: bytes = bytes() >>> e: bytes = bytes(b'a') >>> f: bytes = bytes(b'alice')
19.2.8. Bytearray
Used to inform static type checker that the variable should be
bytearrayMutable sequence of bytes
Not hashable
>>> b: bytearray = bytearray(b'') >>> b: bytearray = bytearray(b'a') >>> a: bytearray = bytearray(b'alice')
19.2.9. Use Case - 1
>>> firstname: str = 'Alice' >>> lastname: str = 'Apricot' >>> age: int = 30 >>> height: float = 170.0 >>> weight: float = 55.5 >>> adult: bool = True >>> role: None = None
19.2.10. Assignments
# %% About # - Name: Typing Basic Numeric # - Difficulty: easy # - Lines: 3 # - Minutes: 1 # %% 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 # %% 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ść # %% Expected # a: ... = 1 # b: ... = 1.0 # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 5), \ 'Python has an is invalid version; expected: `3.5` or newer.' >>> import importlib >>> from typing import get_type_hints >>> module = importlib.import_module(__name__) >>> annotations = get_type_hints(module) >>> assert annotations['a'] == int >>> assert annotations['b'] == float >>> assert a == 1, \ 'Variable `a` was modified; do not change the value, just add type annotation.' >>> assert b == 1.0, \ 'Variable `b` was modified; do not change the value, just add type annotation.' """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports # %% Types # %% Data # %% Result a = 1 b = 1.0
# %% About # - Name: Typing Basic Logic # - Difficulty: easy # - Lines: 3 # - Minutes: 1 # %% 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 # %% 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ść # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 5), \ 'Python has an is invalid version; expected: `3.5` or newer.' >>> import importlib >>> from typing import get_type_hints >>> module = importlib.import_module(__name__) >>> annotations = get_type_hints(module) >>> assert annotations['a'] == bool >>> assert annotations['b'] == bool >>> assert annotations['c'] == type(None) >>> assert a is True, \ 'Do not modify variable `a` value, just add type annotation' >>> assert b is False, \ 'Do not modify variable `b` value, just add type annotation' >>> assert c is None, \ 'Do not modify variable `c` value, just add type annotation' """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports # %% Types # %% Data # %% Result a = True b = False c = None
# %% About # - Name: Typing Basic String # - Difficulty: easy # - Lines: 2 # - Minutes: 1 # %% 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 # %% 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ść # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 5), \ 'Python has an is invalid version; expected: `3.5` or newer.' >>> import importlib >>> from typing import get_type_hints >>> module = importlib.import_module(__name__) >>> annotations = get_type_hints(module) >>> assert annotations['a'] == str >>> assert annotations['b'] == bytes >>> assert annotations['c'] == bytearray >>> assert a == 'hello', \ 'Variable `a` was modified; do not change the value, just add type annotation.' >>> assert b == b'hello', \ 'Variable `b` was modified; do not change the value, just add type annotation.' >>> assert c == bytearray(b'hello'), \ 'Variable `c` was modified; do not change the value, just add type annotation.' """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports # %% Types # %% Data # %% Result a = 'hello' b = b'hello' c = bytearray(b'hello')