http://www.anchor.fm/DivergentMind
Here’s what I’ve been able to do today so far. Not much due to my meds kicking my butt, but there is more to be done tonight.
#if statements.
#thanks to xebec for the tri-screen. It works wonderfully.
#so today I’ll be going through chapter five of the python crash course. It covers if statements.
cars = [‘audi’, ‘bmw’, ‘subaru’, ‘toyota’]
for car in cars:
if car == ‘bmw’:
print(car.upper())
else:
print(car.title())
#conditional tests.
#either true or false.
#>>> car = ‘bmw’
#>>> car == ‘bmw’
#True
#>>> car = ‘audi’
#>>> car == ‘bmw’
#False
#= makes a statement, == asks a question.
#ignoring case when checking for equality.
#>>> = ‘audi’
#>>> == ‘Audi’
#False
#If wanting to check variable without case restrictions use .lower().
#>>> car = ‘Audi’
#>>> car.lower() == ‘audi’
#True
#>>> car
#>>>’Audi’
answer = 17
if answer != 42:
print(“That is not the correct answer. Please try again!”)
#>>> age = 19
#>>> < 21
#True
#>>> age <= 21
#True
#>>> age > 21
#False
# age >= 21
#False
#Checking and to check multiple conditions:
#>>> age_0 = 22
#>>> age_1 = 18
#>>> age_0 >= 21 and age_1 >= 21
#False
#>>> age_1 = 22
#>>> age_0 >= 21 and age_1 >= 21
#True
#checking with or to check multiple conditions:
#>>> age_0 = 22
#>>> age_1 = 18
#>>> age_0 >= 21 or age_1 >= 21
#True
#>>> age_0 = 18
#>>> age_0 >= 21 or age_1 >= 21
#False
#Using the in keyword.
#>>> requested_toppings = [‘mushrooms’, ‘onions’, ‘pineapple’]
#>>> ‘mushrooms’ in requested_toppings
#True
#>>> ‘pepperoni’ in requested_toppings
#False
#Checking whether a value is not in a list.
banned_users = [‘andy’, ‘woody’, ‘slinky’]
user = ‘marie’
if user not in banned_users:
print(f”{user.title()}, you can post a response if you wish.”)
#Boolean Expressions.
game_active = True
can_edit = False
#Simple if statements have one test and one action:
#if conditional_test:
# do something
#if true code is executed, if false then code is ignored.
age = 19
if age >= 18:
print(“You are old enough to vote!”)
#you can have any number of lines of code in if statements if they can be executed.
print(“Have you registered to vote yet?”)
#if-else statements.
age = 17
if age >= 18:
print(“You are old enough to vote!”)
print(“Have you registered to vote yet?”)
else:
print(“Sorry, you are too young to vote.”)
print(“Please register to vote as soon as you turn 18!”)
#The if-elif-else chain:
#determine your variable.
age = 12
#create an if statement.
if age < 4:
print(“Your admission is free.”)
elif age < 18:
print(“Your admission will cost you $25”)
else:
print(“Your admission cost is a staggering $40”)
#it is more efficient to contain a changing variable within teh if-elif-else and call a simple print() function instead.
age = 12
if age < 4:
price = 0
elif age < 18:
price = 25
else:
price = 40
print(f”Your admission cost is ${price}.”)
#using multiple elif blocks.
age = 12
if age < 4:
price = 0
elif age < 18:
price = 25
elif age < 65:
price = 40
else:
price = 20
print(f”Your admission cost is ${price}.”)
#When you need to check each test before presenting a result you can remove the else: block and replace with elif.
age = 12
if age < 4:
price = 0
elif age < 18:
price = 25
elif age < 65:
price = 40
elif age >= 65:
price = 20
print(f”Your admission cost is ${price}.”)
#else is a catchall statement.
#Testing Mulitple Conditions with just if statements.
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_toppings:
print(“Adding extra cheese.”)
print(“\nFinished making your pizza!”)
#The above doesn’t work with an if-elif-else set because it would only use one test and pass the rest.
#Using if statements with Lists:
#pg 86
Leave a comment