banned_users = ['andrew', 'carolina', 'david'] user = 'marie' if user notin banned_users: print(user.title() + ", you can post a response if you wish.")
Marie, you can post a response if you wish.
布尔表达式
不过是条件测试的别名 只有True和False
if语句
1 2
if conditional_test: do something
1 2 3 4
# 简单例子 age = 19 if age >= 18: print("You are old enough to vote!")
You are old enough to vote!
if-else语句
1 2 3 4 5 6 7
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!")
Sorry, you are too young to vote.
Please register to vote as soon as you turn 18!
if-elif-else 结构
1 2 3 4 5 6 7 8
age = 12 if age < 4: price = 0 elif age < 18: price = 5 else: price = 10 print("Your admission cost is $" + str(price) + ".")
Your admission cost is $5.
使用多个 elif 代码块
1 2 3 4 5 6 7 8 9 10
age = 12 if age < 4: price = 0 elif age < 18: price = 5 elif age < 65: price = 10 else: price = 5 print("Your admission cost is $" + str(price) + ".")
Your admission cost is $5.
省略 else 代码块
1 2 3 4 5 6 7 8 9 10
age = 12 if age < 4: price = 0 elif age < 18: price = 5 elif age < 65: price = 10 elif age >= 65: price = 5 print("Your admission cost is $" + str(price) + ".")
Your admission cost is $5.
测试多个条件
1 2 3 4 5 6 7 8 9 10
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!")
Adding mushrooms.
Adding extra cheese.
Finished 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("Adding " + requested_topping + ".") print("\nFinished making your pizza!")
Adding mushrooms.
Sorry, we are out of green peppers right now.
Adding extra cheese.
Finished making your pizza!
1 2 3 4 5 6 7 8
# 列表为空 requested_toppings = [] if requested_toppings: for requested_topping in requested_toppings: print("Adding " + requested_topping + ".") print("\nFinished making your pizza!") else: print("Are you sure you want a plain pizza?")
Are you sure you want a plain pizza?
1 2 3 4 5 6 7 8 9 10
# 使用多个列表 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("Adding " + requested_topping + ".") else: print("Sorry, we don't have " + requested_topping + ".") print("\nFinished making your pizza!")
Adding mushrooms.
Sorry, we don't have french fries.
Adding extra cheese.
Finished making your pizza!
friends = ['phil', 'sarah'] for name in favorite_languages.keys(): print(name.title()) if name in friends: print("Hi " + name.title() + ", I see your favorite language is " + favorite_languages[name].title() + "!")
Jen
Sarah
Hi Sarah, I see your favorite language is C!
Edward
Phil
Hi Phil, I see your favorite language is Python!
按顺序遍历字典中的所有键
sorted
1 2 3 4 5 6 7 8
favorite_languages = { 'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python', } for name insorted(favorite_languages.keys()): print(name)
edward
jen
phil
sarah
遍历字典中的所有值
1 2 3 4 5 6 7 8
favorite_languages = { 'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python', } for value in favorite_languages.values(): print(value)
# 存储所点比萨的信息 pizza = {'crust': 'thick', 'toppings': ['mushrooms', 'extra cheese'], } # 概述所点的比萨 print("You ordered a " + pizza['crust'] + "-crust pizza " + "with the following toppings:") for topping in pizza['toppings']: print("\t" + topping)
You ordered a thick-crust pizza with the following toppings:
mushrooms
extra cheese
每当需要在字典中将一个键关联到多个值时,都可以在字典中嵌套一个列表
1 2 3 4 5 6 7 8 9 10
favorite_languages = { 'jen': ['python', 'ruby'], 'sarah': ['c'], 'edward': ['ruby', 'go'], 'phil': ['python', 'haskell'], } for name, languages in favorite_languages.items(): print("\n" + name.title() + "'s favorite languages are:") for language in languages: print("\t" + language.title())
Jen's favorite languages are:
Python
Ruby
Sarah's favorite languages are:
C
Edward's favorite languages are:
Ruby
Go
Phil's favorite languages are:
Python
Haskell