4.1. Logic Bool

  • True - represents positive value

  • False - represents negative value

  • Builtin function bool() converts argument to bool

>>> data = True
>>> data = False

4.1.1. Syntax

  • First letter capitalized, other are lower cased

>>> data = True
>>> data = False
>>> data = true
Traceback (most recent call last):
NameError: name 'true' is not defined
>>> data = TRUE
Traceback (most recent call last):
NameError: name 'TRUE' is not defined

4.1.2. Conversion

  • Builtin function bool() converts argument to bool

>>> bool(1)
True
>>>
>>> bool(1.0)
True
>>>
>>> bool('hello')
True

4.1.3. Negative values

  • 0 - zero integer

  • 0.0 - zero float

  • False - false bool

  • '' - empty str

  • () - empty tuple

  • [] - empty list

  • set() - empty set

  • {} - empty dict

  • None - empty or unknown value

>>> bool(0)
False
>>>
>>> bool(0.0)
False
>>>
>>>
>>> bool(False)
False
>>>
>>> bool(None)
False
>>>
>>> bool('')
False
>>>
>>> bool(())
False
>>>
>>> bool([])
False
>>>
>>> bool(set())
False
>>>
>>> bool({})
False

4.1.4. Check

  • data is True and data is not True - identity check

  • data == True and data != True - value check

  • data is True is faster than data == True

Identity check (preferred):

>>> data = True
>>>
>>>
>>> data is True
True
>>> data is not True
False

Value check:

>>> data = True
>>>
>>>
>>> data == True
True
>>> data != True
False

Performance:

>>> data = True
>>>
>>> %%timeit -r 1000 -n 1000  # doctest: +SKIP
... data == True
...
9.12 ns ± 2.59 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
9.09 ns ± 2.39 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
9.10 ns ± 1.84 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
9.19 ns ± 3.07 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
9.43 ns ± 2.55 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
>>> data = True
>>>
>>> %%timeit -r 1000 -n 1000  # doctest: +SKIP
... data is True
...
5.58 ns ± 2.65 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
5.56 ns ± 1.91 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
5.58 ns ± 2.46 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
5.59 ns ± 2.22 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
5.58 ns ± 1.83 ns per loop (mean ± std. dev. of 1000 runs, 1,000 loops each)
  • Date: 2025-01-07

  • Python: 3.13.1

  • IPython: 8.30.0

  • System: macOS 15.2

  • Computer: MacBook M3 Max

  • CPU: 16 cores (12 performance and 4 efficiency) / 3nm

  • RAM: 128 GB RAM LPDDR5

4.1.5. Recap

  • True - represents positive value

  • False - represents negative value

  • First letter capitalized, other are lower cased

  • Builtin bool() converts argument to bool

  • 0 - zero integer

  • 0.0 - zero float

  • False - false bool

  • '' - empty str

  • () - empty tuple

  • [] - empty list

  • set() - empty set

  • {} - empty dict

  • None - empty or unknown value

  • data is True - check if something is True

Define:

>>> x = True
>>> x = False

Check:

>>> data = True
>>>
>>> data is True
True
>>>
>>> data is not True
False

Convert:

>>> bool(1)
True
>>>
>>> bool(0)
False

4.1.6. 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: Type Bool Int
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2

# %% English
# 1. Define `result_a: bool` with result of `bool(1)`
# 2. Define `result_b: bool` with result of `bool(0)`
# 3. Define `result_c: bool` with result of `bool(-1)`
# 4. Non-functional requirements:
#    - run doctests - all must succeed
#    - in place of ellipsis (`...`) insert only `True` or `False`
#    - do not evaluate expressions in REPL or script
#    - fill in what you think is the result
#    - this assignment checks if you understand the bool type

# %% Polish
# 1. Zdefiniuj `result_a: bool` z wynikiem `bool(1)`
# 2. Zdefiniuj `result_b: bool` z wynikiem `bool(0)`
# 3. Zdefiniuj `result_c: bool` z wynikiem `bool(-1)`
# 4. Wymagania niefunkcjonalne:
#    - uruchom doctesty - wszystkie muszą się powieść
#    - w miejsce trzech kropek (`...`) wstawiaj tylko `True` lub `False`
#    - nie ewaluuj wyrażeń w REPL'u ani w skrypcie Python
#    - wpisz to co Ci sie wydaje, że jest wynikiem
#    - zadanie sprawdza, czy rozumiesz typ bool

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> from pprint import pprint

