遍历整个列表
假设我们有一个名单,需要将其中每个名字打印出来。上一节,我们是分别获取每个元素的索引,然后打印名字。如果名单很长很长,就需要大量重复代码。此时可以用到for循环。
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)
输出结果:
>>>
alice
david
carolina
我们定义了一个for循环for magician in magicians:
,这行代码将从列表magicians
中取出一个名字,存储在magician
中;对于每一个magician
,都执行print(magician)
。这就是for循环。
深入地研究for循环
Python将首先读取第一行代码:
for magician in magicians:
这行代码让Python获取列表magicians
中第一个值('alice'
),存储在magician
中,然后Python读取下一行代码:
print(magician)
Python打印magician
的值,也就是'alice'
。由于列表中还有其他元素,Python返回循环第一行。
for magician in magicians:
Python获取列表中下一个元素('david'
),再执行这行代码:
print(magician)
直到列表中所有元素被打印为止。
编写for循环时,临时变量的取名可以是任何名称,但是建议可以取有意义的名称,例如:
for cat in cats:
for dog in dogs:
for item in list_of_items:
在for循环中执行更多操作
下面扩展前面的示例,对于每个magician
执行更多的操作:夸他的表演很精彩。
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print("I can't wait to see your next trick, " + magician.title() + ".\n")
上面的两个print
都缩进了,也就是属于这个for循环,都将对magician
执行一次。\n
是换行符,会在迭代结束后,插入一个空行。
输出结果:
>>>
Alice, that was a great trick!
I can't wait to see your next trick, Alice.
David, that was a great trick!
I can't wait to see your next trick, David.
Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.
在for循环结束后执行一些操作
for循环结束后,通常要总结性输出或接着执行其他任务。下面打印一条向全体致谢的消息。这条信息只打印一次,要放在for循环之外。
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print("I can't wait to see your next trick, " + magician.title() + ".\n")
print("Thank you, everypne. That was a great show!")
输出结果:
>>>
Alice, that was a great trick!
I can't wait to see your next trick, Alice.
David, that was a great trick!
I can't wait to see your next trick, David.
Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.
Thank you, everypne. That was a great show!
常见的错误
- 忘记缩进。位于循环组成部分的代码行一定要缩进。
- 忘记缩进额外的代码行。如果上文中的两个
print
只缩进了一个,结果也会不如意。 - 不必要的缩进。如果最后一句总结性的打印也缩进了,就会循环输出。
- 遗漏了冒号。for语句末尾是有冒号的。
练习题
4-1 比萨
想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用for循环将每种比萨的名称都打印出来。
- 修改这个for循环,使其打印包含比萨名称的句子,而不仅仅是比萨的名称。对于每种比萨,都显示一行输出,如“I like pepperoni pizza”。
- 在程序末尾添加一行代码,它不在for循环中,指出你有多喜欢比萨。输出应包含针对每种比萨的消息,还有一个总结性句子,如“I really love pizza!”。
pizzas = ["french bread","baked ziti","grandma slice","new york slice","chicago pizza","puck's smoked salmon pizza"]
for pizza in pizzas:
print(pizza)
for pizza in pizzas:
print("I like " + pizza.title() + ".")
print("I really like pizza!")
输出结果:
french bread
baked ziti
grandma slice
new york slice
chicago pizza
puck's smoked salmon pizza
I like French Bread.
I like Baked Ziti.
I like Grandma Slice.
I like New York Slice.
I like Chicago Pizza.
I like Puck'S Smoked Salmon Pizza.
I really like pizza!
4-2 动物
想出至少三种有共同特征的动物,将这些动物的名称存储在一个列表中,再使用for循环将每种动物的名称都打印出来。
- 修改这个程序,使其针对每种动物都打印一个句子,如“A dog would make a great pet”。
- 在程序末尾添加一行代码,指出这些动物的共同之处,如打印诸如“Any of these animals would make a great pet!”这样的句子。
# 不写动物,写水果。最近有些缺乏维生素。
fruits = ["pineapples","apples","mongos"]
for fruit in fruits:
print(fruit.title() + " are rich in vitamins.")
print("Any of these fruits is rich in vitamins")
输出结果:
Pineapples are rich in vitamins.
Apples are rich in vitamins.
Mongos are rich in vitamins.
Any of these fruits is rich in vitamins