2.3. Type Bool

2.3.1. Type Definition

  • True - Positive value

  • False - Negative value

  • First letter capitalized, other are lower cased

Listing 2.11. bool Type Definition
data = True               # True
data = False              # False

2.3.2. Type Casting

Listing 2.12. bool() converts argument to bool
bool(1)                     # True
bool(1.0)                   # True
bool('Jan Twardowski')      # True
Listing 2.13. Negative values
bool(int())                 # False
bool(float())               # False
bool(complex())             # False
bool(bool())                # False
bool(str())                 # False
bool(tuple())               # False
bool(list())                # False
bool(dict())                # False
bool(set())                 # False
bool(frozenset())           # False

bool(0)                     # False
bool(0.0)                   # False
bool(0+0j)                  # False
bool(0.0+0.0j)              # False
bool(False)                 # False
bool(None)                  # False
bool('')                    # False
bool(())                    # False
bool([])                    # False
bool({})                    # False

2.3.3. 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

2.3.4. 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

2.3.5. Boolean Algebra

True and True or False      # True
False and False or True     # True
(True and True) or False    # True
True and (True or False)    # True

True and False or False     # False
True and (False or False)   # False
(firstname == 'Mark' and lastname == 'Watney') \
    or (firstname == 'Jan' and lastname == 'Twardowski') \
    or (firstname == 'Melissa' and lastname == 'Lewis')

2.3.6. Built-in Functions

  • type() - Checks type of an object

  • isinstance(a, b) - If a is instance of b

  • isinstance(a, (b,c)) - If a is instance of b or c

type(True)                      # <class 'bool'>
type(False)                     # <class 'bool'>
isinstance(1, bool)             # False
isinstance(1, int)              # True
isinstance(1, float)            # False

isinstance(1.23, bool)          # False
isinstance(1.23, int)           # False
isinstance(1.23, float)         # True

isinstance(True, bool)          # True
isinstance(True, int)           # True
isinstance(True, float)         # False

isinstance(False, bool)         # True
isinstance(False, int)          # True
isinstance(False, float)        # False

2.3.7. Assignments

2.3.7.1. Type Bool True or False

English
  1. Use data from "Input" section (see below)

  2. Which variables are True?

  3. Which variables are False?

Polish
  1. Użyj danych z sekcji "Input" (patrz poniżej)

  2. Które zmienne są True?

  3. Które zmienne są False?

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

c = bool(0)
d = bool(0.0)
e = bool(-0)
f = bool(-0.0)

g = bool('a')
h = bool('.')
i = bool('0')
j = bool('0.0')
k = bool('')
l = bool(' ')

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

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

  • Type casting

  • Logic types

2.3.7.2. Type Bool Simple

English
  1. Use data from "Input" section (see below)

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

  3. Insert only True or False

  4. Compare result with "Output" section (see below)

Polish
  1. Użyj danych z sekcji "Input" (patrz poniżej)

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

  3. Wstawiaj tylko True lub False

  4. Porównaj wyniki z sekcją "Output" (patrz poniżej)

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