6.2. Conditional Statements

6.2.1. Positive and negative values

Negative values:

  • False

  • None

  • 0

  • 0.0

  • 0+0j

  • 0.0+0.0j

  • empty str() or ''

  • empty tuple() or ()

  • empty dict() or {}

  • empty list() or []

  • empty set()

Positive values:

  • any other

6.2.2. if

  • The same indent level

6.2.2.1. Syntax

Listing 77. Single line statements
if True:
    print('First line of the true statement')
Listing 78. Multiline blocks
if True:
    print('First line of the true statement')
    print('Second line of the true statement')
    print('Third line of the true statement')

6.2.2.2. Checking for simple value

age = 7

if age == 7:
    print('Go to school')
Listing 79. Inside joke (see José Jiménez)
job = 'astronaut'

if job == 'astronaut':
    print('Astronauts are from USA')
number = 6

if number % 2 == 0:
    print('Even')
# Even

6.2.2.3. Checking if value is in range

age = 7

if 0 <= age < 18:
    print('Age is between [0, 18)')
# Age is between [0, 18)
a = 10
b = 100

if 0 <= a <= 50 < b:
    print('Yes')
# Yes

6.2.2.4. Checking if has value

  • More advanced examples will be covered latter

name = input('What is your name?: ')
# What is your name?: Jan Twardowski<Enter key>

if name:
    print(f'My name is... {name}')
# My name is Jan Twardowski
name = None

if name:
    print(f'My name... {name}')

6.2.3. else

  • Optional

  • Executed when condition is not met

6.2.3.1. Syntax

Listing 80. Single line statements
if True:
    print('First line of the true statement')
else:
    print('First line of the false statement')
Listing 81. Multiline blocks
if True:
    print('First line of the true statement')
    print('Second line of the true statement')
    print('Third line of the true statement')
else:
    print('First line of the false statement')
    print('Second line of the false statement')
    print('Third line of the false statement')

6.2.3.2. Checking if variable is certain value

job = 'cosmonaut'

if job == 'astronaut':
    print('Astronauts are from USA')
else:
    print('Cosmonauts are from Russia')
# Cosmonauts are from Russia
name = input('What is your name?: ')
# What is your name?: <Enter key>

if name:
    print(f'My name is... {name}')
else:
    print('Did you forget to type your name?')
# Did you forget to type your name?

6.2.4. Inline if

Listing 82. Normal if
country = 'Russia'

if country == 'Russia':
    job = 'cosmonaut'
else:
    job = 'astronaut'
Listing 83. One line version
country = 'Russia'

job = 'cosmonaut' if country == 'Russia' else 'astronaut'

6.2.5. elif

  • Used to check for additional condition if first is not met

  • In other languages is known as else if

language = 'Polish'

if language == 'English':
    print('Hello')
elif language == 'Russian':
    print('Здравствуйте')
elif language == 'German':
    print('Guten Tag')
elif language == 'Polish':
    print('Witaj')
else:
    print("I don't speak this language")

# Witaj

6.2.5.1. Switch statement

  • No switch statement in Python!

  • switch in Object Oriented Programming is considered a bad practise

  • PEP 275

switch = {
    'English': 'Hello',
    'Russian': 'Здравствуйте',
    'German': 'Guten Tag',
    'Polish': 'Witaj',
}

language = 'French'
switch.get(language, "I don't speak this language")
# "I don't speak this language"
def switch(key):
    return {
        'English': 'Hello',
        'Russian': 'Здравствуйте',
        'German': 'Guten Tag',
        'Polish': 'Witaj',
    }.get(key, "I don't speak this language")


switch('Russian')       # 'Здравствуйте'
switch('French')        # "I don't speak this language"

6.2.6. Assignments

6.2.6.1. Conditioning on user input

  • Complexity level: easy

  • Lines of code to write: 6 lines

  • Estimated time of completion: 5 min

  • Solution: solution/ifelse_input.py

English
  1. Ask user for age

  2. User will pass only valid int

  3. Print whether user is adult

Polish
  1. Poproś użytkownika o wiek

  2. Użytkownika poda tylko poprawne int

  3. Wypisz czy użytkownik jest pełnoletni

The whys and wherefores
  • Reading input

  • Type casting

  • Conditional statements

  • Defining variables

  • Magic Number