>>> assert result_a is not Ellipsis, \
'Assign your result to variable `result_a`'
>>> assert type(result_a) is bool, \
'Variable `result_a` has invalid type, should be bool'

>>> assert result_b is not Ellipsis, \
'Assign your result to variable `result_b`'
>>> assert type(result_b) is bool, \
'Variable `result_b` has invalid type, should be bool'

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

>>> pprint(result_a)
True
>>> pprint(result_b)
False
>>> pprint(result_c)
True
"""

# Define `result_a: bool` with result of `bool(1)`
# type: bool
result_a = ...

# Define `result_b: bool` with result of `bool(0)`
# type: bool
result_b = ...

# Define `result_c: bool` with result of `bool(-1)`
# 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: Type Bool Float
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2

# %% English
# 1. Define `result_a: bool` with result of `bool(1.0)`
# 2. Define `result_b: bool` with result of `bool(0.0)`
# 3. Define `result_c: bool` with result of `bool(-1.0)`
# 4. Non-functional requirements:
#    - run doctests - all must succeed
#    - in place of ellipsis (`...`) insert only `True` or `False`
#    - do not evaluate expressions in REPL or script
#    - fill in what you think is the result
#    - this assignment checks if you understand the bool type

# %% Polish
# 1. Zdefiniuj `result_a: bool` z wynikiem `bool(1.0)`
# 2. Zdefiniuj `result_b: bool` z wynikiem `bool(0.0)`
# 3. Zdefiniuj `result_c: bool` z wynikiem `bool(-1.0)`
# 4. Wymagania niefunkcjonalne:
#    - uruchom doctesty - wszystkie muszą się powieść
#    - w miejsce trzech kropek (`...`) wstawiaj tylko `True` lub `False`
#    - nie ewaluuj wyrażeń w REPL'u ani w skrypcie Python
#    - wpisz to co Ci sie wydaje, że jest wynikiem
#    - zadanie sprawdza, czy rozumiesz typ bool

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> from pprint import pprint

>>> assert result_a is not Ellipsis, \
'Assign your result to variable `result_a`'
>>> assert type(result_a) is bool, \
'Variable `result_a` has invalid type, should be bool'

>>> assert result_b is not Ellipsis, \
'Assign your result to variable `result_b`'
>>> assert type(result_b) is bool, \
'Variable `result_b` has invalid type, should be bool'

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

>>> pprint(result_a)
True
>>> pprint(result_b)
False
>>> pprint(result_c)
True
"""

# Define `result_a: bool` with result of `bool(1.0)`
# type: bool
result_a = ...

# Define `result_b: bool` with result of `bool(0.0)`
# type: bool
result_b = ...

# Define `result_c: bool` with result of `bool(-1.0)`
# 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: Type Bool Bool
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2

# %% English
# 1. Define `result_a: bool` with result of `bool(True)`
# 2. Define `result_b: bool` with result of `bool(False)`
# 3. Define `result_c: bool` with result of `bool(None)`
# 4. Non-functional requirements:
#    - run doctests - all must succeed
#    - in place of ellipsis (`...`) insert only `True` or `False`
#    - do not evaluate expressions in REPL or script
#    - fill in what you think is the result
#    - this assignment checks if you understand the bool type

# %% Polish
# 1. Zdefiniuj `result_a: bool` z wynikiem `bool(True)`
# 2. Zdefiniuj `result_b: bool` z wynikiem `bool(False)`
# 3. Zdefiniuj `result_c: bool` z wynikiem `bool(None)`
# 4. Wymagania niefunkcjonalne:
#    - uruchom doctesty - wszystkie muszą się powieść
#    - w miejsce trzech kropek (`...`) wstawiaj tylko `True` lub `False`
#    - nie ewaluuj wyrażeń w REPL'u ani w skrypcie Python
#    - wpisz to co Ci sie wydaje, że jest wynikiem
#    - zadanie sprawdza, czy rozumiesz typ bool

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> from pprint import pprint

