Chapter 5: IF Statements Flashcards
print out a list of cars using a for loop, but single out a specific value to add a function to it
cars = ['audi', 'bmw', 'subaru', 'toyota'] for car in cars: if car == 'bmw': print(car.upper()) else: print(car.title())
write several simple conditional tests, some being True and some being False
car = ‘bmw’
print(car == ‘bmw’)
car = ‘audi’
print(car==’bmw’)
write several simple conditional tests while changing capitalizations
car = ‘Audi’
print(car==’audi’)
print(car.lower() == ‘audi’)
us an if statement and a variable to check for inequality
requested_topping = ‘mushrooms’
if requested_topping != ‘mushrooms’:
print(“hold the anchovies”)
check whether a number is the same as a number in a variable
age = 18
print(age == 18)
use an if statement to check a numbered variable for inequality
answer = 17
if answer != 17:
print(“string”)
make other numerical comparisons using other signs
< > <= >=
write a numerical comparison that needs two conditions to be met to be true or false
age = 22 age_1 = 18 print(age >= 21 and age_1 >= 21) age_1 = 22 print(age >= 21 and age_1 >= 21)
create a list in a variable and check if a value is in a list
write one that is true and one that is false
requested_topping = [‘mushrooms’, ‘onions’, ‘pineapple’]
print(‘mushrooms’ in requested_topping)
print(‘pepperoni’ in requested_topping)
create a list in a variable and create a variable with a value that will be checked to see if it is not in the list using an if statement
use an f-string in the if statement response
banned_users = ['andrew', 'carolina', 'david'] user = 'marie'
if user not in banned_users:
print(f”{user.title()}, you can post a response if you wish”)
what is a boolean expression
a conditional test uses boolean values that are either true or false
boolean values provide an efficient way to track the state of a program or a particular condition that is important in your program
write a series of conditional tests. print a statement describing each test and your prediction for the results of each test
car = ‘subaru’
print(“Is car == ‘subaru’? I predict True.”)
print(car == ‘audi’)
print(“\nIs car == ‘subaru’? I predict False.”)
print(car == ‘audi’)
write a simple if statement
then use a variable to do the same thing
if conditional_test:
print(do something)
age = 19
if age >= 18:
print(“You are old enough to vote!”)
add an else statement to any previous if statement
age = 19
if age >= 18:
print(““you are old enough to vote!””)
Else:
print(“You are too young to vote”)
write an if-elif-else chain using -admissions for anyone under the age of 4 is free - admissions for anyone between the ages of 4 and 18 is $25 -admissions for anyone older than 18 is $40
age = 12 if age < 4: price = 0 elif age <18: price = 25 else: price = 40
add another elif statement with the additions of above 65 = 20.
elif age<65:
price = 40
else:
price = 20
why would you omit the else statement?
if you have a specific final condition you are testing for, it may be appropriate to omit the else
test multiple if statements using a list
requested_toppings = [‘mushrooms’, ‘extra cheese’]
If ‘mushrooms’ in requested_toppings:
print(“adding mushrooms”)
if ‘pepperoni’ in requested_toppings:
print(“adding pepperoni”)
if ‘extra cheese’ in requested_topping:
print(“adding extra cheese”)
why would you use multiple if statements?
python only needs one test to pass in an if-elif-else chain. Use multiple if it’s possible for more than one condition to be true.
write a for loop that prints out a value in a string
requested_toppings = [‘mushrooms’, ‘green peppers’, ‘extra cheese’]
for value in requested_toppings:
print(f”“adding {value}.””)
print(“\nFinished making your pizza”)
add an if-else chain to the for loop that says they are out of green peppers
if value == ‘green peppers’:
print(““sorry we are out of green peppers””)
else:
print(f”adding {value}”)
Check to make sure a list isn’t empty
if requested_toppings:
for value in requested_toppings:
print(f”adding {value}.”)
print(“\nFinished making your pizza”)
else:
print(“are you sure you want a plain pizza”)