例一:
打印名字5次:

print('请输入名字5次')
for i in range(5):
    i = i+1
    print("Jim:",i)

等价的while

print("输出5次名字")
i=1
while i<6:
    print("Jim:",i)
    i+=1

例二:
计算出0-100的和:

total = 0
for num in range(101):
    total += num
print(total)

例三:
计算100以内偶数的和,奇数的和:

s =0
d =0
for n in range(101):
    if n%2==0:
        s+=n
    elif n%2==1:
        d+=n
print(s)
print(d)

例四:
for循环打印出1-11

print("以下是for")
for i in range(1,11):
    print(i)

range()参数

range(5)
range(0,5)
range(0,5,1)
0为开始
5为结束(不含5)
1为步长