python列表循环

列表的循环遍历

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

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

你可能感兴趣的:(python列表循环)