输入:
cars = ['audi', 'bmw', 'subaru', 'toyota'] for car in cars: if car == 'bmw': print(car.upper()) else: print(car.title())
输出:
Audi BMW Subaru Toyota
在Python中检查是否相等时区分大小写,可以在判断式中加上()。
在Python中的 且 是符号 and,相当于C语言中的 &&
或 是符号 or ,相当于C语言中的 ||
Python中不使用else if作为条件判断,而使用elif
1.2 检查列表中的值
输入:
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.")
输出:
Marie, you can post a response if you wish.
同理可以判断列表是不是空,
输入:
a=[]
if a:
print("a不为空")
else:
print("a为空")
输出:
a为空



