《Python Crash Course》导读(Chapter 5)

发布于:2022-12-10 ⋅ 阅读:(308) ⋅ 点赞:(0)

Code:[ZachxPKU/Python Crash Course]

Chapter 5 if statements

5.1 A simple example


cars = ['audi', 'bmw', 'subaru', 'toyota']

for car in cars:
    if car == 'bmw':
        print(car.upper())
    else:
        print(car.title())

5.2 Conditional Tests

5.2.1 Checking for equality


car = 'bmw'
car == 'bmw'

car = 'audi'
car == 'bmw'

5.2.2 Ignoring case when checking for equality


car = 'Audi'
car == 'audi'

car = 'Audi'
car.lower() == 'audi'

car

5.2.3 Checking for inequality


requested_topping = 'mushrooms'

if requested_topping != 'anchovies':
    print("Hold the anchovies!")

5.2.4 Numerical Comparisons


age = 18
age == 18

answer = 17
if answer != 42:
    print("That is not the correct answer. Please try again!")

age = 19
age < 21

age <= 21

age >= 21

5.2.5 Checking Multiple Conditions

Using and to check multiple conditions


age_0 = 22
age_1 = 18
age_0 >= 21 and age_1 >= 21

age_1 = 22
age_0 >= 21 and age_1 >= 21

(age_0 >= 21) and (age_1 >= 21)

using or to check multiple conditions


age_0 = 22
age_1 = 18
age_0 >= 21 or age_1 >= 21

age_0 = 18
age_0 >= 21 or age_1 >= 21

5.2.6 Checking whether a value is in a list


requested_toppings = ['mushrooms', 'onions', 'pineapple']
'mushrooms' in requested_toppings

'pepperoni' in requested_toppings

5.2.7 Checking whether a value is not in a list


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.")

5.2.8 Boolean expressions


game_active = True
can_edit = False 

TRY IT YOURSELF

  • conditional_tests.py
  • more_conditional_tests.py

5.3 if statements

5.3.1 if statements


age = 19
if age >= 18:
    print("You are old enough to vote!")

age = 19
if age >= 18:
    print("You are old enough to vote!")
    print("Have you registered to vote yet?")

5.3.2 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!")

5.3.3 the if-elif-else chain


age = 12

if age < 4:
    print("Your admission cost is $0.")
    
elif age < 18:
    print("Your admission cost is $25.")
else:
    print("Your admission cost is $40.")

age = 12

if age < 4:
    price = 0
    
elif age < 18:
    price = 25
    
else:
    price = 40
    
print(f"Your admission cost is {price}.")

5.3.4 Using multiple elif blocks


age = 12

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}.")

5.3.5 Omitting the else block


age = 12

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}.")

5.3.6 Testing multiple conditions


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!")

requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:
    print("Adding mushrooms.")
elif 'pepperoni' in requested_toppings:
    print("Adding pepperoni.")
elif 'extra cheese' in requested_toppings:
    print("Adding extra cheese.")
    
print("\nFinished making your pizza!")

TRY IT YOURSELF

  • alien_colors_1.py
  • alien_colors_2.py
  • alien_colors_3.py
  • stages_of_life.py
  • favorite_fruit.py

5.4 Using if statements with lists

5.4.1 Checking for special items


requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']

for requested_topping in requested_toppings:
    print(f"Adding {requested_topping}.")
    
print("\nFinished making your pizza!")

requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']

for requested_topping in requested_toppings:
    if requested_topping == 'green peppers':
        print("Sorry, we are out of green peppers right now.")
    else:
        print(f"Adding {requested_topping}.")
    
print("\nFinished making your pizza!")

5.4.2 Checking that a list is not empty


requested_toppings = []

if requested_toppings:
    for requested_topping in requested_toppings:
        print(f"Adding {requested_topping}.")
    print("\nFinished making your pizza!")
    
else:
    print("Are you sure you want a plain pizza?")
        

5.4.3 Using multiple lists


available_toppings = ['mushrooms', 'olives', 'green peppers',
'pepperoni', 'pineapple', 'extra cheese']

requested_toppings = ['mushrooms', 'french fries', 'extra cheese']

for requested_topping in requested_toppings:
    if requested_topping in available_toppings:
        print(f"Adding {requested_topping}.")
    else:
        print(f"Sorry, we don't have {requested_topping}.")

print("\nFinished making your pizza!")

TRY IT YOURSELF

  • hello_admin.py
  • no_users.py
  • checking_usernames.py
  • ordinal_numbers.py

5.5 Styling your if statements


if age < 4:
    print("if age < 4")

is better than


if age<4:
    print("if age<4")

TRY IT YOURSELF

5.6 Summary

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到