9.4. Boolean Expression

  • Ternary Operator

  • Conditional Expression

  • Shorthand Expressions

9.4.1. Recap

>>> number = 3
>>>
>>> if number in range(0,10):
...     is_digit = True
... else:
...     is_digit = False
>>>
>>> print(is_digit)
True

9.4.2. SetUp

Simulate user input (for test automation):

>>> from unittest.mock import MagicMock
>>> input = MagicMock(side_effect=['10', '10'])

9.4.3. Conditional Expression

>>> number = 3
>>> is_digit = True if number in range(0,10) else False
>>>
>>> print(is_digit)
True

9.4.4. Shorthand Expressions

>>> number = 3
>>> is_digit = (number in range(0,10))
>>>
>>> print(is_digit)
True

9.4.5. Use Case - 0x01

  • Is adult

>>> age = input('What is your age?: ')   #input: '10'
>>> adult = 'Yes' if int(age) >= 18 else 'No'
>>>
>>> print(adult)
No

9.4.6. Use Case - 0x02

  • Is numeric

>>> age = input('What is your age?: ')   #input: '10'
>>> age = float(age) if age.isnumeric() else None
>>>
>>> print(age)
10.0

9.4.7. Use Case - 0x03

  • Even/odd

>>> number = 3
>>> is_even = (number % 2 == 0 )
>>>
>>> print(is_even)
False

9.4.8. Use Case - 0x04

  • Astronaut/Cosmonaut

>>> country = 'Russia'
>>> job = 'astronaut' if country == 'USA' else 'cosmonaut'
>>>
>>> print(job)
cosmonaut

9.4.9. Use Case - 0x05

  • IPv4/IPv6

>>> ip = '127.0.0.1'
>>> protocol = 'IPv4' if '.' in ip else 'IPv6'
>>>
>>> print(protocol)
IPv4
>>> ip = 'fe80::aede:48ff:fe01:1133'
>>> protocol = 'IPv4' if '.' in ip else 'IPv6'
>>>
>>> print(protocol)
IPv6

9.4.10. Assignments

Code 9.4. Solution
"""
* Assignment: Conditional Expression Underage/Adult
* Type: class assignment
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 min

English:
    1. The user entered his/her age: 30
    2. Define variable `result: str` with value:
        a. 'junior' - if age is below 18
        b. 'senior' - if age is 18 or above
    3. Use conditional expression (`if` one-liner)
    4. Run doctests - all must succeed

Polish:
    1. Użytkownik podał swój wiek: 30
    2. Zdefiniuj zmienną `result: str` z wartością:
       a. 'junior' - jeżeli wiek jest poniżej 18 lat
       b. 'senior' - jeżeli wiek jest 18 lat lub więcej
    3. Wykorzystaj warunkowe wyrażenie (jednolinikowego `if`)
    4. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `<`

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from pprint import pprint

    >>> assert result is not Ellipsis, \
    'Assign your result to variable `result`'

    >>> assert type(result) is str, \
    'Variable `result` has invalid type, should be str'

    >>> assert result in {'junior', 'senior'}, \
    'Variable `result` should be either junior or senior'

    >>> pprint(result)
    'senior'
"""

AGE = 30


# Define variable `result: str` with value:
# - 'junior' - if age is below 18
# - 'senior' - if age is 18 or above
# Use conditional expression (`if` one-liner)
# type: str
result = ...


Code 9.5. Solution
"""
* Assignment: Conditional Expression IPv4/IPv6
* Type: class assignment
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 min

English:
    1. To `result: str` assign whether `IP_ADDRESS` is IPv4 or IPv6 protocol:
       a. `IPv4` if dot `.` is in the IP address
       b. `IPv6` if dot `.` is not in the IP address
    2. Non-functional requirements:
       a. Use one line `if`
    3. Run doctests - all must succeed

Polish:
    1. Użytkownik podał adres IP
    1. Do `result: str` przypisz czy `IP_ADDRESS` jest protokołu IPv4 czy IPv6:
       a. `IPv4` jeżeli jest kropka `.` w adresie IP
       b. `IPv6` jeżeli kropki nie ma
    2. Wymagania niefunkcjonalne:
       a. Wykorzystaj jednolinikowego `if`
    3. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `in`
    * In IPv6 address there is no dot `.`
    * IPv4 example: 127.0.0.1
    * IPv6 example: 2001:0db8:85a3:0000:0000:8a2e:0370:7334

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert result is not Ellipsis, \
    'Assign your result to variable `result`'

    >>> assert type(result) is str, \
    'Variable `result` has invalid type, should be str'

    >>> assert result in ('IPv4', 'IPv6'), \
    'Variable `result` must be either `IPv4` or `IPv6`'
"""

IP_ADDRESS = '127.0.0.1'

# Whether 'IPv4' or 'IPv6'
# type: str
result = ...