11.3. Conditional Else — Python
Unconditional Alternative
Optional
Executed when condition is not met
Syntax:
>>> # ... if <condition>: ... <do something> ... else: ... <do something>
11.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 < 65: ... print('adult') adult
11.3.2. Solution
>>> age = 9 >>> >>> if age < 18: ... print('junior') ... else: ... print('adult') junior
11.3.3. Conditional Assignment
if var=... else: var=...
>>> age = 9 >>> >>> if age < 18: ... status = 'junior' ... else: ... status = 'adult' >>> >>> print(status) junior
11.3.4. Checking If Empty
if data is None
>>> data = None >>> >>> if data is None: ... print('empty') ... else: ... print('full') empty
11.3.5. Membership
if username in database
>>> username = 'mallory' >>> database = {'alice', 'bob', 'carol'} >>> >>> if username in database: ... is_authorized = True ... print('User logged-in') ... else: ... is_authorized = False ... print('Access denied') ... Access denied
11.3.6. Use Case - 1
>>> number = 5 >>> >>> if number % 2 == 0: ... print('even') ... else: ... print('odd') odd
11.3.7. Use Case - 2
>>> country = 'USA' >>> >>> if country == 'USA': ... job = 'astronaut' ... else: ... job = 'cosmonaut' >>> >>> print(job) astronaut
11.3.8. Assignments
# %% About # - Name: Conditional Else Adult # - Difficulty: easy # - Lines: 4 # - Minutes: 3 # %% 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 # %% 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ść # %% Expected # >>> result # 'senior' # %% Hints # - `<` # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> from pprint import pprint >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> assert result is not Ellipsis, \ 'Variable `result` has an invalid value; assign result of your program to it.' >>> assert type(result) is str, \ 'Variable `result` has an invalid type; expected: `str`.' >>> assert result in {'junior', 'senior'}, \ 'Variable `result` should be either junior or senior' >>> pprint(result) 'senior' """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports # %% Types result: str # %% Data AGE = 30 # %% Result
# %% About # - Name: Conditional Else IPv4/IPv6 # - Difficulty: easy # - Lines: 4 # - Minutes: 3 # %% 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 # %% 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ść # %% Expected # >>> result # 'IPv4' # %% 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` # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> assert result is not Ellipsis, \ 'Variable `result` has an invalid value; assign result of your program to it.' >>> assert type(result) is str, \ 'Variable `result` has an invalid type; expected: `str`.' >>> assert result in ('IPv4', 'IPv6'), \ 'Variable `result` must be either `IPv4` or `IPv6`' """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports # %% Types result: str # %% Data IP_ADDRESS = '127.0.0.1' # %% Result
# %% About # - Name: Conditional Else ListContains # - Difficulty: easy # - Lines: 4 # - Minutes: 2 # %% 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 # %% English # 1. If 'mallory' is in `DATA`: # - yes - set variable `result` to True # - no - set variable `result` to False # 2. Use `if-else` block # 3. Run doctests - all must succeed # %% Polish # 1. Jeżeli 'mallory' znajduje się w `DATA`: # - tak - ustaw zmienną `result` na True # - nie - ustaw zmienną `result` na False # 2. Użyj bloku `if-else` # 3. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> result # False # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> assert result is not Ellipsis, \ 'Variable `result` has an invalid value; assign result of your program to it.' >>> assert type(result) is bool, \ 'Variable `result` has an invalid type; expected: `bool`.' >>> from pprint import pprint >>> pprint(result) False """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports # %% Types result: bool # %% Data DATA = {'alice', 'bob', 'carol'} # %% Result result = ...
# %% About # - Name: Conditional Else DictContains # - Difficulty: easy # - Lines: 4 # - Minutes: 2 # %% 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 # %% English # 1. If 'alice' is in `DATA` keys: # - yes - set variable `result` to True # - no - set variable `result` to False # 2. Use `if-else` block # 3. Run doctests - all must succeed # %% Polish # 1. Jeżeli 'alice' znajduje się w kluczach `DATA`: # - tak - ustaw zmienną `result` na True # - nie - ustaw zmienną `result` na False # 2. Użyj bloku `if-else` # 3. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> result # True # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> assert result is not Ellipsis, \ 'Variable `result` has an invalid value; assign result of your program to it.' >>> assert type(result) is bool, \ 'Variable `result` has an invalid type; expected: `bool`.' >>> from pprint import pprint >>> pprint(result) True """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports # %% Types result: bool # %% Data DATA = { 'alice': 'secret', 'bob': 'qwerty', 'carol': '123456', 'dave': 'abc123', 'eve': 'password1', 'mallory': 'NULL', } # %% Result result = ...
# %% About # - Name: Conditional Else Login # - Difficulty: easy # - Lines: 4 # - Minutes: 2 # %% 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 # %% English # 1. If: # - variable `USERNAME` is equal to 'alice' # - variable `PASSWORD` is equal to 'secret' # - then set variable `result` to True # 2. Use `if` block # 3. Run doctests - all must succeed # %% Polish # 1. Jeżeli: # - zmienna `USERNAME` jest równa 'alice' # - zmienna `PASSWORD` jest równa 'secret' # - to ustaw zmienną `result` na True # 2. Użyj bloku `if` # 3. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> result # True # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> assert result is not Ellipsis, \ 'Variable `result` has an invalid value; assign result of your program to it.' >>> assert type(result) is bool, \ 'Variable `result` has an invalid type; expected: `bool`.' >>> from pprint import pprint >>> pprint(result) True """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports # %% Types result: bool # %% Data USERNAME = 'alice' PASSWORD = 'secret' # %% Result result = ...