6.3. Conditional Expressions

6.3.1. Complex expressions

6.3.1.1. and

Listing 83. Inside joke (see José Jiménez)
first_name = 'José'
last_name = 'Jiménez'

if first_name == 'José' and last_name == 'Jiménez':
    print('My name... José Jiménez')
else:
    print("I don't know this catchphrase")

# My name... José Jiménez

6.3.1.2. or

name = 'Jan'

if name == 'Jan' or name == 'Melissa':
    print('Hello astronaut')
else:
    print('Sorry, astronauts only')

# Hello astronaut

6.3.1.3. and and or

  • Use parenthesis for explicit order

first_name = 'José'
last_name = 'Jiménez'

if (first_name == 'Jan' and last_name == 'Twardowski')
        or (first_name == 'Mark' and last_name == 'Watney'):
    print('Hello astronaut')
else:
    print('Sorry, astronauts only')

# Sorry, astronauts only

6.3.2. Good practice

6.3.2.1. Complex conditions

for line in file:
    if line and not line.startswith('#') or not line.isspace():
        ...

6.3.2.2. Defining exit conditions first

for line in file:
    line = line.strip()

    if line.startswith('#'):
        continue

    if line.isspace():
        continue

    ...

6.3.3. Control Statements

6.3.3.1. in with tuple, dict, list, set

  • in checks whether value is in collection

  • works with tuple, dict, list, set

  • Checking if something in set - O(1) Contains

  • Checking if something in list - O(n) Contains

crew = {'Jan Twardowski', 'Mark Watney', 'Melissa Lewis'}

if 'José Jiménez' in crew:
    print('Yes')
else:
    print('No')

# No

6.3.3.2. in with str

  • in checks whether str is a part of another str

text = 'Monty Python'

if 'Python' in text:
    print('Yes')
else:
    print('No')

# Yes

6.3.3.3. not

  • not negates (logically inverts) condition

crew = {'José', 'Max', 'Иван'}

if 'Jan' not in crew:
    print('You are not an astronaut')

# You are not an astronaut
name = None

if not name:
    print('Name is empty')

6.3.3.4. is

name = None

if name is None:
    print('Name is empty')
name = None

if name is not None:
    print(name)

6.3.4. Assignments

6.3.4.1. Classification of blood pressure in adults

English
  1. Table contains classification of input data (see below)

  2. Blood Pressure classification according to American Heart Association

  3. User inputs blood pressure in XXX/YY format, where:

    • XXX: int systolic pressure

    • YY: int diastolic pressure

  4. Print status of given blood pressure

  5. If systolic and diastolic values are in different categories, assume worst case

Polish
  1. Tabela zawiera klasyfikację danych wejściowych (patrz sekcja input)

  2. Klasyfikacja ciśnienia krwi wg. American Heart Association

  3. Użytkownik wprowadza ciśnienie krwi w formacie XXX/YY, gdzie:

    • XXX: int to wartość ciśnienia skurczowego (ang. systolic)

    • YY: int to wartość ciśnienia rozkurczowego (ang. diastolic)

  4. Wypisz status wprowadzonego ciśnienia krwi

  5. Gdy wartości ciśnienia skurczowego i rozkurczowego należą do różnych kategorii, przyjmij gorszy przypadek

Table 10. Classification of blood pressure in adults [Weal18]

Blood Pressure Category

Systolic [mm Hg]

Operator

Diastolic [mm Hg]

Normal

Less than 120

and

Less than 80

Elevated

120-129

and

Less than 80

Hypertension stage 1

130-139

or

80-89

Hypertension stage 2

140 or higher

or

90 or higher

Hypertensive Crisis

Higher than 180

and/or

Higher than 120

The whys and wherefores
  • Reading user input

  • Type casting

  • Conditional statements

  • Composite conditional statements

  • Checking for corner cases

  • Defining constants and variables