5.4. Conditional Expressions

5.4.1. Conjunction

Listing 5.38. 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

5.4.2. Disjunction

name = 'Watney'

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

5.4.3. Boolean Algebra

  • 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
Listing 5.39. Complex conditions
for line in file:
    if line and (not line.startswith('#') or not line.isspace()):
        ...


for line in file:
    if len(line) == 0:
        continue

    if line.startswith('#'):
        continue

    if line.isspace():
        continue

    ...

5.4.4. Contains

text = 'Monty Python'

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

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

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

5.4.5. Identity

name = None

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

5.4.6. Negation

  • not negates (logically inverts) condition

name = None

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

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)

5.4.7. Assignments

5.4.7.1. Conditional Expression

  • Assignment name: Conditional Expression

  • Last update: 2020-10-01

  • Complexity level: medium

  • Lines of code to write: 25 lines

  • Estimated time of completion: 21 min

  • Solution: solution/conditional_expression.py

English
  1. Use data from "Input" section (see below)

  2. Table contains Blood Pressure classification according to American Heart Association [Weal18]

  3. User inputs blood pressure in XXX/YY or XXX/YYY format

  4. User will not try to 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. Użyj danych z sekcji "Input" (patrz poniżej)

  2. Tabela zawiera klasyfikację ciśnienia krwi wg American Heart Association [Weal18]

  3. Użytkownik wprowadza ciśnienie krwi w formacie XXX/YY lub XXX/YYY

  4. Użytkownik nie będzie próbował 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

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