3.3. Syntax Types
Numeric Types (
int,float,complex)Logic Types (
bool,None)String Types (
str,bytes)Iterables Types (
tuple,list,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.
3.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
3.3.2. Logic Types
bool- Boolean valueNone- Empty value (null)
>>> data = True # bool
>>> data = False # bool
>>> data = None # NoneType
3.3.3. String Types
str- Unicode stringbytes- Bytes string
>>> data = 'Alice' # str
>>> data = b'Alice' # bytes
3.3.4. Iterable Types
tuple- Ordered and immutable sequence of objectslist- Ordered and mutable sequence of objectsset- Unordered and mutable sequence of objects
>>> data = (1, 2.0, 'three') # tuple
>>> data = [1, 2.0, 'three'] # list
>>> data = {1, 2.0, 'three'} # set
3.3.5. Mapping Types
dict- Key-value data structure
>>> data = {
... 'firstname': 'Alice',
... 'lastname': 'Apricot',
... 'age': 30,
... }
3.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
3.3.7. Recap
Numeric Types (
int,float,complex)Logic Types (
bool,None)String Types (
str,bytes)Iterables Types (
tuple,list,set)Mapping Types (
dict)type()- Returns exact type of an argument
3.3.8. Use Case - 1
>>> name = 'Alice'
>>> age = 30
>>> height = 170.0
>>> friends = None
>>> is_staff = True
>>> groups = ['users', 'admin']
3.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 `result1` with value 1
# 2. Define variable `result2` with value 1.0
# 3. Define variable `result3` with value True
# 4. Define variable `result4` with value None
# 5. Define variable `result5` with value 'hello'
# 6. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj zmienną `result1` z wartością 1
# 2. Zdefiniuj zmienną `result2` z wartością 1.0
# 3. Zdefiniuj zmienną `result3` z wartością True
# 4. Zdefiniuj zmienną `result4` z wartością None
# 5. Zdefiniuj zmienną `result5` z wartością 'hello'
# 6. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result1
# 1
#
# >>> result2
# 1.0
#
# >>> result3
# True
#
# >>> result4
# None
#
# >>> result5
# 'hello'
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert 'result1' in globals(), \
'Variable `result1` is not defined; assign result of your program to it.'
>>> assert result1 is not Ellipsis, \
'Variable `result1` has an invalid value; assign result of your program to it.'
>>> assert type(result1) is int, \
'Variable `result1` has an invalid type; expected: `int`.'
>>> assert result1 == 1, \
'Variable `result1` has invalid value, should be 1'
>>> assert 'result2' in globals(), \
'Variable `result2` is not defined; assign result of your program to it.'
>>> assert result2 is not Ellipsis, \
'Variable `result2` has an invalid value; assign result of your program to it.'
>>> assert type(result2) is float, \
'Variable `result2` has an invalid type; expected: `float`.'
>>> assert result2 == 1.0, \
'Variable `result2` has invalid value, should be 1.0'
>>> assert 'result3' in globals(), \
'Variable `result3` is not defined; assign result of your program to it.'
>>> assert result3 is not Ellipsis, \
'Variable `result3` has an invalid value; assign result of your program to it.'
>>> assert type(result3) is bool, \
'Variable `result3` has an invalid type; expected: `bool`.'
>>> assert result3 is True, \
'Variable `result3` has invalid value, should be True'
>>> assert 'result4' in globals(), \
'Variable `result4` is not defined; assign result of your program to it.'
>>> assert result4 is not Ellipsis, \
'Variable `result4` has an invalid value; assign result of your program to it.'
>>> assert type(result4) is type(None), \
'Variable `result4` has an invalid type; expected: `type(None)`.'
>>> assert result4 is None, \
'Variable `result4` has invalid value, should be None'
>>> assert 'result5' in globals(), \
'Variable `result5` is not defined; assign result of your program to it.'
>>> assert result5 is not Ellipsis, \
'Variable `result5` has an invalid value; assign result of your program to it.'
>>> assert type(result5) is str, \
'Variable `result5` has an invalid type; expected: `str`.'
>>> assert result5 == 'hello', \
'Variable `result5` has invalid value, should be "hello"'
>>> from pprint import pprint
>>> pprint(result1)
1
>>> pprint(result2)
1.0
>>> pprint(result3)
True
>>> pprint(result4)
None
>>> pprint(result5)
'hello'
"""
# %% 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
result1: int
result2: float
result3: bool
result4: None
result5: str
# %% Data
# %% Result
result1 = ...
result2 = ...
result3 = ...
result4 = ...
result5 = ...
# %% 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 `result1` with value (1, 2, 3)
# 2. Define variable `result2` with value [1, 2, 3]
# 3. Define variable `result3` with value {1, 2, 3}
# 4. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj zmienną `result1` z wartością (1, 2, 3)
# 2. Zdefiniuj zmienną `result2` z wartością [1, 2, 3]
# 3. Zdefiniuj zmienną `result3` z wartością {1, 2, 3}
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result1
# (1, 2, 3)
#
# >>> result2
# [1, 2, 3]
#
# >>> result3
# {1, 2, 3}
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert 'result1' in globals(), \
'Variable `result1` is not defined; assign result of your program to it.'
>>> assert result1 is not Ellipsis, \
'Variable `result1` has an invalid value; assign result of your program to it.'
>>> assert type(result1) is tuple, \
'Variable `result1` has an invalid type; expected: `tuple`.'
>>> assert result1 == (1, 2, 3), \
'Variable `result1` has invalid value, should be (1, 2, 3)'
>>> assert 'result2' in globals(), \
'Variable `result2` is not defined; assign result of your program to it.'
>>> assert result2 is not Ellipsis, \
'Variable `result2` has an invalid value; assign result of your program to it.'
>>> assert type(result2) is list, \
'Variable `result2` has an invalid type; expected: `list`.'
>>> assert result2 == [1, 2, 3], \
'Variable `result2` has invalid value, should be [1, 2, 3]'
>>> assert 'result3' in globals(), \
'Variable `result3` is not defined; assign result of your program to it.'
>>> assert result3 is not Ellipsis, \
'Variable `result3` has an invalid value; assign result of your program to it.'
>>> assert type(result3) is set, \
'Variable `result3` has an invalid type; expected: `set`.'
>>> assert result3 == {1, 2, 3}, \
'Variable `result3` has invalid value, should be {1, 2, 3}'
>>> from pprint import pprint
>>> pprint(result1)
(1, 2, 3)
>>> pprint(result2)
[1, 2, 3]
>>> pprint(result3)
{1, 2, 3}
"""
# %% 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
result1: tuple[int,...]
result2: list[int]
result3: set[int]
# %% Data
# %% Result
result1 = ...
result2 = ...
result3 = ...
# %% 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ść
# %% Expected
# >>> result
# {'a': 1, 'b': 2, 'c': 3}
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is dict, \
'Variable `result` has an invalid type; expected: `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 -f -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ść
# %% Expected
# >>> result
# <class 'int'>
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is type, \
'Variable `result` has an invalid type; expected: `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 -f -v myfile.py`
# %% Imports
result: type[int]
# %% Types
# %% Data
DATA = 1
# %% Result
result = ...