if 语句
总述与例子
定义列表
cars = ['audi', 'bwm', 'subaru', 'toyota']
for car in cars:
if car == 'bwm': # 如果遇到bwm
print(car.upper()) # 全部大写
else: # 如果没有
print(car.title()) # 首字母大写
没有总述
条件测试
是否相等(==)
>>> car = 'bwm'
>>> car == 'bwm'
True
>>> car = 'audi'
>>> car == 'bwm'
False
- 检查是否相等时区分大小写
>>> car = 'Audi'
>>> car == 'audi'
False
可以使用lower()方法, 比较时全部使用小写.
>>> car = 'Audi'
>>> car.lower() == 'audi'
True
是否不相等(!=)
name = 'John'
if name != 'Andi': # 如果name不是Andi
print('You are not Andi!')
比较数字
是否相等
>>> age = 18
>>> age == 18
True
是否不相等
answer = 100
if answer != 10:
print("That is not the correct answer.")
数字的其他比较
>>> age = 19
>>> age < 21 # 小于
True
>>> age <= 21 # 小于等于
True
>>> age > 21 # 大于
False
>>> age >= 21 # 大于等于
False
判断多个条件(and 与 or)
- and 只有在两个条件都为True时才会返回True
否则返回False.
True and True = True
True and False = False
False and False = False
- or 只有在两个条件都为False时才会返回False
否则返回True.
True or True = True
True or False = True
False or False = False
检查列表中的值
检查特定值是否包含在列表中(in)
test_list = ['aaa', 'bbb', 'ccc']
if 'aaa' in test_list: # 返回 True
print('aaa is in test_list')
if 'ddd' in test_list: # 返回 False
print('oh.')
检查特定值是否不包含在列表中(not in)
users = ['jack', 'carolina', 'david']
user = 'max'
if user not in users:
print(user.title() + ', you can not pass.')
布尔表达式
在程序中可以声明布尔型变量,值为True 或者False
game_play = True
can_edit = False
if 语句
简单的if语句
代码块需要缩进才会被判断属于if语句
if [条件]:
[代码块]
if - else 语句
if [条件]:
[条件为True 代码块]
else:
[条件为False 代码块]
if - elif - else 语句
条件1先被判断
条件2在条件1不通过时判断
代码块1在条件1通过时执行
代码块2在条件2通过时执行
代码块3在条件都不通过时执行
if [条件1]:
[代码块1]
elif [条件2]:
[代码块2]
else:
[代码块3]
省略 else 代码块
if - elif - else 语句中可以省略else语句
如果你确保你的条件是全面的,可以省略else
使用else可能会遇到意想不到的情况,这时候执行的代码可能不会达到预期。
测试多个条件
多个独立的if语句可以测试多个条件,而且确保每个条件都会被测试。而不会跳过任何一个测试。
requested_toppings = ['mushroom', 'extra cheese']
if 'mushroom' in requested_toppings:
print('Adding mushrooms.')
if 'pepperoni' in requested_toppings:
print('Adding pepperoni')
if 'extra cheese' in requested_toppings:
print('Adding extra cheese')
使用if语句处理列表
检查列表中的特殊值
- 作用:当列表中包含某个元素时,执行一段代码。
- 栗子:披萨店的某个食材用完了:
toppings = ['mushroom', 'green peppers', 'extra cheese']
# 依次将列表中的元素赋值给一个变量
# 判断变量是否为green peppers
for topping in toppings:
if topping == 'green peppers':
print('我们没有青椒了')
else:
print('加入' + topping)
print('完成了!')
判断列表是否为空
- 作用:判断列表是不是空的。
- 栗子:披萨店检查顾客需要的食材列表是不是空的(空的你是想吃面饼吗)
toppings = [] # 创建一个空列表
if toppings: # 如果列表有东西
for topping in toppings:
if topping == 'green peppers':
print('我们没有青椒了')
else:
print('加入' + topping)
else: # 如果列表没东西
print('您来披萨店吃烤饼吗?(手动滑稽)')
使用多个列表
- 作用:依次判断一个列表中各项是否包含在另一个列表中。
- 栗子:
- 披萨店。
- 需要实现:判断顾客要求的食材是否包含在现有食材中。(他要吃的我有没有),如果有,执行上面的代码(加入xxx)。如果没有,告知没有。
- 实现:
- 将披萨店现有的食材创建为一个列表,将顾客要求的食材创建列表。
- 用for循环,依次将顾客要求的食材储存到一个变量中
- for循环中加入if语句,判断这个变量有没有包含在现有食材的列表中。
# 现有的食材
available_toppings = ['mushroom', 'olives', 'green peppers',
'pepperoni', 'pineapple', 'extra cheese']
# 顾客要求的食材
requested_toppings = ['mushroom', 'french fries', 'extra cheese']
# Ps: french fries : 炸薯条。披萨加炸薯条,讲究。
# 依次判断顾客要求的食材是否包含在现有食材中.
for requested_topping in requested_toppings:
if requested_topping in available_toppings:
print('加入' + requested_topping)
else:
print('我们没有任何' + requested_topping)
print('完成了!')