range
当作定义的数字范围列表。
满足顾头不顾腚,可以加步长,与for循环结合使用。
一般使用
for i in range(0, 101):
print(i)
结果:
#[0,1,2,3,....100]
for i in range(11):
print(i)
结果:
# [0,1,2,3,....10]
加步长
for i in range(0, 101, 3):
print(i)
结果:
# [0, 3, 6, 9, 12... 96, 99]
for i in range(10, 0, -1):
print(i)
结果:
# [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
l1 = [22, 33, 33, 33, 66, 77, 88, 99]
for i in l1:
print(l1.index(i))
结果:
# [0, 1, 1, 1, 4, 5, 6, 7]