条件判断
score = 85
if score >= 90:
print("优秀")
elif score >= 60:
print("及格")
else:
print("不及格")
注意 if / elif / else 后面的冒号和缩进,缩进内的语句属于同一个代码块。
循环
# 遍历列表
for item in ["苹果", "香蕉", "橘子"]:
print(item)
# 重复 5 次
for i in range(5):
print(i) # 0 1 2 3 4
# while 循环
n = 3
while n > 0:
print(n)
n -= 1
break 结束整个循环,continue 跳过本次循环。循环的 else 子句只在循环正常结束(未被 break)时执行。