Conditionals

  • if

  • elif

  • else

Conditionals: if

Conditionals are statements that check for a condition, using the if statement, and then only execute a set of code if the condition evaluates as True.
condition = True

if condition:
    print('This code executes if the condition evaluates as True.')
This code executes if the condition evaluates as True.

Clicker Question #1

Replace --- below with something that will print ‘True’

  • A) I did it!

  • B) I think I did it!

  • C) I tried but am stuck.

  • D) I’m unsure where to start

# can evaluate boolean expression with bool
bool(2 + 2 >= 4)
True
math = (2 + 2)

if math:
    print('True')
True

Conditional: else

After an if, you can use an else that will run if the conditional(s) above have not run.
condition = False

if condition:
    print('This code executes if the condition evaluates as True.')
else: 
    print('This code executes if the condition evaluates as False')
This code executes if the condition evaluates as False

Clicker Question #2

Replace --- below with something that will print ‘False’.

  • A) I did it!

  • B) I think I did it!

  • C) I tried but am stuck.

  • D) I’m unsure where to start

bool(0)
False
bool(None)
False
bool('')
False
my_value = ---

if my_value:
    print('True')
else: 
    print('False')

Conditional: elif

After an if statement, you can have any number of elif`s (meaning 'else if') to check other conditions.
condition_1 = False
condition_2 = True

if condition_1:
    print('This code executes if condition_1 evaluates as True.')
elif condition_2:
    print('This code executes if condition_1 did not evaluate as True, but condition_2 does.')
else: 
    print('This code executes if both condition_1 and condition_2 evaluate as False')
This code executes if condition_1 did not evaluate as True, but condition_2 does.

elif without an else

An else statement is not required, but if both the if and the elif condtions are not met (both evaluate as False), then nothing is returned.

condition_1 = False
condition_2 = False

if condition_1:
    print('This code executes if condition_1 evaluates as True.')
elif condition_2:
    print('This code executes if condition_1 did not evaluate as True, but condition_2 does.')

elif after an else does not make sense

The order will always be if-elif-else…with only the if being required. If the elif is at the end…it will never be tested, as the else will have already returned a value once reached (and thus Python will throw an error).

## THIS CODE WILL PRODUCE AN ERROR
condition_1 = False
condition_2 = False

if condition_1:
    print('This code executes if condition_1 evaluates as True.')
else: 
    print('This code executes if both condition_1 and condition_2 evaluate as False')
elif condition_2:
    print('This code executes if condition_1 did not evaluate as True, but condition_2 does.')
  File "<ipython-input-19-aedfc0cec5db>", line 9
    elif condition_2:
       ^
SyntaxError: invalid syntax

Conditionals With Value Comparisons

Any expression that can be evaluated as a boolean, such as value comparisons, can be used with conditionals.
language = "Perl"

if language == "Python" or language == "R":
    print("Yay!")
elif language == "Perl":
    print("Hmmmmmmm")
else:
    print("Get yourself a programming language!")
Hmmmmmmm
# Exploring conditionals
number = 4

print('Before Conditional')
 
if number < 5:
    print('    if statement execution')
elif number > 5:
    print('    elif statement execution')

print('After Conditional')
Before Conditional
    if statement execution
After Conditional

Clicker Question #3

What will the following code snippet print out:

if False:
    print("John")
elif True:
    print("Paul")
elif True:
    print("George")
else:
    print("Ringo")
Paul
  • A) John

  • B) Paul, George, Ringo

  • C) Paul

  • D) Paul, George

  • E) Ringo

Clicker Question #4

What will the following code snippet print out:

if 1 + 1 == 2:
    print("I did Math")
elif 1/0:
    print("I broke Math")
else:
    print("I didn't do math")
I did Math
  • A) I did Math

  • B) I broke Math

  • C) I didn’t do math

  • D) This code won’t execute

Clicker Question #5

What will the following code snippet print out:

conditional = False
python = "great"

if conditional:
    if python == "great":
        print("Yay Python!")
    else:
        print("Oh no.")
else:
    print("I'm here.")
Oh no.
  • A) Yay Python!

  • B) Oh no.

  • C) I’m here.

  • D) This code won’t execute

Properties of conditionals

  • All conditionals start with an if, can have an optional and variable number of elif’s and an optional else statement

  • Conditionals can take any expression that can be evaluated as True or False.

  • At most one component (if / elif / else) of a conditional will run

  • The order of conditional blocks is always if then elif(s) then else

  • Code is only ever executed if the condition is met