1. Conditional Statements

1.1. if

  • The same indent level

1.1.1. Simple syntax

if True:
    print('First line of the true statement')

1.1.2. 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')

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

1.1.4. Checking for simple value

name = 'José Jiménez'

if name == 'José Jiménez':
    print('My name... José Jiménez')

1.1.5. Checking if value is in range

age = 7

if 0 <= age < 18:
    print('Age is between [0, 18)')

1.1.6. Checking if has value

  • More advanced examples will be covered latter

name = None

if name:
    print(f'My name... {name}')
else:
    print('Name is not defined')

1.2. else

  • Optional

  • Executed when condition is not met

1.2.1. Checking if variable is certain value

name = 'José Jiménez'

if name == 'José Jiménez':
    print('My name... José Jiménez')
else:
    print('Your name is different')

1.2.2. 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')

1.3. Inline if

Listing 3. Normal if
ip = '127.0.0.1'

if '.' in ip:
    protocol = 'IPv4'
else:
    protocol = 'IPv6'
Listing 4. One line version
ip = '127.0.0.1'

protocol = 'IPv4' if '.' in ip else 'IPv6'

1.4. 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 == 'Germany':
    print('Guten tag!')
elif language == 'Poland':
    print('Witaj!')
else:
    print("I don't speak this language")

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

1.5. Assignments

1.5.1. Conditioning on user input

  • Complexity level: easy

  • Lines of code to write: 6 lines

  • Estimated time of completion: 5 min

  • Filename: 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

1.5.2. Is odd number

  • Complexity level: easy

  • Lines of code to write: 5 lines

  • Estimated time of completion: 5 min

  • Filename: solution/ifelse_is_odd.py

English
  1. Read a number from user

  2. User will pass only valid int

  3. Print whether number is odd

  4. Number is odd, when divided modulo (%) by 2 has a reminder

Polish
  1. Wczytaj liczbę od użytkownika

  2. Użytkownika poda tylko poprawne int

  3. Wypisz czy liczba jest nieparzysta

  4. Liczba jest nieparzysta, gdy dzielona modulo (%) przez 2 ma resztę

The whys and wherefores
  • Reading input from user

  • Type casting

  • Print formatting

  • Numerical operators

Hints
  • % has different meaning for int and str