第四章-操作列表

匿名 (未验证) 提交于 2019-12-02 22:51:30

4-1 比萨 : 想出至少是三种你喜欢的比萨,将其名称存储在一个列表中,再使用for循环将每种比萨的名称都打印出来。

1 pizzas=['a','b','c'] 2 for pizz in pizzas: 3     print(pizz) 4 for pizza in pizzas: 5     print("I like "+pizza+" pizza.") 6 print('I really like pizza.')

输出:

1 a 2 b 3 c 4 I like a pizza. 5 I like b pizza. 6 I like c pizza. 7 I really like pizza.

4-2 动物 :想出至少三种有共同特征的动物,将这些动物的名称存储在一个列表中,再使用for 循环将每种动物的名称都打印出来。

1 animals=['cat','dog','pig'] 2 for anima in animals: 3     print(anima) 4 for animal in animals: 5     print("A "+animal+" would make a great pet.") 6 print("Any of these animals would make a great pet.")

输出:

1 cat 2 dog 3 pig 4 A cat would make a great pet. 5 A dog would make a great pet. 6 A pig would make a great pet. 7 Any of these animals would make a great pet.

1 for value in range(1,21):#range()生成一系列数字 2     print(value)

输出:

 1 1  2 2  3 3  4 4  5 5  6 6  7 7  8 8  9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20

4-4 一百万

1 numbers=list(range(1,10))#list()将range()的结果直接转换为列表。 2 print(numbers)

输出:

1 [1, 2, 3, 4, 5, 6, 7, 8, 9]

4-5 计算1~1 000 000的总和

1 numbers=list(range(1,1000001)) 2 print(min(numbers)) 3 print(max(numbers)) 4 print(sum(numbers))

输出:

1 1 2 1000000 3 500000500000

4-6 奇数

1 numbers=list(range(1,21,2)) 2 for number in numbers: 3     print(number)

输出:

 1 1  2 3  3 5  4 7  5 9  6 11  7 13  8 15  9 17 10 19

4-7 3的倍数 :创建一个列表,其中包含3~30内能被3整除的数字;再使用一个for 循环将这个列表中的数字都打印出来。

1 numbers=list(range(3,31,3)) 2 for number in numbers: 3     print(number)

输出:

 1 3  2 6  3 9  4 12  5 15  6 18  7 21  8 24  9 27 10 30

4-8 立方 :将同一个数字乘三次称为立方。例如,在Python中,2的立方用2**3 表示。请创建一个列表,其中包含前10个整数(即1~10)的立方,再使用一个for 循 环将这些立方数都打印出来。

1 numbers=list(range(1,11)) 2 for number in numbers: 3     print(number**3)

输出:

 1 1  2 8  3 27  4 64  5 125  6 216  7 343  8 512  9 729 10 1000

4-9 立方解析 :使用列表解析生成一个列表,其中包含前10个整数的立方

1 lifang=[value**3 for value in range(1,11)]#for末尾没有冒号 2 print(lifang)

输出:

1 [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

4-10 切片 :选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。

1 lifang=[value**3 for value in range(1,11)]#for末尾没有冒号 2 print(lifang) 3 print("The first three itemss in the list are:") 4 print(lifang[0:3]) 5 print("Three items from the middle of the list are:") 6 print(lifang[4:7]) 7 print('The last three items in the list are:') 8 print(lifang[-3:])

输出:

1 [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000] 2 The first three itemss in the list are: 3 [1, 8, 27] 4 Three items from the middle of the list are: 5 [125, 216, 343] 6 The last three items in the list are: 7 [512, 729, 1000]

4-11 你的比萨和我的比萨 :在你为完成练习4-1而编写的程序中,创建比萨列表的副本,并将其存储到变量friend_pizzas 中,再完成如下任务。

 1 pizzas=['a','b','c']  2 friends_pizza=pizzas[:]  3 pizzas.append('d')  4 friends_pizza.append('e')  5 print('My favorite pizzas are:')  6 for pizza in pizzas:  7     print(pizza)  8 print("My friend's favorite pizzas are: ")  9 for friend_pizza in friends_pizza: 10     print(friend_pizza)

输出:

 1 My favorite pizzas are:  2 a  3 b  4 c  5 d  6 My friend's favorite pizzas are:   7 a  8 b  9 c 10 e

4-12 使用多个循环 :在本节中,为节省篇幅,程序foods.py的每个版本都没有使用for 循环来打印列表。请选择一个版本的foods.py,在其中编写两个for 循环,将各 个食品列表都打印出来。

1 my_foods=['pizza','falafel','carrot cake'] 2 friend_foods=my_foods[:] 3 print("My favorite foods are;") 4 for my_food in my_foods: 5     print(my_food) 6 print("My friend's favorite foods are:") 7 for friend_food in friend_foods: 8     print(friend_food)

输出:

1 My favorite foods are; 2 pizza 3 falafel 4 carrot cake 5 My friend's favorite foods are: 6 pizza 7 falafel 8 carrot cake

4-13 自助餐 自助餐 :有一家自助式餐馆,只提供五种简单的食品。请想出五种简单的食品,并将其存储在一个元组中。

 1 #4-13  2 foods=('pizza','falafel','cake','cannoli','ice_cream')#要有引号  3 for food in foods:  4     print(food)  5 #foods[0]=apple  6 foods=('apple','banana','carrot_cake','cannoli','ice_cream')  7 for food1 in foods:  8     print(food1)  9 num=(250,100)#数字没有引号 10 print(num[0])

输出:

 1 pizza  2 falafel  3 cake  4 cannoli  5 ice_cream  6 apple  7 banana  8 carrot_cake  9 cannoli 10 ice_cream 11 250

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!