Conditionals#
Q&A
Q: Would both the codes from Form A and Form B be considered functions? (this is referencing hte code/activity from last class)
A: Yes
Q: How would a oral exam essentially be structured? A: Staff will have two questions each week for each student - the first question students will be asked to walk through code and explain what it’s accomplishing. The second question will ask a broad question about the previous week’s material. For example, this week, questions focus on functions. Next week, questions will focus on conditionals. Students have 5 min to answer both.
Q: Have you considered posting video quizzes further in advance?
A: Yes. And apologies VQ5 was late. For the rest of the quarter, the following week’s video quizzes will be posted on the previous Friday.
Course Announcements
Due this week:
CL2 due Fri
A1 due Sun
Heads up:
Posted this Fri (tomorrow): VQ6, CL3, A2
VQ6 due Mon will be longer watching/reading than typical….but there won’t be any reading/VQ for Thursday so overall weekly workload similar
Oral exam notes:
Please try to be on time if you’re not in the lab when you scheduled your oral exam
If you delete your oral exam Gcal invite, it deletes your scheduled time
Conditionals#
if
elif
else
value = 5
if value == True:
print('if statement executed')
elif value == False:
print('elif statement executed')
else:
print('else statement executed')
Conditionals#
speed_limit = 65
speed = 75
if speed > speed_limit:
ticket = True
elif speed == speed_limit:
ticket = 'warning'
else:
ticket = False
print(ticket)
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 following
if
/elif
is only ever executed if the condition is met
Activity: Conditionals I#
Complete the question in this Google Form (https://forms.gle/DmwaJCNXiVbz9j5K9) and then click submit.
# TEST CODE OUT HERE
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)
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!
Activity: Conditionals II#
Complete the question in this Google Form (https://forms.gle/sh82qAWNhtY4Y2X58) and then click submit.
There are three questions, the third of which requires coding:
In a previous lecture, we wrote a function
convert_to_f
…but you’d probably want a function that could go in both directions.
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 multiplied by 9/5 and then 32 will be added to that quantity to convert to Fahrenheit. A temperature in Fahrenheit would have 32 subtracted and then that quantity multiplied by 5/9.
## YOUR CODE HERE
# TEST YOUR FUNCTION HERE
Activity: Conditionals III#
Commplete the question in this Google Form (https://forms.gle/vFbqVjqwHyE2BTk4A) and then click submit
Write a function called calculate_tip that uses the
bill_amount
andtip_percent
, will return the tip and the total amount to pay
Now for the catch: we want it to calculate the tip correctly whether people enter a proportion (i.e.
.2
to represent 20%) or a percent (i.e.20
to represent 20%)…we’re going to assume that a value greater than zero but less than or equal to 1 is a proportion.
Test cases:
calculate_tip(100, .10)
andcalculate_tip(100, 10)
should both return the tip of 10.0 and a total of 110.0
Final note: two variables can be returned from a function if they are separated by a comma
## YOUR CODE HERE
# TEST YOUR FUNCTION HERE
# NOTE TO SELF: Discuss positional vs keyword arguments on execution and default values
Summary#
if
/elif
/else
Code Style
Conditionals + Functions
Collections (sneak peak)#
ability to store more than one piece of information in a single variable
lists, dictionaries, tuples
# list
my_list = [1, 2, 3, 4,]
# tuple
my_tuple = (1, 2, 3, 4)
# dictionary
my_dictionary = {'Shannon': 'mom',
'Josh': 'dad',
'Kayden': 'kid'}
Indexing#
access a value or set of values from of a collection
uses square brackets
[]
my_list[0]
Lists are mutable; tuples are not#
my_list[0] = 6
my_list
# THIS WILL ERROR
my_tuple[0] = 6
my_tuple