《Python编程从入门到实践》记录之列表遍历

实际编程中,需要遍历列表所有元素,对每个元素执行相同操作。Python中的for循环可实现列表遍历功能。

假设有一个魔术师名单的列表,需要执行将魔术师名字都打印出来的操作,这时,使用for循环可以很容易实现此功能:

magicians=['alice', 'david', 'carolina']
for magician in magicians:  # 不要忘记for循环后边的冒号!!!
    print(magician)

输出结果:

alice
david
carolina

猜你喜欢

转载自blog.csdn.net/Sophia_11/article/details/83928333