6.3. Conditional Expressions

6.3.1. Complex expressions

6.3.1.1. and

Listing 84. Inside joke (see José Jiménez)
firstname = 'Mark'
lastname = 'Watney'

if firstname == 'Mark' and lastname == 'Watney':
    print('Hello Space Pirate')
else:
    print('Sorry, astronauts only')
# Hello Space Pirate

6.3.1.2. or

name = 'Watney'

if name == 'Watney' or name == 'Twardowski':
    print('Hello astronaut')
else:
    print('Sorry, astronauts only')
# Hello astronaut

6.3.1.3. and and or

  • Use parenthesis for explicit order

firstname = 'José'
lastname = 'Jiménez'

if (firstname == 'Mark' and lastname == 'Watney') \
        or (firstname == 'Jan' and lastname == 'Twardowski') \
        or (firstname == 'Melissa' and lastname == 'Lewis'):
    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

  • in checks whether value is in sequence

  • works with tuple, dict, list, set, str

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

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

text = 'Monty Python'

if 'Python' in text:
    print('Yes')
else:
    print('No')
# Yes
crew = ['Watney', 'Twardowski', 'Lewis']

if 'Jiménez' in crew:
    print('Yes')
else:
    print('No')
# No
crew = {'Watney', 'Twardowski', 'Lewis'}

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

6.3.3.2. is

name = None

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

6.3.3.3. not

  • not negates (logically inverts) condition

name = None

if not name:
    print('Name is empty')
crew = {'Watney', 'Twardowski', 'Lewis'}

if 'Ivanovich' not in crew:
    print('You are not assigned to the crew')
# You are not assigned to the crew
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 or XXX/YYY format

  4. User will not input invalid data

  5. Data format:

    • XXX: int systolic pressure

    • YY: int or YYY: int diastolic pressure

  6. Print status of given blood pressure

  7. 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 lub XXX/YYY

  4. Użytkownik nie będzie wprowadzał danych niepoprawnych

  5. Format danych:

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

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

  6. Wypisz status wprowadzonego ciśnienia krwi

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

  8. (z gwiazdką)

Table 11. 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

Input
'119/79': 'Normal',
'120/80': 'Hypertension stage 1',
'121/79': 'Elevated',
'120/81': 'Hypertension stage 1',
'130/80': 'Hypertension stage 1',
'130/89': 'Hypertension stage 1',
'140/85': 'Hypertension stage 2',
'140/89': 'Hypertension stage 2',
'141/90': 'Hypertension stage 2',
'141/91': 'Hypertension stage 2',
'180/120': ('Hypertension stage 2', 'Hypertensive Crisis')
The whys and wherefores
  • Reading user input

  • Type casting

  • Conditional statements

  • Composite conditional statements

  • Checking for corner cases

  • Defining constants and variables