一,python中for语句简单遍历数据
代码:
for name in ["kak", "John", "Mani", "Matt"]:
print(name)
运行结果:
kak
John
Mani
Matt
二、python遍历数组方法
了python遍历数组,两种遍历数组的方法:
一种是直接通过for in 遍历数组;
一种是通过rang函数先获得数组长度,在根据索引遍历数组。
方式一,通过for in遍历数组:
colours = ["red","green","blue"]
for colour in colours:
print colour
# red
# green
# blue
方式二,先获得数组的长度,然后根据索引号遍历数组,同时输出索引号。
colours = ["red","green","blue"]
for i in range(0, len(colours)):
print i, colour[i]
# 0 red
# 1 green
# 2 blue