5.4. Conditional Expressions

5.4.1. Conjunction

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

Complex conditions:

>>> # doctest: +SKIP
... for line in file:
...    if line and (not line.startswith('#') or not line.isspace()):
...        ...
>>> # doctest: +SKIP
... 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')
Name is empty

5.4.6. Negation

  • not negates (logically inverts) condition

    >>> name = None
    >>>
    >>> if not name:
    ...    print('Name is empty')
    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

Code 5.3. Solution
"""
* Assignment: Conditional Expression
* Complexity: medium
* Lines of code: 10 lines
* Time: 13 min

English:
    1. Use data from "Given" section (see below)
    2. Table contains Blood Pressure classification according to American Heart Association [1]
    3. User inputs blood pressure in `XXX/YY` or `XXX/YYY` format
    4. User will not try to input invalid data
    5. Data format:
        a. `XXX: int` systolic pressure
        b. `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 "Given" (patrz poniżej)
    2. Tabela zawiera klasyfikację ciśnienia krwi wg American Heart Association [1]
    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:
        a. `XXX: int` to wartość ciśnienia skurczowego (ang. *systolic*)
        b. `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

References:
    [1] Whelton, Paul K. and et al.
        2017 ACC/AHA/AAPA/ABC/ACPM/AGS/APhA/ASH/ASPC/NMA/PCNA Guideline for the
        Prevention, Detection, Evaluation, and Management of High Blood Pressure
        in Adults: Executive Summary: A Report of the American College of
        Cardiology/American Heart Association Task Force on Clinical Practice Guidelines.
        Journal of Hypertension. vol 71. pages 1269–1324. 2018. doi: 10.1161/HYP.0000000000000066

Tests:
    TODO: Doctests
    >>> type(result)
    <class 'list'>
    >>> len(result) in (1, 2)
    True
    >>>
    >>> if len(result) == 2:
    ...     (STATUS_HYPERTENSIVE_CRISIS in result
    ...      and STATUS_HYPERTENSION_STAGE_2 in result)
    ... elif len(result) == 1:
    ...     (STATUS_NORMAL in result
    ...      or STATUS_ELEVATED in result
    ...      or STATUS_HYPERTENSION_STAGE_1 in result
    ...      or STATUS_HYPERTENSION_STAGE_2 in result
    ...      or STATUS_HYPERTENSIVE_CRISIS in result)
    ... else:
    ...     raise ValueError
    True
    >>> assert blood_pressure == '119/79' and result == ['Normal'] or True
    >>> assert blood_pressure == '120/80' and result == ['Hypertension stage 1'] or True
    >>> assert blood_pressure == '121/79' and result == ['Elevated'] or True
    >>> assert blood_pressure == '120/81' and result == ['Hypertension stage 1'] or True
    >>> assert blood_pressure == '130/80' and result == ['Hypertension stage 1'] or True
    >>> assert blood_pressure == '130/89' and result == ['Hypertension stage 1'] or True
    >>> assert blood_pressure == '140/85' and result == ['Hypertension stage 2'] or True
    >>> assert blood_pressure == '140/89' and result == ['Hypertension stage 2'] or True
    >>> assert blood_pressure == '141/90' and result == ['Hypertension stage 2'] or True
    >>> assert blood_pressure == '141/91' and result == ['Hypertension stage 2'] or True
    >>> assert blood_pressure == '180/120' and result == ['Hypertension stage 2', 'Hypertensive Crisis'] or True
"""


# Given
STATUS_NORMAL = 'Normal'
STATUS_ELEVATED = 'Elevated'
STATUS_HYPERTENSION_STAGE_1 = 'Hypertension stage 1'
STATUS_HYPERTENSION_STAGE_2 = 'Hypertension stage 2'
STATUS_HYPERTENSIVE_CRISIS = 'Hypertensive Crisis'

blood_pressure = input('What is your Blood Pressure?: ')
systolic, diastolic = blood_pressure.strip().split('/')
systolic = int(systolic)
diastolic = int(diastolic)

result = []

"""
| 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   |
"""