>>> assert result_a is not Ellipsis, \
'Assign your result to variable `result_a`'
>>> assert type(result_a) is bool, \
'Variable `result_a` has invalid type, should be bool'

>>> assert result_b is not Ellipsis, \
'Assign your result to variable `result_b`'
>>> assert type(result_b) is bool, \
'Variable `result_b` has invalid type, should be bool'

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

>>> pprint(result_a)
True
>>> pprint(result_b)
False
>>> pprint(result_c)
False
"""

# Define `result_a: bool` with result of `bool(True)`
# type: bool
result_a = ...

# Define `result_b: bool` with result of `bool(False)`
# type: bool
result_b = ...

# Define `result_c: bool` with result of `bool(None)`
# 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: Type Bool Str
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2

# %% English
# 1. Define `result_a: bool` with result of `bool('hello')`
# 2. Define `result_b: bool` with result of `bool('')`
# 3. Non-functional requirements:
#    - run doctests - all must succeed
#    - in place of ellipsis (`...`) insert only `True` or `False`
#    - do not evaluate expressions in REPL or script
#    - fill in what you think is the result
#    - this assignment checks if you understand the bool type

# %% Polish
# 1. Zdefiniuj `result_a: bool` z wynikiem `bool('hello')`
# 2. Zdefiniuj `result_b: bool` z wynikiem `bool('')`
# 3. Wymagania niefunkcjonalne:
#    - uruchom doctesty - wszystkie muszą się powieść
#    - w miejsce trzech kropek (`...`) wstawiaj tylko `True` lub `False`
#    - nie ewaluuj wyrażeń w REPL'u ani w skrypcie Python
#    - wpisz to co Ci sie wydaje, że jest wynikiem
#    - zadanie sprawdza, czy rozumiesz typ bool

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> from pprint import pprint

>>> assert result_a is not Ellipsis, \
'Assign your result to variable `result_a`'
>>> assert type(result_a) is bool, \
'Variable `result_a` has invalid type, should be bool'

>>> assert result_b is not Ellipsis, \
'Assign your result to variable `result_b`'
>>> assert type(result_b) is bool, \
'Variable `result_b` has invalid type, should be bool'

>>> pprint(result_a)
True
>>> pprint(result_b)
False
"""

# Define `result_a: bool` with result of `bool('hello')`
# type: bool
result_a = ...

# Define `result_b: bool` with result of `bool('')`
# type: bool
result_b = ...


# %% 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: Type Bool StrTrueFalse
# - Difficulty: easy
# - Lines: 5
# - Minutes: 3

# %% English
# 1. Define `result_a: bool` with result of `bool('0')`
# 2. Define `result_b: bool` with result of `bool('0.0')`
# 3. Define `result_c: bool` with result of `bool('False')`
# 4. Define `result_d: bool` with result of `bool('None')`
# 5. Define `result_e: bool` with result of `bool(' ')`
# 6. Non-functional requirements:
#    - run doctests - all must succeed
#    - in place of ellipsis (`...`) insert only `True` or `False`
#    - do not evaluate expressions in REPL or script
#    - fill in what you think is the result
#    - this assignment checks if you understand the bool type

# %% Polish
# 1. Zdefiniuj `result_a: bool` z wynikiem `bool('0')`
# 2. Zdefiniuj `result_b: bool` z wynikiem `bool('0.0')`
# 3. Zdefiniuj `result_c: bool` z wynikiem `bool('False')`
# 4. Zdefiniuj `result_d: bool` z wynikiem `bool('None')`
# 5. Zdefiniuj `result_e: bool` z wynikiem `bool(' ')`
# 6. Wymagania niefunkcjonalne:
#    - uruchom doctesty - wszystkie muszą się powieść
#    - w miejsce trzech kropek (`...`) wstawiaj tylko `True` lub `False`
#    - nie ewaluuj wyrażeń w REPL'u ani w skrypcie Python
#    - wpisz to co Ci sie wydaje, że jest wynikiem
#    - zadanie sprawdza, czy rozumiesz typ bool

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> from pprint import pprint

>>> assert result_a is not Ellipsis, \
'Assign your result to variable `result_a`'
>>> assert type(result_a) is bool, \
'Variable `result_a` has invalid type, should be bool'

