目录
PPT中链接为参考文献,黑色实例为jupyter notebook运行截图
分支结构
用于返回特定值,使用较少
循环结构
while(无限循环):
# Exception Handling
# asks the user for input until a valid integer has been entered,
# but allows the user to interrupt the program
# (using Control-C or I for jupyter notebook or whatever the operating system supports)
# note that a user-generated interruption is signaled by raising the KeyboardInterrupt exception.
while True:
try:
x = int(input("Please enter a integer: "))
break
except ValueError:
print("Oops! Invalid literal for int(). Try again...")
for(遍历循环):
类型:
# 循环N次
for i in range(N):
<Blocks>
# 遍历文件file的每一行
for line in file:
<Blocks>
# 遍历字符串string每个字符
for c in string:
<Blocks>
# 遍历列表list每个元素
for item in lst:
<Blocks>
range(N, M, K) : 起点(可忽略):终点(不含):步长(可忽略)
break&continue:
break退出内层循环,“打破”循环结构
continue结束当前循环,“继续”下一次循环