Conditionals#
ifelifelse
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 = 60
if speed > speed_limit:
ticket = True
elif speed == speed_limit:
ticket = 'warning'
else:
ticket = False
print(ticket)
False
Properties of conditionals#
All conditionals start with an
if, can have an optional and variable number ofelif’s and an optionalelsestatementConditionals can take any expression that can be evaluated as
TrueorFalse.At most one component (
if/elif/else) of a conditional will runThe order of conditional blocks is always
ifthenelif(s) thenelseCode following
if/elifis only ever executed if the condition is met
Activity: Conditionals I#
Complete the question in this Google Form (https://forms.gle/h7LZNmD4WcpveLam6) and then click submit.
# SPACE TO TEST THINGS OUT
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(6)
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/ebekT5weU4mgwsNq9) 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_temperaturethat 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.
## SPACE TO TEST THINGS OUT
Activity: Collections#
Activity: Conditionals III#
Commplete the question in this Google Form (https://forms.gle/n45ykyKZa5RwMoCSA) and then click submit
Write a function called
calculate_tipthat uses thebill_amountandtip_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.
.2to represent 20%) or a percent (i.e.20to 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
## SPACE TO TEST THINGS OUT
## NOTE TO SELF: discuss keyword vs. positional arguments
Summary#
if/elif/elseCode 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