2.3. Syntax Types
Numeric Types (
int
,float
,complex
)Logic Types (
bool
,None
)String Types (
str
,bytes
)Iterables Types (
list
,tuple
,set
)Mapping Types (
dict
)type()
- Returns exact type of an argument
This concept is only briefly described here. More information will be in upcoming chapters.
2.3.1. Numeric Types
int
- Integer numberfloat
- Floating point numbercomplex
- Complex number
data = 1 # int
data = 1.0 # float
data = 1.0+2.0j # complex
2.3.2. Logic Types
bool
- Boolean valueNone
- Empty value (null)
data = True # bool
data = False # bool
data = None # NoneType
2.3.3. String Types
str
- Unicode stringbytes
- Bytes string
data = 'Alice' # str
data = b'Alice' # bytes
2.3.4. Iterable Types
list
- Ordered and mutable sequence of objectstuple
- Ordered and immutable sequence of objectsset
- Unordered and mutable sequence of objects
data = [1, 2.0, 'three'] # list
data = (1, 2.0, 'three') # tuple
data = {1, 2.0, 'three'} # set
2.3.5. Mapping Types
dict
- Key-value data structure
data = {
'name': 'Alice',
'email': 'alice@example.com',
'age': 30,
}
2.3.6. 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.7. Recap
Numeric Types (
int
,float
,complex
)Logic Types (
bool
,None
)String Types (
str
,bytes
)Iterables Types (
list
,tuple
,set
)Mapping Types (
dict
)type()
- Returns exact type of an argument
2.3.8. Use Case - 1
name = 'Alice'
age = 30
height = 170.0
friends = None
is_staff = True
groups = ['users', 'admin']
2.3.9. Assignments
# %% About
# - Name: Syntax Types Basic
# - Difficulty: easy
# - Lines: 5
# - Minutes: 3
# %% 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. 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ść
# %% Doctests
"""
>>> 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'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% Imports
# %% Types
result_a: int
result_b: float
result_c: bool
result_d: None
result_e: str
# %% Data
# %% Result
result_a = ...
result_b = ...
result_c = ...
result_d = ...
result_e = ...
# %% About
# - Name: Syntax Types Iterable
# - Difficulty: easy
# - Lines: 3
# - Minutes: 3
# %% 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. 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ść
# %% Doctests
"""
>>> 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}
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% Imports
# %% Types
result_a: tuple[int,...]
result_b: list[int]
result_c: set[int]
# %% Data
# %% Result
result_a = ...
result_b = ...
result_c = ...
# %% About
# - Name: Syntax Types Mapping
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% 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. 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ść
# %% Doctests
"""
>>> 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}
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% Imports
# %% Types
result: dict[str,int]
# %% Data
# %% Result
result = ...
# %% About
# - Name: Syntax Types Type
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% 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. Define variable `result` with result of type checking 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ść
# %% Doctests
"""
>>> 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'>
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% Imports
result: type[int]
# %% Types
# %% Data
DATA = 1
# %% Result
result = ...