
条件语句(if 语句):
条件语句用于根据条件的真假执行不同的代码块。
x = 10if x > 0:  # 如果 x 大于 0print("x 是正数")  # 输出:x 是正数
elif x == 0:  # 如果 x 等于 0print("x 是零")
else:  # 如果以上条件都不满足print("x 是负数")循环语句(for 循环和 while 循环):
循环语句用于重复执行特定代码块。
 for 循环:
fruits = ["apple", "banana", "cherry"]for fruit in fruits:  # 遍历列表 fruits 中的元素print(fruit)  # 输出列表中的水果
# 输出结果:
# apple
# banana
# cherrywhile 循环:
i = 1
while i <= 5:  # 当 i 小于等于 5 时执行循环print(i)  # 输出 i 的值i += 1  # i 自增 1
# 输出结果:
# 1
# 2
# 3
# 4
# 5循环控制语句(break、continue 和 pass):
循环控制语句用于控制循环的执行流程。
 break:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:  # 遍历列表 fruits 中的元素print(fruit)  # 输出列表中的水果if fruit == "banana":  # 如果当前水果是 bananabreak  # 终止循环
# 输出结果:
# apple
# bananacontinue:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:  # 遍历列表 fruits 中的元素if fruit == "banana":  # 如果当前水果是 bananacontinue  # 跳过本次循环,继续下一次循环print(fruit)  # 输出列表中的水果(除了 banana)
# 输出结果:
# apple
# cherrypass:
 pass 语句是空语句,用于保持程序结构的完整性,不做任何操作。
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:  # 遍历列表 fruits 中的元素if fruit == "banana":pass  # pass 语句,不执行任何操作else:print(fruit)  # 输出列表中的水果(除了 banana)
# 输出结果:
# apple
# cherry异常处理语句(try-except 语句):
异常处理语句用于捕获和处理代码中的异常情况。
try:x = 10 / 0  # 尝试执行可能引发异常的代码
except ZeroDivisionError:  # 捕获 ZeroDivisionError 异常print("除零错误发生")  # 输出异常信息
# 输出结果:
# 除零错误发生