Making Choices with Conditional Statements

cereal[cereal['mfr'] == 'K']
name mfr type calories ... shelf weight cups rating
2 All-Bran K Cold 70 ... 3 1.0 0.33 59.425505
3 All-Bran with Extra Fiber K Cold 50 ... 3 1.0 0.50 93.704912
6 Apple Jacks K Cold 110 ... 2 1.0 1.00 33.174094
... ... ... ... ... ... ... ... ... ...
62 Rice Krispies K Cold 110 ... 1 1.0 1.00 40.560159
66 Smacks K Cold 110 ... 2 1.0 0.75 31.230054
67 Special K K Cold 110 ... 1 1.0 1.00 53.131324

23 rows × 16 columns

my_name = 'Hayley' 

if my_name.lower() == 'hayley':
    print('My name is Hayley too!')
elif my_name.lower() == 'totoro':
    print('Interesting, I loved that movie!')
else:
    print("That's a great name.")
  
print('Nice to meet you!')
My name is Hayley too!
Nice to meet you!
404 image
my_name = 'Totoro' 

if my_name.lower() == 'hayley':
    print('My name is Hayley too!')
elif my_name.lower() == 'totoro':
    print('Interesting, I loved that movie!')
else:
    print("That's a great name.")
  
print('Nice to meet you!')
Interesting, I loved that movie!
Nice to meet you!
404 image
my_name = 'Desmond' 

if my_name.lower() == 'hayley':
    print('My name is Hayley too!')
elif my_name.lower() == 'totoro':
    print('Interesting, I loved that movie!')
else:
    print("That's a great name.")
  
print('Nice to meet you!')
That's a great name.
Nice to meet you!
404 image

Syntax

my_name = 'Hayley' 

if my_name.lower() == 'hayley':
    print('My name is Hayley too!')
elif my_name.lower() == 'totoro':
    print('Interesting, I loved that movie!')
else:
    print("That's a great name.")
  
print('Nice to meet you!')
My name is Hayley too!
Nice to meet you!

Structure

my_name = 'Hayley' 

if my_name.lower() == 'hayley':
    print('My name is Hayley too!')
My name is Hayley too!


The structure of a choice is as follows:

if SOME_BOOLEAN:
    statement body 

Keywords - if, else

my_name = 'Mia' 

if my_name.lower() == 'hayley':
    print('My name is Hayley too!')
elif my_name.lower() == 'totoro':
    print('Interesting, I loved that movie!')
else:
    print("That's a great name.")
  
print('Nice to meet you!')
That's a great name.
Nice to meet you!

Keywords - elif

my_name = 'Totoro' 

if my_name.lower() == 'hayley':
    print('My name is Hayley too!')
elif my_name.lower() == 'totoro':
    print('Interesting, I loved that movie!')
else:
    print("That's a great name.")
  
print('Nice to meet you!')
Interesting, I loved that movie!
Nice to meet you!
if my_name.lower() == 'hayley':
    print('My name is Hayley too!')
elif my_name.lower() == 'totoro':
    print('Interesting, I loved that movie!')
elif  my_name.lower() == 'Desmond':
    print("That's a great name.")
  
print('Nice to meet you!')


my_name = 'Hayley' 

elif my_name.lower() == 'totoro':
    print("Interesting, I loved that movie!")
Error: invalid syntax (<string>, line 2)

Order matters

item = 13 

if item > 10:
    magnitude = 'Between 10 and 20'
elif item > 20:
    magnitude = 'Greater than 20'
else:
    magnitude = '10 or less'
 
magnitude
'Between 10 and 20'
item = 25 

if item > 10:
    magnitude = 'Between 10 and 20'
elif item > 20:
    magnitude = 'Greater than 20'
else:
    magnitude = '10 or less'
 
magnitude
'Between 10 and 20'
404 image
item = 25 

if item > 20:
    magnitude = 'Greater than 20'
elif item > 10:
    magnitude = 'Between 10 and 20'
else:
    magnitude = '10 or less'
 
magnitude
'Greater than 20'
404 image
item = 13 

if item > 20:
    magnitude = 'Greater than 20'
elif item > 10:
    magnitude = 'Between 10 and 20'
else:
    magnitude = '10 or less'
 
magnitude
'Between 10 and 20'
404 image

Inline

item = 13


if  item > 10:
    magnitude = 'Greater than 10'
else:
    magnitude = '10 or less'
    
magnitude
'Greater than 10'


magnitude = "Greater than 10" if item > 10 else "10 or less"
magnitude
'Greater than 10'

Python Keyword “in”

exercises = ['burpees', 'lunges', 'squats', 'curls', 'deadlifts']

'squats' in exercises
True


if 'squats' in exercises:
    sore = "Extreme"
else:
    sore = "Not sore"
sore
'Extreme'

Let’s apply what we learned!