2. Composite statements

2.1. Complex expressions

2.1.1. and

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

if first_name == 'José' and last_name == 'Jiménez':
    print('My name... José Jiménez')
else:
    print('Your name is different')

2.1.2. or

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

if first_name == 'José' or first_name == 'Max':
    print('Your name is José or Max')
else:
    print('Your name is different')

2.1.3. and and or

  • Use parenthesis for explicit order

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

if (first_name == 'José' and last_name == 'Jiménez')
        or (first_name == 'Mark' and last_name == 'Watney'):
    print('Your name is José Jiménez or Mark Watney')
else:
    print('Your name is different')

2.2. Good practice

2.2.1. Complex conditions

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

2.2.2. Defining exit conditions first

for line in file:
    if line.startswith('#'):
        continue

    if line.isspace():
        continue

    ...

2.3. Control Statements

2.3.1. in with tuple, dict, list, set

  • in checks whether value is in collection

  • works with tuple, dict, list, set

usernames = {'José Jiménez', 'Jan Twardowski', 'Mark Watney'}

if 'José Jiménez' in usernames:
    print(True)
else:
    print(False)

2.3.2. in with str

  • in checks whether str is a part of another str

text = 'My name... José Jiménez'

if 'José' in text:
    print(True)
else:
    print(False)

2.3.3. not

  • not negates (logically inverts) condition

name = None

if not name:
    print('Name is not defined')
usernames = {'José', 'Max', 'Иван'}

if 'José' not in usernames:
    print('Not found')

2.3.4. is

name = None

if name is None:
    print('Name is not defined')
name = None

if name is not None:
    print(name)

2.4. Assignments

2.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 poniżej)

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