2.3. Syntax Types
2.3.1. Summary
int
- Integer numberfloat
- Floating point numberbool
- Boolean valueNone
- Empty or unknown value (null)str
- Character stringlist
- Ordered sequence of objects (can be modified)tuple
- Ordered sequence of objects (cannot be modified)set
- Unordered sequence of objects (can be modified)dict
- Key-value data structuretype()
- Returns exact type of an argument
This concept is only briefly described here. More information will be in upcoming chapters.
2.3.2. Basic types
int
- Integer numberfloat
- Floating point numberbool
- Boolean valueNone
- Empty value (null)str
- Character string
>>> data = 1 # int
>>> data = 1.0 # float
>>> data = True # bool
>>> data = False # bool
>>> data = None # NoneType
>>> data = 'abc' # str
2.3.3. Iterables
list
- Ordered sequence of objects (can be modified)tuple
- Ordered sequence of objects (cannot be modified)set
- Unordered sequence of objects (can be modified)
>>> data = [1, 2, 3] # list
>>> data = (1, 2, 3) # tuple
>>> data = {1, 2, 3} # set
2.3.4. Mappings
dict
- Key-value data structure
>>> data = {'a': 1, 'b': 2} # dict
2.3.5. Type Checking
type()
- Returns type of an argumentTo check if type is what you expected use
type()
>>> x = 1
>>> type(x)
<class 'int'>
>>> x = 1
>>> type(x) is int
True
2.3.6. Recap
int
- Integer numberfloat
- Floating point numberbool
- Boolean valueNone
- Empty or unknown value (null)str
- Character stringlist
- Ordered sequence of objects (can be modified)tuple
- Ordered sequence of objects (cannot be modified)set
- Unordered sequence of objects (can be modified)dict
- Key-value data structuretype()
- Returns exact type of an argument
2.3.7. Use Case - 1
>>> name = 'Mark Watney'
>>> age = 42
>>> height = 178.0
>>> friends = None
>>> is_staff = True
>>> groups = ['users', 'admin']
2.3.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: Syntax Types Basic
# - Difficulty: easy
# - Lines: 5
# - Minutes: 3
# %% English
# 1. Define variable `result_a` with value 1
# 2. Define variable `result_b` with value 1.0
# 3. Define variable `result_c` with value True
# 4. Define variable `result_d` with value None
# 5. Define variable `result_e` with value 'hello'
# 6. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj zmienną `result_a` z wartością 1
# 2. Zdefiniuj zmienną `result_b` z wartością 1.0
# 3. Zdefiniuj zmienną `result_c` z wartością True
# 4. Zdefiniuj zmienną `result_d` z wartością None
# 5. Zdefiniuj zmienną `result_e` z wartością 'hello'
# 6. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result_a is not Ellipsis, \
'Assign your result to variable `result_a`'
>>> assert type(result_a) is int, \
'Variable `result_a` has invalid type, should be int'
>>> assert result_a == 1, \
'Variable `result_a` has invalid value, should be 1'
>>> assert result_b is not Ellipsis, \
'Assign your result to variable `result_b`'
>>> assert type(result_b) is float, \
'Variable `result_b` has invalid type, should be float'
>>> assert result_b == 1.0, \
'Variable `result_b` has invalid value, should be 1.0'
>>> assert result_c is not Ellipsis, \
'Assign your result to variable `result_c`'
>>> assert type(result_c) is bool, \
'Variable `result_c` has invalid type, should be bool'
>>> assert result_c is True, \
'Variable `result_c` has invalid value, should be True'
>>> assert result_d is not Ellipsis, \
'Assign your result to variable `result_d`'
>>> assert type(result_d) is type(None), \
'Variable `result_d` has invalid type, should be None'
>>> assert result_d is None, \
'Variable `result_d` has invalid value, should be None'
>>> assert result_e is not Ellipsis, \
'Assign your result to variable `result_e`'
>>> assert type(result_e) is str, \
'Variable `result_e` has invalid type, should be str'
>>> assert result_e == 'hello', \
'Variable `result_e` has invalid value, should be "hello"'
>>> from pprint import pprint
>>> pprint(result_a)
1
>>> pprint(result_b)
1.0
>>> pprint(result_c)
True
>>> pprint(result_d)
None
>>> pprint(result_e)
'hello'
"""
# Define variable `result_a` with value 1
# type: int
result_a = ...
# Define variable `result_b` with value 1.0
# type: float
result_b = ...
# Define variable `result_c` with value True
# type: bool
result_c = ...
# Define variable `result_d` with value None
# type: None
result_d = ...
# Define variable `result_e` with value 'hello'
# type: str
result_e = ...
# %% 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: Syntax Types Iterable
# - Difficulty: easy
# - Lines: 3
# - Minutes: 3
# %% English
# 1. Define variable `result_a` with value (1, 2, 3)
# 2. Define variable `result_b` with value [1, 2, 3]
# 3. Define variable `result_c` with value {1, 2, 3}
# 4. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj zmienną `result_a` z wartością (1, 2, 3)
# 2. Zdefiniuj zmienną `result_b` z wartością [1, 2, 3]
# 3. Zdefiniuj zmienną `result_c` z wartością {1, 2, 3}
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result_a is not Ellipsis, \
'Assign your result to variable `result_a`'
>>> assert type(result_a) is tuple, \
'Variable `result_a` has invalid type, should be tuple'
>>> assert result_a == (1, 2, 3), \
'Variable `result_a` has invalid value, should be (1, 2, 3)'
>>> assert result_b is not Ellipsis, \
'Assign your result to variable `result_b`'
>>> assert type(result_b) is list, \
'Variable `result_b` has invalid type, should be list'
>>> assert result_b == [1, 2, 3], \
'Variable `result_b` has invalid value, should be [1, 2, 3]'
>>> assert result_c is not Ellipsis, \
'Assign your result to variable `result_c`'
>>> assert type(result_c) is set, \
'Variable `result_c` has invalid type, should be set'
>>> assert result_c == {1, 2, 3}, \
'Variable `result_c` has invalid value, should be {1, 2, 3}'
>>> from pprint import pprint
>>> pprint(result_a)
(1, 2, 3)
>>> pprint(result_b)
[1, 2, 3]
>>> pprint(result_c)
{1, 2, 3}
"""
# Define variable `result_a` with value (1, 2, 3)
# type: int
result_a = ...
# Define variable `result_b` with value [1, 2, 3]
# type: float
result_b = ...
# Define variable `result_c` with value {1, 2, 3}
# type: bool
result_c = ...
# %% 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: Syntax Types Mapping
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Define variable `result` with value {'a': 1, 'b': 2, 'c': 3}
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj zmienną `result` z wartością {'a': 1, 'b': 2, 'c': 3}
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is dict, \
'Variable `result` has invalid type, should be dict'
>>> assert result == {'a': 1, 'b': 2, 'c': 3}, \
'Variable `result` has invalid value, should be {"a": 1, "b": 2, "c": 3}'
>>> from pprint import pprint
>>> pprint(result)
{'a': 1, 'b': 2, 'c': 3}
"""
# Define variable `result` with value {'a': 1, 'b': 2, 'c': 3}
# type: int
result = ...
# %% 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: Syntax Types Type
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Define variable `result` with result of type cheking of variable DATA
# 2. Use `type()`
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj zmienną `result` z rezultatem sprawdzania typu zmiennej DATA
# 2. Użyj `type()`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is type, \
'Variable `result` has invalid type, should be type'
>>> assert result is int, \
'Variable `result` has invalid value, should be int'
>>> from pprint import pprint
>>> pprint(result)
<class 'int'>
"""
DATA = 1
# Define variable `result` with result of type cheking of variable DATA
# Use `type()`
# type: type[int]
result = ...