3.3. Type bool

3.3.1. Type Definition

  • True - Positive value

  • False - Negative value

  • First letter capitalized, other are lower cased

Listing 24. bool Type Definition
my_var = True               # True
my_var = False              # False

3.3.2. Type Casting

Listing 25. bool() converts argument to bool
bool(1)                     # True
bool(1.0)                   # True
bool('Jan Twardowski')      # True
Listing 26. Negative values
bool(bool())                # False
bool(False)                 # False
bool(None)                  # False
bool(int())                 # False
bool(0)                     # False
bool(float())               # False
bool(0.0)                   # False
bool(complex())             # False
bool(0+0j)                  # False
bool(0.0+0.0j)              # False
bool(str())                 # False
bool('')                    # False
bool(tuple())               # False
bool(())                    # False
bool(list())                # False
bool([])                    # False
bool(dict())                # False
bool({})                    # False
bool(set())                 # False

3.3.3. Boolean Algebra

3.3.3.1. Conjunction

True and True               # True
True and False              # False
False and True              # False
False and False             # False
1 and 1                     # True
1 and 0                     # False
0 and 1                     # False
0 and 0                     # False
'Jan' and 'Jan'             # True
'Jan' and ''                # False
'' and 'Jan'                # False
'' and ''                   # False
'Jan' and 1                 # True
'Jan' and 0                 # False
0.0 and 'Jan'               # False
1 and False                 # False

3.3.3.2. Disjunction

True or True                # True
True or False               # True
False or True               # True
False or False              # False
1 or 1                      # True
1 or 0                      # True
0 or 1                      # True
0 or 0                      # False
'José' or 'Иван'            # True
'José' or ''                # True
'' or 'José'                # True
'' or ''                    # False
1 or 'Иван'                 # True
True or ''                  # True
0 or True                   # True
0.0 or False                # False

3.3.3.3. Complex Statements

True and True or False      # True
True and False or False     # False
False and False or True     # True

3.3.4. Assignments

3.3.4.1. To bool or not to bool

  • Complexity level: easy

  • Lines of code to write: 15 lines

  • Estimated time of completion: 10 min

  • Filename: solution/bool_simple.py

English
  1. Which variables are True?

  2. Which variables are False?

Polish
  1. Które zmienne są True?

  2. Które zmienne są False?

Input
a = bool(False)
b = bool(True)

c = bool('a')
d = bool('.')
e = bool('0')
f = bool('0.0')
g = bool('')
h = bool(' ')

i = bool(0)
j = bool(0.0)
k = bool(-0)
l = bool(-0.0)

m = bool(int('0'))
n = bool(float('-0'))

o = bool(-0.0+0.0j)
p = bool('-0.0+0.0j')
The whys and wherefores
  • Defining variables

  • Type casting

  • Logic types

3.3.4.2. True of False

English
  1. What you need to put in expressions to get the expected outcome?

  2. Insert only True or False

Polish
  1. Co należy podstawić w wyrażeniach aby otrzymać wartość oczekiwaną?

  2. Wstawiaj tylko True lub False

Input
a = bool(...) == True                   # True
b = bool(...) == False                  # True
c = ... == True                         # True
d = ... != False                        # True
e = ... or ...                          # True
f = ... and ...                         # False
g = bool(bool(...) == False) or False   # True
h = bool(...) is not bool(...)          # False
Output
print(bool(a))                          # True
print(bool(b))                          # True
print(bool(c))                          # True
print(bool(d))                          # True
print(bool(e))                          # True
print(bool(f))                          # False
print(bool(g))                          # True
print(bool(h))                          # False
The whys and wherefores
  • Defining variables

  • Type casting

  • Logic types