流程控制
条件判断 (If…Else)
Section titled “条件判断 (If…Else)”Python 支持常见的逻辑条件。
a = 33b = 200
if b > a: print("b is greater than a")elif a == b: print("a and b are equal")else: print("a is greater than b")if a > b: print("a is greater than b")简写 If…Else
Section titled “简写 If…Else”print("A") if a > b else print("B")While 循环
Section titled “While 循环”只要条件为真,while 循环就会重复执行代码块。
i = 1while i < 6: print(i) i += 1break 与 continue
Section titled “break 与 continue”break: 停止循环continue: 停止当前迭代,继续下一次迭代
i = 1while i < 6: if i == 3: break i += 1For 循环
Section titled “For 循环”用于遍历序列(列表、元组、字典、集合或字符串)。
fruits = ["apple", "banana", "cherry"]for x in fruits: print(x)range() 函数
Section titled “range() 函数”比如生成一个数字序列。
for x in range(6): print(x) # 打印 0 到 5