a = False
if a:
print('hello!')
else:
print('goodbye!')
Sometimes we want two blocks of code:
This is done with the else
keyword.
Either the if block or the else block will execute. Never both!
a = 5
if a > 5:
print('hello!')
else:
print('goodbye!')
Of course, we can use comparison operators to create a boolean.
a = 5
b = 'foo'
if a > 5 or b == 'foo':
print('hello!')
else:
print('goodbye!')
We can also use the and
or the or
operator to combine two booleans into a single boolean for use in control flow.
a = 5
if a > 5:
print('hello!')
elif a > 0:
print('eh')
else:
print('goodbye!')
Sometimes we want several if clauses. We can achieve this with the elif
keyword.
Once again, the if block, elif block, and else block are all mutually exclusive. Only one will execute!
What will this print for different values of a
?
a = 5
if a:
print('hello!')
else:
print('goodbye!')
What if we try to give a non-boolean to the if
statement?
Python will try to cast the value into a boolean, then use the result of that casting to perform the control flow.
This can be convenient, but it can also be dangerous!
a = 0
if a:
print('hello!')
else:
print('goodbye!')
What will this return?
a = 0
if a is not None:
print(a)
else:
print('goodbye!')
Often we want to check if something exists, for which we can compare with None
!
a = 0
if a is None:
raise Exception('a should exist, but it doesnt!')
Exceptions are our friends.
We like our code to raise explicit, friendly, helpful suggestions whenever things aren't as they should be.
We can raise exceptions with the raise
keyword, followed by an exception type. Exception
is the most basic type of exception.
a = 0
if a is None:
raise Exception('a should exist, but it doesnt!')
Exceptions take a message parameter, which is a string that describes what went wrong.
Helpful exception messages are an important part of writing good code!
def printer(a):
if a is None:
raise Exception('I can not print nothing!!')
print(a)
Good functions throw exceptions when they are given bad data.
Why?