2. Logical Types

2.1. bool

  • True - Positive value
  • False - Negative value
  • First letter capitalized, other are lower cased

2.1.1. Defining bool

my_var = True
my_var = False

2.1.2. Converting to bool

  • Also known as “type casting”
  • float() converts argument to float
bool(1)               # True
bool(0)               # False
bool(1.0)             # True
bool(0.0)             # False
bool('José')          # True
bool('')              # False

2.1.3. Negative values

  • False
  • 0
  • 0.0
  • () - empty tuple
  • {} - empty dict
  • [] - empty list
  • '' - empty str
  • set() - empty set
  • None

2.1.4. Using and

1 and 1               # True
1 and 0               # False
0 and 0               # False
'José' and 'Иван'     # True
'José' and ''         # False
'' and ''             # False

2.1.5. Using or

1 or 1                # True
1 or 0                # True
0 or 0                # False
'José' or 'Иван'      # True
'José' or ''          # True
'' or ''              # False

2.2. None

  • First letter capitalized, other are lower cased
  • Empty value (null)
  • It is not False value
  • With if statements behaves like negative values
  • Used for unknown (unset) values

2.2.1. Defining None

my_var = None

2.3. Logic operators

Tab. 2.3. 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
x is y x is None x is the same object as y
x is not y x is not None x is not the same object as y

2.4. Assignments

2.4.1. To bool or not to bool

  1. Wprowadzono zmienne:

    a = False
    b = True
    c = None
    
    c = 'a'
    d = '.'
    e = '0'
    f = '0.0'
    g = ''
    h = ' '
    
    j = 0
    k = 0.0
    l = -0
    m = -0.0+0.0j
    
    n = int('0')
    o = float('-0')
    
  2. Które zmienne mają wartość True, None, False?

  3. Czym się różni None od False?

About:
  • Filename: types_bool.py
  • Lines of code to write: 15 lines
  • Estimated time of completion: 10 min
The whys and wherefores:
 
  • Definiowanie zmiennych
  • Konwersja typów
  • Typy logiczne

2.4.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 = ... is None                                # True
    i = ... is not None                            # False
    j = 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
    print(h)  # True
    print(i)  # False
    print(j)  # False
    
  2. Zadanie dla chętnych:

    k = bool(bool(...) is not bool(...)) == False  # True
    l = (bool(bool(...) is not bool(...)) == False and bool(...))   # False
    m = (bool(bool(...) is not bool(...)) == False and bool(...)) and (... is not None)   # False
    
    print(k)  # True
    print(l)  # False
    print(m)  # False
    
About:
  • Filename: types_bool_or_none.py
  • Lines of code to write: 10 lines
  • Estimated time of completion: 5 min
The whys and wherefores:
 
  • Definiowanie zmiennych
  • Konwersja typów
  • Typy logiczne