0%

Python之流程控制语句

这里介绍Python中的if、for、while语句

abstract.png

条件测试

常见地条件测试如下所示。特别地,Python支持链式比较

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
print("--------------------- 条件测试 ----------------------------")

# 判断值是否相等、不等
print('"Aaron" == "Aa"+"ron" : ', "Aaron" == "Aa"+"ron")
print('"Aaron" != "Bob" : ', "Aaron" != "Bob")

# 数学比较
print("2 != 8 : ", 2 != 8)
print("2 < 8 : ", 2 < 8)
print("2 <= 8 : ", 2 <= 8)
print("8 <= 8 : ", 8 <= 8)
print("8 > 2 : ", 8 > 2)
print("8 >= 2 : ", 8 >= 2)
print("2 >= 2 : ", 2 >= 2)

# 链式比较
aaron_age = 35
aaron_is_young = 18 <= aaron_age < 60
print("aaron is young ? ", aaron_is_young)


# 逻辑与 and
print("(2==2) and (1<9) : ", (2==2) and (1<9) )
print("(2!=2) and (1<9) : ", (2!=2) and (1<9) )
# 逻辑或 or
print("(2>996) or (11<9) : ", (2>996) or (11<9) )
print("(2!=2) or (1<9) : ", (2!=2) or (1<9) )
# 逻辑非 not
print("not (3>1) : ", not(3>1) )
print("not (3>9) : ", not(3>9) )

# in、not in 成员运算符:判断指定值是否 在/不在 容器中
names = ("Aaron", "Tony")
print("Tony in names : ", "Tony" in names)
print("Bob not in names : ", "Bob" not in names)
ages = [28, 25, 37]
print("25 in ages : ", 25 in ages)
print("99 not in ages : ", 99 not in ages)
citys = {"Beijing", "Shanghai"}
print("Beijing in citys : ", "Beijing" in citys)
print("Hangzhou not in citys : ", "Hangzhou" not in citys)

# is、is not 身份运算符:判断两个变量 是否指向 同一个对象/不同对象
a = 4
b = a
print('a(4) is b(4) : ', a is b)
# 也可以通过id()函数获取所指向对象的内存地址进行比较
print('id(a) == id(b) : ', id(a) == id(b))

m = [1,2,3]
n = [1,2,3]
print("m:", m)
print("n:", n)
print('m == n : ', m == n)
print('m is n : ', m is n)
print('m is not n : ', m is not n)
# 也可以通过id()函数获取所指向对象的内存地址进行比较
print('id(m) != id(n) : ', id(m) != id(n))

figure 1.png

if语句

Python中的if语句在功能上和其他语言没有什么区别。支持if、if-else、if-elif-else等形式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
print("------------------ if语句 --------------------")

# if 语句
if 996 > 995 :
print("Bye, Aaron")

# if-else 语句
if 996 < 995 :
print("Hello ", end="")
print("996 < 995")
else:
print("Hi ", end="")
print("996 > 995")

# if-elif-else语句
age = 12
if age < 4:
print("Hi ", end="")
print("Your cost is $0")
elif age < 18:
print("Hello ", end="")
print("Your cost is $11.11")
elif age < 35:
print("Oh, ", end="")
print("Your cost is $33")
else: # 这里 else 子句是可选的,不是必须的
print("GG ", end="")
print("Your cost is $996")

figure 2.png

特别地,对于if、while语句的条件表达式,对于True/False的判定存在下述规则

  • 对于数字0、空值 None、单引号空字符串’’、双引号空字符串””、空列表[]、空元组()、空字典{}、空集合set() 都会被视为False
  • 对于非零数字、非空单引号空字符串、非空双引号空字符串、非空列表、非空元组、非空字典、非空集合 都会被视为True
1
2
3
4
if "" :
print("Bye, Aaron")
else:
print("This is a Bad News")

figure 3.png

while语句

Python的while语句在功能上与其他语言并无无区别

1
2
3
4
5
6
7
8
9
10
11
12
print("-------------while语句----------------------")

count = 0
while count<15:
count += 1
if count==2:
# continue语句: 提前结束本次循环,继续执行下次循环
continue
if count==7:
# break语句: 直接退出当前循环
break
print("count -->>", count)

figure 4.png

特别地,Python中的while语句还可以使用else子句,其仅在while循环中没有调用break时才会执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
print("---------------while-else语句-----------------------------")
nums =[1,3,5,7,9,12]
index = 0
while index < len(nums):
num = nums[index]
index += 1
if( num%2==0 ):
print("[break]: 发现偶数")
break
else:
print("[else]: 未发现偶数!!")

print("--------------------------------------------------")

index = 0
nums =[2,4,6,8,10]
while index < len(nums):
num = nums[index]
index += 1
if( num%2==1 ):
print("[break]: 发现奇数")
break
else:
print("[else]: 未发现奇数!!")

figure 5.png

for语句

Python的for语句仅可用于遍历可迭代对象

1
2
3
4
5
6
7
8
print("--------------------------for语句--------------------------")

names = ["Aaron", "David", "Bob"]
for name in names:
print("name ---->>>",name)

for index in range(3):
print("index : ", index, " --->>> ", names[index])

figure 6.png

特别地,Python中的for语句还可以使用else子句,其仅在for循环中没有调用break时才会执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
print("--------------------------for-else语句--------------------------")

nums =[1,3,5,7,9,12]
for num in nums:
if( num%2==0 ):
print("[break]: 发现偶数")
break
else:
print("[else]: 未发现偶数!!")

print("----------------------------------------")

nums =[2,4,6,8,10]
for num in nums:
if( num%2==1 ):
print("[break]: 发现奇数")
break
else:
print("[else]: 未发现奇数!!")

figure 7.png

参考文献

  1. Python编程·第3版:从入门到实践 Eric Matthes著
  2. Python基础教程·第3版 Magnus Lie Hetland著
  3. 流畅的Python·第1版 Luciano Ramalho著
请我喝杯咖啡捏~

欢迎关注我的微信公众号:青灯抽丝