3.2. Typing Basic

3.2.1. Int

  • Used to inform static type checker that the variable should be int

>>> a: int = 1
>>> b: int = 0
>>> c: int = -1

3.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

3.2.3. Bool

  • Used to inform static type checker that the variable should be bool

>>> a: bool = True
>>> b: bool = False

3.2.4. None

  • Used to inform static type checker that the variable should be None

  • None is not bool

>>> a: None = None

3.2.5. Str

  • Used to inform static type checker that the variable should be str

>>> a: str = 'Mark'
>>> b: str = ''

3.2.6. Bytes

  • Used to inform static type checker that the variable should be bytes

>>> a: bytes = b'Mark'
>>> b: bytes = b''

3.2.7. Use Case - 1

>>> firstname: str = 'Mark'
>>> lastname: str = 'Watney'
>>> age: int = 42
>>> height: float = 178.0
>>> weight: float = 75.5
>>> adult: bool = True
>>> role: None = None

3.2.8. 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 Basic Numeric
# - 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, 5), \
'Python 3.5+ required'

>>> 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, \
'Do not modify variable `a` value, just add type annotation'
>>> assert b == 1.0, \
'Do not modify variable `b` value, just add type annotation'
"""

# Add type annotations to variables
a = 1
b = 1.0


# %% 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 Basic Logic
# - 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, 5), \
'Python 3.5+ required'

>>> 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'
"""

# Add type annotations to variables
a = True
b = False
c = None


# %% 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 Basic String
# - Difficulty: easy
# - Lines: 2
# - 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, 5), \
'Python 3.5+ required'

>>> 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 a == 'hello', \
'Do not modify variable `a` value, just add type annotation'
>>> assert b == b'hello', \
'Do not modify variable `b` value, just add type annotation'
"""

# Add type annotations to variables
a = 'hello'
b = b'hello'