第三章列表简介的家庭作业。
3-1 姓名:将一些朋友的姓名存储在一个列表中,并将其命名为names。依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来。
>>> names = ['pangpang', 'huahua', 'xixi', 'chacha', 'mumu']
>>> for name in names:
... print (name)
...
结果:
pangpang
huahua
xixi
chacha
mumu
3-2 问候语: 继续使用练习3-1中的列表,但不打印每个朋友的姓名,而为每人打印一条消息。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。
>>> for name in names:
... print ("Hello, " + name + "!")
结果:
Hello, pangpang!
Hello, huahua!
Hello, xixi!
Hello, chacha!
Hello, mumu!
3-3 自己的列表: 想想你喜欢的通勤方式,如骑摩托车或开汽车,并创建一个包含多种通勤方式的列表。根据该列表打印一系列有关这些通勤方式的宣言,如“I would like to own a Honda motorcycle”。
>>> traf = ['bicycle', 'train', 'subway', 'plane', 'car']
>>> print ("I would like to travel all over the world by " + traf[0])
I would like to travel all over the world by bicycle
>>> print ("I would like to travel all over the world by " + traf[1])
I would like to travel all over the world by train
>>> print ("I would like to travel all over the world by " + traf[2])
I would like to travel all over the world by subway
>>> print ("I would like to travel all over the world by " + traf[3])
I would like to travel all over the world by plane
>>> print ("I would like to travel all over the world by " + traf[4])
I would like to travel all over the world by car