python列表循环

2018-08-21  本文已影响9人  握瑾怀瑜_

列表的循环遍历

为了更有效的输出列表中的数据,我们可以采用循环的方式完成。

for循环方式:

name = ['xiaoming','xiaoli','xiaozhang','xiaowang']

for x in name:

    print(x)

结果为:

xiaoming

xiaoli

xiaozhang

xiaowang

while循环方式:

name = ['xiaoming','xiaoli','xiaozhang','xiaowang']

length = len(name)

i = 0

while i < length:

    print(name[i])

    i += 1

结果为:

xiaoming

xiaoli

xiaozhang

xiaowang

上一篇下一篇

猜你喜欢

热点阅读