第5章 if 语句

5.1 简单示例

若名称是apple,以全部大写输出,否则以首字母大写输出:

items = ['aaa', 'bbb', 'ccc', 'ddd']
for item in items:
    if item == 'aaa':
        print(item.upper())
    else :
        print(item.title())

>>>
AAA
Bbb
Ccc
Ddd

5.2 条件测试

5.2.1 检查是否相等

要判断两边的值是否相等,可使用两个等号(==):

item = 'apple'
print(item == 'apple')
print(item == 'banana')

>>>
True
False

5.2.2 检查相等区分大小写

Python检查相等时默认区分大小:

item = 'Apple'
print(item == 'apple')
print(item == 'Apple')

>>>
False
True

若想在检查相等时不区分大小写,可以将变量转为小写之后再比较:

item = 'Apple'
print(item.lower() == 'apple')
print(item == 'Apple')

>>>
True
True

5.2.2 检查是否不相等

要判断两边的值是否不相等,可使用叹号和等号(!=):

item = 'apple'
print(item != 'Apple')

>>>
True

5.2.4 数值比较

num = 22
print(num == 22)
print(num > 21)
print(num < 22)
print(num >= 18)

>>>
True
True
False
True

5.2.5 检查多个条件

1.使用 and 检查多个条件

num_1 = 10
num_2 = 20
print(num_1 >= 10 and num_2 <= 20)
print(num_1 > 10 and num_2 < 20)

>>>
True
False

2.使用 or 检查多个条件

num_1 = 10
num_2 = 20
print(num_1 >= 10 or num_2 < 20)

>>>
True

5.2.6 检查特定值是否包含(不包含)在列表中

使用 in 来检查特定值是否包含在列表中:

table = ['apple', 'banana', 'cat', 'dog']
print('cat' in table)

>>>
True

使用 not in 来检查特定值是否不包含在列表中:

table = ['apple', 'banana', 'cat', 'dog']
print('cat' not in table)

>>>
False

5.3 if语句

5.3.1 简单的if语句

num = 22
if num >= 20:
    print("num >= 20.")

>>>
num >= 20.

5.3.2 if-else 语句

num = 18
if num >= 22:
    print("num >= 22.")
else:
    print("num < 22.")

>>>
num < 22.

5.3.3 if-elif-else 结构

table = "banana"
if table == "apple":
    print("apple")
elif table == "banana":
    print("banana")
else:
    print("cat")

>>>
banana

或者,在代码块最后再输出结果:

tables = "banana"
if tables == "apple":
    table = "apple"
elif tables == "banana":
    table = "banana"
else:
    table = "cat"
print(table)

>>>
banana

5.3.4 使用多个 elif 代码块

tables = "banana"
if tables == "apple":
    table = "apple"
elif tables == "banana":
    table = "banana"
elif tables == "dog":
    tables= "dog"
else:
    table = "cat"
print(table)

>>>
banana

5.3.5 省略 else 代码块

tables = "cat"
if tables == "apple":
    table = "apple"
elif tables == "banana":
    table = "banana"
elif tables == "dog":
    tables= "dog"
elif tables == "cat":
    table = "cat"
print(table)

>>>
cat

5.3.6 测试多个条件

如果必须检查所有条件,应使用一系列不包含 elif 和 else 代码块的if语句,如一个顾客购买了苹果和雪梨,我们需要输出这个顾客购买的明细:

item = ['苹果', '雪梨']
if '苹果' in item:
    print("购买了苹果。")
if '雪梨' in item:
    print("购买了雪梨。")
print("订单结束。")

>>>
购买了苹果。
购买了雪梨。
订单结束。

如果使用 if-elif-else 结构,代码将不能正确运行:

item = ['苹果', '雪梨']
if '苹果' in item:
    print("购买了苹果。")
elif '雪梨' in item:
    print("购买了雪梨。")
elif '西瓜' in item:
    print("购买了西瓜。")
print("订单结束。")

>>>
购买了苹果。
订单结束。

5.4 使用 if 语句处理列表

5.4.1 检查特殊元素

创建一个列表来表示顾客需要购买的水果,并使用循环来输出需求列表:

items = ['苹果', '雪梨', '西瓜']
for item in items:
    print(f"顾客需要:{item}。")
print("以上需求列表结束。")

>>>
顾客需要:苹果。
顾客需要:雪梨。
顾客需要:西瓜。
以上需求列表结束。

如果我们雪梨出现售罄,需要输出一条消息,表示雪梨已经售罄:

items = ['苹果', '雪梨', '西瓜']
for item in items:
    if item == "雪梨":
        print("雪梨已售罄!")
    else:
        print(f"顾客需要:{item}。")
print("以上需求列表结束。")

>>>
顾客需要:苹果。
雪梨已售罄!
顾客需要:西瓜。
以上需求列表结束。

5.4.2 确定列表不为空

在 if 语句中将列表名用作条件表达式时,Python 将在列表至少包含一个元素时返回 True ,并在列表为空的时候返回 False 。

item = []
if item:
    print("列表不为空")
else:
    print("列表为空")

>>>
列表为空

5.4.3 使用多个列表

创建一个 item_1 列表来表示顾客需要购买的水果,同时创建一个 item_2 列表来表示售罄的水果,并使用条件判断顾客需求列表内水果是否售罄:

item_1 = ['苹果', '雪梨', '西瓜']
item_2 = ['葡萄', '西瓜']
for item in item_1:
    if item in item_2:
        print(f"{item}已售罄!")
    else:
        print(f"顾客需要:{item}。")
print("\n以上需求列表结束。")

>>>
顾客需要:苹果。
顾客需要:雪梨。
西瓜已售罄!

以上需求列表结束。

results matching ""

    No results matching ""

    results matching ""

      No results matching ""