两个等号==,返回True或者False。
car = "Audi"
print(car.lower() == 'audi')
print(car)
lower()函数不改变存储在变量car中的值。
!=
==、<、<=、>、>=、
and、or
requested_toppings = ['mushrooms', 'onions', 'pineapple']
print('mushrooms' in requested_toppings)
print('pepperoni' in requested_toppings)
检查特定值是否不包含在列表中
requested_toppings = ['mushrooms', 'onions', 'pineapple']
print('mushrooms' not in requested_toppings)
print('pepperoni' not in requested_toppings)
注意大小写:
True或者False
if conditional_test:
do something
age = 19
if age >= 18:
print("ok")
if-else语句
age = 19
if age >= 18:
print("ok")
else:
print("no ok")
if-elif-else结构
age = 12
if age < 4:
print("小朋友")
elif age > 18:
print("年轻人")
else:
print("该写作业了")
requested_toppings = []
if requested_toppings:
for requested_topping in requested_toppings:
print(requested_topping)
else:
print("空的列表")
python不支持do ~ while循环。
current_number = 1
while current_number <= 5:
print(current_number)
current_number += 1
让用户选择何时退出
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program."
message = ""
while message != 'quit':
message = input(prompt)
print(message)
发现'quit'一并打印出来,改进:
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program."
message = ""
while message != 'quit':
message = input(prompt)
if message != 'quit':
print(message)
在要求很多条件满足才能继续运行的程序中,可以定义一个变量,用于判断整个程序是否处于活动状态,该变量称为标志。可让标志为True时继续运行,False时终止运行。
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program."
active = True
while active:
message = input(prompt)
if message == 'quit':
active = False
else:
print(message)
使用break退出循环
prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.)"
while True:
city = input(prompt)
if city == 'quit':
break
else:
print("I'd love to go to " + city.title() + "!")
在循环中使用continue
current_number = 0
while current_number < 10:
current_number += 1
if current_number % 2 == 0:
continue
print(current_number)
如果程序陷入无限循环,可按Ctrl+C,也可关闭显示程序输出的终端窗口。
for循环是一种遍历列表的有效方式,但是for循环中不应该修改列表,否则将导致Python难以跟踪其中的元素。要在遍历列表的同时对其进行修改,可使用while循环。通过while循环同列表和字典结合使用,可收集、存储并组织大量输入。
uncomfirmed_users = ['alice', 'brian', 'candace']
confirmed_users = []
# 验证每个用户,直到没有未验证的用户为止
# 将每个通过验证的用户都移动到已验证用户列表
while uncomfirmed_users:
current_user = uncomfirmed_users.pop()
print("Verifying user: " + current_user.title())
confirmed_users.append(current_user)
# 显示所有已验证的用户
print("\nThe following users have been confirmed:")
for confirmed_user in confirmed_users:
print(confirmed_user)
假如有一个宠物列表,其中包含多个值为'cat'的元素,要删除所有这些元素,可以使用while循环:
pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
print(pets)
while 'cat' in pets:
pets.remove('cat')
print(pets)
下面的循环每次都提示输入被调查者的名字和回答。然后将数据存储在一个字典中,将被调查者和回答关联起来:
responses = {}
# 设置一个标志,指出调查是否继续
polling_active = True
while polling_active:
# 提示输入被调查者的名字和回答
name = input("\nWhat's your name?")
response = input("Which mountain would you like to climb someday?")
# 将答案存储在字典中
responses[name] = response
# 看看是否还有人要参与调查
repeat = input("Would you like to let another person respond?(yes/no)")
if repeat == 'no':
polling_active = False
#调查结束,显示结果
print("\n--- Poll Results ---")
for name, response in responses.items():
print(name + " would like to climb " + response + ".")
for循环
for iterating_var in sequence:
statements(s)