3. bool

3.1. Defining bool

  • True - Positive value

  • False - Negative value

  • First letter capitalized, other are lower cased

my_var = True               # True
my_var = False              # False

3.2. Converting to bool

  • Also known as "type casting"

  • bool() converts argument to bool

bool(1)                     # True
bool(0)                     # False
bool(1.0)                   # True
bool(0.0)                   # False
bool('Jan Twardowski')      # True
bool('')                    # False

3.3. Negative values

  • False

  • None

  • 0

  • 0.0

  • 0+0j

  • 0.0+0.0j

  • empty str() or ''

  • empty tuple() or ()

  • empty dict() or {}

  • empty list() or []

  • empty set()

3.4. Boolean logic

3.4.1. Using and

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.4.2. Using or

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.4.3. Using both: or and and

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

3.5. Logic operators

Table 10. Logic operators

Operand

Example

Description

x < y

x < 18

value of x is less than y

x <= y

x <= 18

value of x is less or equal y

x > y

x > 18

value of x is greater than y

x >= y

x >= 18

value of x is greater or equal than y

x == y

x == 18

value of x is equal to y

x != y

x != 18

value of x is not equal to y

3.6. Assignments

3.6.1. To bool or not to bool

  1. Wprowadzono zmienne:

    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+0.0j)
    m = bool('-0.0+0.0j')
    
    n = bool(int('0'))
    o = bool(float('-0'))
    
  2. Które zmienne mają wartość True, False?

The whys and wherefores
  • Definiowanie zmiennych

  • Konwersja typów

  • Typy logiczne

3.6.2. True of False

  1. Co należy podstawić do zmiennych aby wyrażenia poniżej zgadzały się z wartością oczekiwaną?

    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
    
    print(a)                                # True
    print(b)                                # True
    print(c)                                # True
    print(d)                                # True
    print(e)                                # True
    print(f)                                # False
    print(g)                                # True
    
The whys and wherefores
  • Definiowanie zmiennych

  • Konwersja typów

  • Typy logiczne