4.1. Logic Bool

  • True - Positive value

  • False - Negative value

  • First letter capitalized, other are lower cased

  • Builtin function bool() converts argument to bool

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. Type Conversion

  • 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('Mark Watney')
True

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

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

4.1.5. Negation

~1 -> 0
~0 -> 1
>>> not True
False
>>> not False
True

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

4.1.7. Use Case - 0x01

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

4.1.8. Use Case - 0x02

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

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

4.1.10. Use Case - 0x01

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

4.1.11. Use Case - 0x02

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

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

4.1.13. Use Case - 0x01

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

4.1.14. Use Case - 0x02

>>> firstname = 'Mark'
>>> lastname = 'Watney'
>>>
>>>
>>> firstname == 'Mark' and lastname == 'Watney'
True
>>>
>>> firstname == 'Melissa' and lastname == 'Lewis'
False
>>>
>>> firstname == 'Rick' and lastname == 'Martinez'
False
>>>
>>> True or False or False
True

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

4.1.16. Assignments

"""
* Assignment: Type Bool True or False
* Type: class assignment
* Complexity: easy
* Lines of code: 14 lines
* Time: 8 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
    >>> from pprint import pprint

    >>> assert a is not Ellipsis, 'Assign your result to variable `a`'
    >>> assert b is not Ellipsis, 'Assign your result to variable `b`'
    >>> assert c is not Ellipsis, 'Assign your result to variable `c`'
    >>> assert d is not Ellipsis, 'Assign your result to variable `d`'
    >>> assert e is not Ellipsis, 'Assign your result to variable `e`'
    >>> assert f is not Ellipsis, 'Assign your result to variable `f`'
    >>> assert g is not Ellipsis, 'Assign your result to variable `g`'
    >>> assert h is not Ellipsis, 'Assign your result to variable `h`'
    >>> assert i is not Ellipsis, 'Assign your result to variable `i`'
    >>> assert j is not Ellipsis, 'Assign your result to variable `j`'
    >>> assert k is not Ellipsis, 'Assign your result to variable `k`'
    >>> assert l is not Ellipsis, 'Assign your result to variable `l`'
    >>> assert m is not Ellipsis, 'Assign your result to variable `m`'
    >>> assert n is not Ellipsis, 'Assign your result to variable `n`'
    >>> 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'

    >>> pprint(a)
    True
    >>> pprint(b)
    False
    >>> pprint(c)
    True
    >>> pprint(d)
    False
    >>> pprint(e)
    True
    >>> pprint(f)
    False
    >>> pprint(g)
    True
    >>> pprint(h)
    False
    >>> pprint(i)
    True
    >>> pprint(j)
    True
    >>> pprint(k)
    False
    >>> pprint(l)
    False
    >>> pprint(m)
    True
    >>> pprint(n)
    False
"""

# Result of bool(True)
# type: bool
example = True

# Result of bool(True)
# type: bool
a = ...

# Result of bool(False)
# type: bool
b = ...

# Result of bool(1)
# type: bool
c = ...

# Result of bool(0)
# type: bool
d = ...

# Result of bool(-1)
# type: bool
e = ...

# Result of bool(0.0)
# type: bool
f = ...

# Result of bool('a')
# type: bool
g = ...

# Result of bool('')
# type: bool
h = ...

# Result of bool(' ')
# type: bool
i = ...

# Result of bool('0')
# type: bool
j = ...

# Result of bool(int('0'))
# type: bool
k = ...

# Result of bool(-0.0+0.0j)
# type: bool
l = ...

# Result of bool('-0.0+0.0j')
# type: bool
m = ...

# Result of bool(complex('-0.0+0.0j'))
# type: bool
n = ...

"""
* Assignment: Type Bool Simple
* Type: class assignment
* Complexity: easy
* Lines of code: 9 lines
* Time: 8 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
    >>> from pprint import pprint

    >>> assert a is not Ellipsis, 'Assign your result to variable `a`'
    >>> assert b is not Ellipsis, 'Assign your result to variable `b`'
    >>> assert c is not Ellipsis, 'Assign your result to variable `c`'
    >>> assert d is not Ellipsis, 'Assign your result to variable `d`'
    >>> assert e is not Ellipsis, 'Assign your result to variable `e`'
    >>> assert f is not Ellipsis, 'Assign your result to variable `f`'
    >>> assert g is not Ellipsis, 'Assign your result to variable `g`'
    >>> assert h is not Ellipsis, 'Assign your result to variable `h`'
    >>> assert i is not Ellipsis, 'Assign your 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'

    >>> result = True is a
    >>> pprint(result)
    True

    >>> result = True is not b
    >>> pprint(result)
    True

    >>> result = not c
    >>> pprint(result)
    True

    >>> result = bool(d) == True
    >>> pprint(result)
    True

    >>> result = bool(e) == False
    >>> pprint(result)
    True

    >>> result = f or False
    >>> pprint(result)
    False

    >>> result = True and g
    >>> pprint(result)
    True

    >>> result = bool(bool(h) == h) or False
    >>> pprint(result)
    True

    >>> result = bool(i) is not bool(False)
    >>> pprint(result)
    False
"""

# Result of `not ...` must be True
# type: bool
example = False

# Result of `True is ...` must be True
# type: bool
a = ...

# Result of `True is not ...` must be True
# type: bool
b = ...

# Result of `not ...` must be True
# type: bool
c = ...

# Result of `bool(...) == True` must be True
# type: bool
d = ...

# Result of `bool(...) == False` must be True
# type: bool
e = ...

# Result of `... or False` must be False
# type: bool
f = ...

# Result of `True and ...` must be True
# type: bool
g = ...

# Result of `bool(bool(...) == ...) or False` must be True
# type: bool
h = ...

# Result of `bool(...) is not bool(False)` must be False
# type: bool
i = ...