6.5. Boolean Expression

6.5.1. Rationale

  • Conditional Expression

  • Shorthand Expressions

6.5.2. Recap

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

6.5.3. Conditional Expression

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

6.5.4. Shorthand Expressions

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

6.5.5. Use Case - 0x01

  • Is Numeric

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

6.5.6. Use Case - 0x02

  • Even/Odd

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

6.5.7. Use Case - 0x03

  • Astronaut/Cosmonaut

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

6.5.8. Use Case - 0x04

  • IPv4/IPv6

>>> ip = '127.0.0.1'
>>>
>>> if '.' in ip:
...     protocol = 'IPv4'
... else:
...     protocol = 'IPv6'
>>>
>>> print(protocol)
IPv4
>>> ip = '127.0.0.1'
>>> protocol = 'IPv4' if '.' in ip else 'IPv6'
>>>
>>> print(protocol)
IPv4

6.5.9. Use Case - 0x05

>>> if age >= 18:
...     result = 'adult'
... else:
...     result = 'kiddo'
>>>
>>> print(result)
kiddo
>>> result = 'adult' if age >= 18 else 'kiddo'
>>>
>>> print(result)
kiddo

6.5.10. Assignments

Code 6.3. Solution
"""
* Assignment: Conditional Expression Underage/Adult
* Required: yes
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 min

English:
    1. Define `result: str` with:
       a. 'underage' if user age is less than 18
       b. 'adult' if user age is equal or greater than 18
    2. Non-functional requirements:
       a. Use one line `if`
    3. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `result: str` z:
       a. 'underage' jeżeli wiek użytkownika jest mniejszy niż 18
       b. 'adult' jeżeli wiek użytkownika jest równy lub większy 18
    2. Wymagania niefunkcjonalne:
       a. Wykorzystaj jednolinikowego `if`
    3. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `>=`

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

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

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

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

ADULT = 18
AGE = 12

# str: Whether 'adult' or 'underage'
result = ...

Code 6.4. Solution
"""
* Assignment: Conditional Expression IPv4/IPv6
* Required: yes
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 min

English:
    1. To `result: bool` 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. Do `result: bool` 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`

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

    >>> assert result is not Ellipsis, \
    'Assign 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'

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