For 循环
for 子项 in 列表或数组:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# For 循环
list = [1,2,3]
# 以:结尾,不要忘记了
for item in list:
print(item)
运行结果
D:\PythonProject>python run.py
1
2
3
While 循环
sum = 1+2+3+…+100,求Sum
备注:主要缩进即可,缩进识别函数块
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# While
MAX = 100
sum = 0
i = 0
while i <= MAX:
sum = sum + i
i = i + 1
# 这里的缩进是有区分的
# print(sum)
# 这里的缩进是有区分的
print(sum)
运行结果
D:\PythonProject>python run.py
5050