9.3. Conditional Else

  • Unconditional Alternative

  • Optional

  • Executed when condition is not met

Syntax:

>>> 
... if <condition>:
...     <do something>
... else:
...     <do something>

9.3.1. Problem

  • Easy to make an error and put > instead >=

  • This will leave one case not covered (when age == 18)

>>> age = 9
>>>
>>> if age < 18:
...     print('junior')
junior
>>>
>>> if age >= 18:
...     print('senior')

9.3.2. Solution

>>> age = 9
>>>
>>> if age < 18:
...     print('junior')
... else:
...     print('senior')
junior

9.3.3. Conditional Assignment

  • if var=... else: var=...

>>> age = 9
>>>
>>> if age < 18:
...     status = 'junior'
... else:
...     status = 'senior'
>>>
>>> print(status)
junior

9.3.4. Checking If Empty

  • if data is None

>>> data = None
>>>
>>> if data is None:
...     print('yes')
... else:
...     print('no')
yes

9.3.5. Membership

  • if login in admins

>>> admins = ['mwatney', 'mlewis', 'rmartinez']
>>> login = 'avogel'
>>>
>>> if login in admins:
...     print('You can modify content')
... else:
...     print('Access denied')
Access denied

9.3.6. Use Case - 1

>>> number = 5
>>>
>>> if number % 2 == 0:
...     print('even')
... else:
...     print('odd')
odd

9.3.7. Use Case - 2

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

9.3.8. Use Case - 3

>>> data = [True, False, True]
>>>
>>> if any(data):
...     print('yes')
... else:
...     print('no')
...
yes

9.3.9. Use Case - 4

>>> data = [True, False, True]
>>>
>>> if all(data):
...     print('yes')
... else:
...     print('no')
...
no

9.3.10. Assignments

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Conditional Else Adult
# - Difficulty: easy
# - Lines: 4
# - Minutes: 3

# %% English
# 1. The user entered his/her age: 30
# 2. Define variable `result: str` with value:
#    - 'junior' - if age is below 18
#    - 'senior' - if age is 18 or above
# 3. Use if-else block
# 4. Run doctests - all must succeed

# %% Polish
# 1. Użytkownik podał swój wiek: 30
# 2. Zdefiniuj zmienną `result: str` z wartością:
#    - 'junior' - jeżeli wiek jest poniżej 18 lat
#    - 'senior' - jeżeli wiek jest 18 lat lub więcej
# 3. Użyj bloku if-else
# 4. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - `<`

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> 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 if-else block
# type: str
...


# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Conditional Else IPv4/IPv6
# - Difficulty: easy
# - Lines: 4
# - Minutes: 3

# %% English
# 1. To `result: str` assign whether `IP_ADDRESS` is IPv4 or IPv6 protocol:
#    - `IPv4` if dot `.` is in the IP address
#    - `IPv6` if dot `.` is not in the IP address
# 2. Run doctests - all must succeed

# %% Polish
# 1. Do `result: str` przypisz czy `IP_ADDRESS` jest protokołu IPv4 czy IPv6:
#    - `IPv4` jeżeli jest kropka `.` w adresie IP
#    - `IPv6` jeżeli kropki nie ma
# 2. 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 sys.version_info >= (3, 9), \
'Python 3.9+ required'

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