Course Announcements
Due Date:
A1 due tonight (11:59 PM)
CL2 due Friday (11:59 PM)
Notes:
Coding Lab Answers in datahub & on course website
A2 released; covers material through 07-Collections
E1 is next Thursday
covers material through the 07-Collections lecture
practice exam will be released Thursday
next Tuesday will be a review - bring questions
Conditionals#
if
elif
else
Conditionals: Motivation#
You use conditional thinking in your daily life constantly.
You use the inputs in your environment to determine your behavior.
For example…every morning when Kayden (my son) wakes up, I have to determine what we’ll do.
if
when he wakes up he asks for “milkies”, we nurse.
elif
(else if) he says “Kayden wants to eat”or
“Kayden wants oatmeal”, we sit down for breakfast.
else
(catch-all; what we do in all other situations), we play.
Conditionals: if
#
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.')
# equivalent to above
if condition == True:
print('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.
math = ---
if math:
print('True')
Conditional: else
#
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')
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.
my_value = ---
if my_value:
print('True')
else:
print('False')
Conditional: elif
#
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')
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.')
Conditionals With Value Comparisons#
speed_limit = 65
speed =
if speed > speed_limit:
ticket = True
else:
ticket = False
# string value comparisons also possible
a1 = 'completed'
if a1 == 'completed'
action = 'grade'
elif a1 == 'in progress':
action = 'wait'
elif a1 == 'not started':
action = 'incomplete'
else:
action = 'uncertain'
Clicker Question #3#
What will the following code snippet print out:
condition = False
if condition:
print("John")
elif not condition:
print("Paul")
elif not condition:
print("George")
else:
print("Ringo")
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")
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.")
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 ofelif
’s and an optionalelse
statementConditionals can take any expression that can be evaluated as
True
orFalse
.At most one component (
if
/elif
/else
) of a conditional will runThe order of conditional blocks is always
if
thenelif
(s) thenelse
Code is only ever executed if the condition is met
Code Style: Conditionals#
avoid single line statements
a single conditional (if + elif + else) has no additional line spaces between statements
Conditionals: Good Code Style#
value = 16
if value % 2 == 0:
out = "even"
else:
out = "odd"
Conditionals: Code Style to Avoid#
if value%2==0:out="even" # avoid statement on same line as conditional
# avoid blank line between if and else
else:out="odd" # don't forget about spacing around operators!
Functions + Conditionals#
Let’s define a more interesting function than what we did in the functions lecture. Here we are using conditionals within our function.
# Determine if a value is even or odd
def even_odd(value):
if value % 2 == 0:
out = "even"
else:
out = "odd"
return out
# Execute our function to check that
# it is working according to our expectations
even_odd(-1)
Clicker Question #6#
In a previous lecture, we wrote a function convert_to_f
…but you’d probably want a function that could go
Write a function convert_temperature
that will convert a temperature C->F and F->C, returning the converted temperature.
Note: A temperature in Celsius will be multipled by 9/5 and then 32 will be added to that quantity to convert to Farenheit. A temperature in Farenheit would have 32 subtracted and then that quantity multipled by 5/9.
A) I did it! B) I think I did it. C) I tried but I am stuck. D) Super duper lost.
## YOUR CODE HERE
# TEST YOUR FUNCTION HERE
Summary#
if
/elif
/else
Code Style
Conditionals + Functions