>>> assert result_b is not Ellipsis, \
'Assign your result to variable `result_b`'
>>> assert type(result_b) is bool, \
'Variable `result_b` has invalid type, should be bool'

>>> 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_d is not Ellipsis, \
'Assign your result to variable `result_d`'
>>> assert type(result_d) is bool, \
'Variable `result_d` has invalid type, should be bool'

>>> assert result_e is not Ellipsis, \
'Assign your result to variable `result_e`'
>>> assert type(result_e) is bool, \
'Variable `result_e` has invalid type, should be bool'

>>> pprint(result_a)
True
>>> pprint(result_b)
True
>>> pprint(result_c)
True
>>> pprint(result_d)
True
>>> pprint(result_e)
True
"""

# Define `result_a: bool` with result of `bool('0')`
# type: bool
result_a = ...

# Define `result_b: bool` with result of `bool('0.0')`
# type: bool
result_b = ...

# Define `result_c: bool` with result of `bool('False')`
# type: bool
result_c = ...

# Define `result_d: bool` with result of `bool('None')`
# type: bool
result_d = ...

# Define `result_e: bool` with result of `bool(' ')`
# type: bool
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: Type Bool StrTrueFalse
# - Difficulty: easy
# - Lines: 5
# - Minutes: 3

# %% English
# 1. Define `result_a: bool` with result of `bool(int('0'))`
# 2. Define `result_b: bool` with result of `bool(float('0.0'))`
# 3. Define `result_c: bool` with result of `bool(bool('False'))`
# 4. Define `result_d: bool` with result of `bool(bool(' '))`
# 5. Define `result_e: bool` with result of `bool(str(''))`
# 6. Non-functional requirements:
#    - run doctests - all must succeed
#    - in place of ellipsis (`...`) insert only `True` or `False`
#    - do not evaluate expressions in REPL or script
#    - fill in what you think is the result
#    - this assignment checks if you understand the bool type

# %% Polish
# 1. Zdefiniuj `result_a: bool` z wynikiem `bool(int('0'))`
# 2. Zdefiniuj `result_b: bool` z wynikiem `bool(float('0.0'))`
# 3. Zdefiniuj `result_c: bool` z wynikiem `bool(bool('False'))`
# 4. Zdefiniuj `result_d: bool` z wynikiem `bool(bool(' '))`
# 5. Zdefiniuj `result_e: bool` z wynikiem `bool(str(''))`
# 6. Wymagania niefunkcjonalne:
#    - uruchom doctesty - wszystkie muszą się powieść
#    - w miejsce trzech kropek (`...`) wstawiaj tylko `True` lub `False`
#    - nie ewaluuj wyrażeń w REPL'u ani w skrypcie Python
#    - wpisz to co Ci sie wydaje, że jest wynikiem
#    - zadanie sprawdza, czy rozumiesz typ bool

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> from pprint import pprint

>>> assert result_a is not Ellipsis, \
'Assign your result to variable `result_a`'
>>> assert type(result_a) is bool, \
'Variable `result_a` has invalid type, should be bool'

>>> assert result_b is not Ellipsis, \
'Assign your result to variable `result_b`'
>>> assert type(result_b) is bool, \
'Variable `result_b` has invalid type, should be bool'

>>> 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_d is not Ellipsis, \
'Assign your result to variable `result_d`'
>>> assert type(result_d) is bool, \
'Variable `result_d` has invalid type, should be bool'

>>> assert result_e is not Ellipsis, \
'Assign your result to variable `result_e`'
>>> assert type(result_e) is bool, \
'Variable `result_e` has invalid type, should be bool'

>>> pprint(result_a)
False
>>> pprint(result_b)
False
>>> pprint(result_c)
True
>>> pprint(result_d)
True
>>> pprint(result_e)
False
"""

# Define `result_a: bool` with result of `bool(int('0'))`
# type: bool
result_a = ...

# Define `result_b: bool` with result of `bool(float('0.0'))`
# type: bool
result_b = ...

# Define `result_c: bool` with result of `bool(bool('False'))`
# type: bool
result_c = ...

# Define `result_d: bool` with result of `bool(bool(' '))`
# type: bool
result_d = ...

# Define `result_e: bool` with result of `bool(str(''))`
# type: bool
result_e = ...