5.4. Conditional Expression¶
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¶
"""
* Assignment: Conditional Expression BloodPressure
* 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:
>>> import sys
>>> sys.tracebacklimit = 0
>>> type(result)
<class 'str'>
>>> result in (STATUS_NORMAL, STATUS_ELEVATED, STATUS_HYPERTENSION_STAGE_1,
... STATUS_HYPERTENSION_STAGE_2, STATUS_HYPERTENSIVE_CRISIS)
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 == 'Hypertensive Crisis' or True
>>> assert blood_pressure == '181/121' and result == 'Hypertensive Crisis' or True
>>> assert blood_pressure == '181/50' and result == 'Hypertensive Crisis' or True
>>> assert blood_pressure == '100/121' and result == 'Hypertensive Crisis' or True
>>> assert blood_pressure == '181/121' and result == 'Hypertensive Crisis' or True
"""
# Mock input() built-in function
from unittest.mock import MagicMock
input = MagicMock(side_effect=['119/79', '120/80', '121/79',
'120/81', '130/80', '130/89',
'140/85', '140/89', '141/90',
'141/91', '180/120', '181/121',
'181/50', '100/121', '181/121'])
# 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 = ... # str: one of the STATUS_*
"""
| 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 |
"""