3.1. Logic Bool

3.1.1. Definition

  • True - Positive value

  • False - Negative value

  • First letter capitalized, other are lower cased

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

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

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

3.1.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'
>>> lastname = 'Watney'
>>>
>>> firstname == 'Mark' and lastname == 'Twardowski'
False
>>> firstname = 'Mark'
>>> lastname = 'Watney'
>>>
>>> firstname == 'Mark'
True
>>> lastname == 'Watney'
True
>>> lastname == 'Twardowski'
False
>>> True and True
True
>>> True and False
False

3.1.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
>>> name = 'Mark Watney'
>>>
>>> name == 'Mark Watney'
True
>>> name == 'Melissa Lewis'
False
>>> True or False
True

3.1.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
>>> firstname = 'Mark'
>>> lastname = 'Watney'
>>>
>>> firstname == 'Mark' and lastname == 'Watney'
True
>>> firstname == 'Jan' and lastname == 'Twardowski'
False
>>> firstname == 'Melissa' and lastname == 'Lewis'
False
>>> True or False or False
True

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

3.1.8. Assignments

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

English:
    1. Which variables are `True`?
    2. Which variables are `False`?
    3. Run doctests - all must succeed

Polish:
    1. Które zmienne są `True`?
    2. Które zmienne są `False`?
    3. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert a is not Ellipsis, 'Assign result to variable: `a`'
    >>> assert b is not Ellipsis, 'Assign result to variable: `b`'
    >>> assert c is not Ellipsis, 'Assign result to variable: `c`'
    >>> assert d is not Ellipsis, 'Assign result to variable: `d`'
    >>> assert e is not Ellipsis, 'Assign result to variable: `e`'
    >>> assert f is not Ellipsis, 'Assign result to variable: `f`'
    >>> assert g is not Ellipsis, 'Assign result to variable: `g`'
    >>> assert h is not Ellipsis, 'Assign result to variable: `h`'
    >>> assert i is not Ellipsis, 'Assign result to variable: `i`'
    >>> assert j is not Ellipsis, 'Assign result to variable: `j`'
    >>> assert k is not Ellipsis, 'Assign result to variable: `k`'
    >>> assert l is not Ellipsis, 'Assign result to variable: `l`'
    >>> assert m is not Ellipsis, 'Assign result to variable: `m`'
    >>> assert n is not Ellipsis, 'Assign result to variable: `n`'
    >>> assert o is not Ellipsis, 'Assign result to variable: `o`'
    >>> assert n is not Ellipsis, 'Assign result to variable: `n`'
    >>> assert p is not Ellipsis, 'Assign result to variable: `p`'
    >>> assert q is not Ellipsis, 'Assign result to variable: `q`'
    >>> assert type(a) is bool, 'Variable `a` has invalid type, should be bool'
    >>> assert type(b) is bool, 'Variable `b` has invalid type, should be bool'
    >>> assert type(c) is bool, 'Variable `c` has invalid type, should be bool'
    >>> assert type(d) is bool, 'Variable `d` has invalid type, should be bool'
    >>> assert type(e) is bool, 'Variable `e` has invalid type, should be bool'
    >>> assert type(f) is bool, 'Variable `f` has invalid type, should be bool'
    >>> assert type(g) is bool, 'Variable `g` has invalid type, should be bool'
    >>> assert type(h) is bool, 'Variable `h` has invalid type, should be bool'
    >>> assert type(i) is bool, 'Variable `i` has invalid type, should be bool'
    >>> assert type(j) is bool, 'Variable `j` has invalid type, should be bool'
    >>> assert type(k) is bool, 'Variable `k` has invalid type, should be bool'
    >>> assert type(l) is bool, 'Variable `l` has invalid type, should be bool'
    >>> assert type(m) is bool, 'Variable `m` has invalid type, should be bool'
    >>> assert type(n) is bool, 'Variable `n` has invalid type, should be bool'
    >>> assert type(o) is bool, 'Variable `o` has invalid type, should be bool'
    >>> assert type(p) is bool, 'Variable `p` has invalid type, should be bool'
    >>> assert type(q) is bool, 'Variable `q` has invalid type, should be bool'

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

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 3.2. Solution
"""
* Assignment: Type Bool Simple
* Required: yes
* Complexity: easy
* Lines of code: 9 lines
* Time: 5 min

English:
    1. What you need to put in expressions to get the expected outcome?
    2. In place of ellipsis (`...`) insert only `True` or `False`
    3. Run doctests - all must succeed

Polish:
    1. Co należy podstawić w wyrażeniach aby otrzymać wartość oczekiwaną?
    2. W miejsce trzech kropek (`...`) wstawiaj tylko `True` lub `False`
    3. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert a is not Ellipsis, 'Assign result to variable: `a`'
    >>> assert b is not Ellipsis, 'Assign result to variable: `b`'
    >>> assert c is not Ellipsis, 'Assign result to variable: `c`'
    >>> assert d is not Ellipsis, 'Assign result to variable: `d`'
    >>> assert e is not Ellipsis, 'Assign result to variable: `e`'
    >>> assert f is not Ellipsis, 'Assign result to variable: `f`'
    >>> assert g is not Ellipsis, 'Assign result to variable: `g`'
    >>> assert h is not Ellipsis, 'Assign result to variable: `h`'
    >>> assert i is not Ellipsis, 'Assign result to variable: `i`'
    >>> assert type(a) is bool, 'Variable `a` has invalid type, should be bool'
    >>> assert type(b) is bool, 'Variable `b` has invalid type, should be bool'
    >>> assert type(c) is bool, 'Variable `c` has invalid type, should be bool'
    >>> assert type(d) is bool, 'Variable `d` has invalid type, should be bool'
    >>> assert type(e) is bool, 'Variable `e` has invalid type, should be bool'
    >>> assert type(f) is bool, 'Variable `f` has invalid type, should be bool'
    >>> assert type(g) is bool, 'Variable `g` has invalid type, should be bool'
    >>> assert type(h) is bool, 'Variable `h` has invalid type, should be bool'
    >>> assert type(i) is bool, 'Variable `i` has invalid type, should be bool'

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

# bool: the expression result must be True
a = True == ...

# bool: the expression result must be True
b = True != ...

# bool: the expression result must be False
c = not ...

# bool: the expression result must be True
d = bool(...) == True

# bool: the expression result must be True
e = bool(...) == False

# bool: the expression result must be False
f = ... or ...

# bool: the expression result must be True
g = ... and ...

# bool: the expression result must be True
h = bool(bool(...) == ...) or False

# bool: the expression result must be False
i = bool(...) is not bool(False)