2.3. Type Bool

2.3.1. Definition

  • True - Positive value

  • False - Negative value

  • First letter capitalized, other are lower cased

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

2.3.2. Type Casting

  • Builtin function bool() converts argument to bool

>>> bool(True)
True
>>> bool(False)
False
>>> bool(1)
True
>>> bool(0)
False
>>> bool(1)
True
>>> bool(2)
True
>>> bool(3)
True
>>> bool(-1)
True
>>> bool(-2)
True
>>> bool(-3)
True
>>> bool(1.0)
True
>>> bool('Jan Twardowski')
True

Negative values

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

2.3.3. Comparison

  • x < y - Less than

  • x <= y - Less or equal

  • x > y - Greater than

  • x >= y - Greater or equal

  • == - Equals

  • != - Not Equals

>>> 10 < 2
False
>>> 10 <= 2
False
>>> 10 > 2
True
>>> 10 >= 2
True
>>> 10 == 2
False
>>> 10 != 2
True
>>> x = 1
>>> y = 2
>>>
>>> x == 1
True
>>> y == 2
True
>>> x == y
False
>>> x != y
True

2.3.4. Conjunction

Definition:

1 & 1 -> 1
1 & 0 -> 0
0 & 1 -> 0
0 & 0 -> 0

Example:

>>> True and True
True
>>> True and False
False
>>> False and True
False
>>> False and False
False

Use Cases:

>>> firstname = 'Mark'
>>> lastname = 'Watney'
>>>
>>> firstname == 'Mark' and lastname == 'Watney'
True
>>> firstname == 'Mark' and lastname == 'Twardowski'
False

2.3.5. Disjunction

Definition:

1 | 1 -> 1
1 | 0 -> 1
0 | 1 -> 1
0 | 0 -> 0

Example:

>>> True or True
True
>>> True or False
True
>>> False or True
True
>>> False or False
False

Use Case:

>>> name = 'Mark Watney'
>>>
>>> name == 'Mark Watney' or name == 'Melissa Lewis'
True

2.3.6. Boolean Algebra

Example:

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

Use Case:

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

2.3.7. Built-in Functions

  • type() - Checks type of an object

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

  • isinstance(a, (x,y)) - If a is instance of x or y

>>> 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.8. Example

>>> import numpy as np
>>>
>>> a = np.array([[1, 2, 3],
...               [4, 5, 6],
...               [7, 8, 9]])
...
>>> a > 2
array([[False, False,  True],
       [ True,  True,  True],
       [ True,  True,  True]])
>>> a < 7
array([[ True,  True,  True],
       [ True,  True,  True],
       [False, False, False]])
>>> a == 9
array([[False, False, False],
       [False, False, False],
       [False, False,  True]])
>>> (a>2) & (a<7) | (a==9)
array([[False, False,  True],
       [ True,  True,  True],
       [False, False,  True]])
>>> a[(a>2) & (a<7) | (a==9)]
array([3, 4, 5, 6, 9])

2.3.9. Assignments

Code 2.17. Solution
"""
* Assignment: Type Bool True or False
* Complexity: easy
* Lines of code: 16 lines
* Time: 5 min

English:
    1. Use data from "Given" section (see below)
    2. Which variables are `True`?
    3. Which variables are `False`?

Polish:
    1. Użyj danych z sekcji "Given" (patrz poniżej)
    2. Które zmienne są `True`?
    3. Które zmienne są `False`?

Tests:
    >>> a
    False
    >>> b
    True
    >>> c
    False
    >>> d
    False
    >>> e
    False
    >>> f
    False
    >>> g
    True
    >>> h
    True
    >>> i
    True
    >>> j
    True
    >>> k
    False
    >>> l
    True
    >>> m
    False
    >>> n
    False
    >>> o
    False
    >>> p
    True
    >>> q
    False
"""

# Given
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')
q = bool(complex('-0.0+0.0j'))

Code 2.18. Solution
"""
* Assignment: Type Bool Simple
* Complexity: easy
* Lines of code: 9 lines
* Time: 5 min

English:
    1. Use data from "Given" section (see below)
    2. What you need to put in expressions to get the expected outcome?
    3. In place of ellipsis (`...`) insert only `True` or `False`
    4. Compare result with "Tests" section (see below)

Polish:
    1. Użyj danych z sekcji "Given" (patrz poniżej)
    2. Co należy podstawić w wyrażeniach aby otrzymać wartość oczekiwaną?
    3. W miejsce trzech kropek (`...`) wstawiaj tylko `True` lub `False`
    4. Porównaj wyniki z sekcją "Tests" (patrz poniżej)

Tests:
    >>> bool(a)
    True
    >>> bool(b)
    True
    >>> bool(c)
    False
    >>> bool(d)
    True
    >>> bool(e)
    True
    >>> bool(f)
    False
    >>> bool(g)
    True
    >>> bool(h)
    True
    >>> bool(i)
    False
"""

# Given
a = True == ...  # True
b = True != ...  # True
c = not ...  # False
d = bool(...) == True  # True
e = bool(...) == False  # True
f = ... or ...  # False
g = ... and ...  # True
h = bool(bool(...) == ...) or False  # True
i = bool(...) is not bool(False)  # False