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