Python习题三--4.1~4.13

4-1 比萨比:想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用for 循环将每种比萨的名称都打印出来。
··修改这个for 循环,使其打印包含比萨名称的句子,而不仅仅是比萨的名称。对于每种比萨,都显示一行输出,如“I like pepperoni pizza”。
··在程序末尾添加一行代码,它不在for 循环中,指出你有多喜欢比萨。输出应包含针对每种比萨的消息,还有一个总结性句子,如“I really love pizza!”。

pizzas=['heijiao','jirou','liulian']
for pizza in pizzas:
    print('I like '+pizza+' pizza')
print('I really like pizza')

I like heijiao pizza
I like jirou pizza
I like liulian pizza
I really like pizza

Process finished with exit code 0

4-2 动物 :想出至少三种有共同特征的动物,将这些动物的名称存储在一个列表中,再使用for 循环将每种动物的名称都打印出来。
··修改这个程序,使其针对每种动物都打印一个句子,如“A dog would make a great pet”。
··在程序末尾添加一行代码,指出这些动物的共同之处,如打印诸如“Any of these animals would make a great pet!”这样的句子

pets=['dog','rabbit','cat']
for pet in pets:
    print('A '+pet+ ' would make a great pet ')
print('Any of these animals would make a great pet!')

A dog would make a great pet 
A rabbit would make a great pet 
A cat would make a great pet 
Any of these animals would make a great pet!

Process finished with exit code 0

4-3 使用一个for 循环打印数字1~20(含)。

for number in range(1,21):
    print(number)

4-4 一百万 :创建一个列表,其中包含数字1~1 000 000,再使用一个for 循环将这些数字打印出来(如果输出的时间太长,按Ctrl+ C停止输出,或关闭输出窗口)。

numbers=list(range(1,1000001))
print(numbers)

但打印出来的并没有显示1~1000000,我们需要对pycharm进行一些设置
在这里插入图片描述
Pycharm软件的控制台周期缓冲区大小默认是1024KB,存不下这么多数(64位Python,数字“0”占用内存24个字节,数字“1”占用内存28个字节)
所以我们需要扩大内存,在系统默认的1024Kb后加个0,即可输出1~1000000个数。
在这里插入图片描述
4-5 计算 1~1 000 000的总和 :创建一个列表,其中包含数字1~1 000 000,再使用min() 和max() 核实该列表确实是从1开始,到1 000 000结束的。另外,对这个列表 调用函数sum() ,看看Python将一百万个数字相加需要多长时间

import time
numbers=list(range(1,1000001))
print(min(numbers))
print(max(numbers))
starttime = time.time()
print(sum(numbers))
endtime=time.time()
duringtime=endtime-starttime
print (duringtime)

1
1000000
500000500000
0.04101276397705078

Process finished with exit code 0

因为第四章还没有学习到函数,这道题也可以通过定义函数来计算时间
这是我参照其它代码写的,但最后运行的时间会不同,这个代码的运行时间会稍微少一点,不知道为什么

import time
def test():
    numbers=list(range(1,1000001))
    print(min(numbers))
    print(max(numbers))
    starttime = time.time()
    print(sum(numbers))
    endtime=time.time()
    duringtime=endtime-starttime
    print (duringtime)
test()
1`在这里插入代码片`
1000000
500000500000
0.032007455825805664

Process finished with exit code 0

4-6 奇数:通过给函数range() 指定第三个参数来创建一个列表,其中包含1~20的奇数;再使用一个for 循环将这些数字都打印出来。

numbers=list(range(1,21,2))
print(numbers)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

Process finished with exit code 0

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

numbers=list(range(3,31,3))
print(numbers)
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]

Process finished with exit code 0

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

numbers=[]
for value in range(1,11):
    numbers.append(value**3)
print(numbers)   #注意不要留空格
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

Process finished with exit code 0

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

numbers=[value**3 for value in range(1,11)]
print(numbers)
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

Process finished with exit code 0

4-10 切片:选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。
··打印消息“Thefirst threeitems in thelistare:”,再使用切片来打印列表的前三个元素。
·· 打印消息“Threeitems fromthe middle ofthelistare:”,再使用切片来打印列表中间的三个元素。
··打印消息“Thelast threeitems in thelistare:”,再使用切片来打印列表末尾的三个元素。

pets=['dog','rabbit','cat','wolf','duck']
print('The first three are:')
for pet in pets[:3]:
    print(pet.title())
print('The items from the middle are:')
for pet in pets[1:4]:
    print(pet.title())
print('The last three items are:')
for pet in pets[-3:]:
    print(pet.title())
The first three are:
Dog
Rabbit
Cat
The items from the middle are:
Rabbit
Cat
Wolf
The last three items are:
Cat
Wolf
Duck

Process finished with exit code 0

4-11 你的比萨和我的比萨 :在你为完成练习4-1而编写的程序中,创建比萨列表的副本,并将其存储到变量friend_pizzas 中,再完成如下任务。
··在原来的比萨列表中添加一种比萨。
·· 在列表friend_pizzas 中添加另一种比萨。 核实你有两个不同的列表。
··为此,打印消息“My favorite pizzas are:”,再使用一个for 循环来打印第一个列表;打印消息“My friend’s favorite pizzas are:”,再使用一 个for 循环来打印第二个列表。核实新增的比萨被添加到了正确的列表中。

my_pizzas=['heijiao','jirou','liulian']
friend_pizzas=my_pizzas[:]
my_pizzas.append('fruit')
friend_pizzas.append('jipa')
print('My favorite pizzas are:')
for pizza in my_pizzas:
   print(pizza)
print("\nMy friend's favorite pizzas are:")
for pizza in friend_pizzas:
   print(pizza)
My favorite pizzas are:
heijiao
jirou
liulian
fruit

My friend's favorite pizzas are:
heijiao
jirou
liulian
jipa

Process finished with exit code 0

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

my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
print("My favorite foods are:")
for food in my_foods:
   print(food)
print("\nMy friend's favorite foods are:")
for food in friend_foods:
   print(food)
My favorite foods are:
pizza
falafel
carrot cake

My friend's favorite foods are:
pizza
falafel
carrot cake

Process finished with exit code 0

4-13 自助餐 :有一家自助式餐馆,只提供五种简单的食品。请想出五种简单的食品,并将其存储在一个元组中。
··使用一个for 循环将该餐馆提供的五种食品都打印出来。
··尝试修改其中的一个元素,核实Python确实会拒绝你这样做。
··餐馆调整了菜单,替换了它提供的其中两种食品。请编写一个这样的代码块:给元组变量赋值,并使用一个for 循环将新元组的每个元素都打印出来。

food=('cabbage','potato','tomato','chicken','fish')
for i in food:
    print(i)
food=('A','B','tomato','chicken','fish')
for i in food:
    print(i)
food[0]=1
Traceback (most recent call last):
  File "D:/pythonProject/first.py", line 7, in <module>
    food[0]=1
TypeError: 'tuple' object does not support item assignment
cabbage
potato
tomato
chicken
fish
A
B
tomato
chicken
fish

Process finished with exit code 1

在本章中,你学习了:如何高效地处理列表中的元素;如何使用for 循环遍历列表,Python如何根据缩进来确定程序的结构以及如何避免一些常见的缩进错误;如何创建简单的 数字列表,以及可对数字列表执行的一些操作;如何通过切片来使用列表的一部分和复制列表。你还学习了元组(它对不应变化的值提供了一定程度的保护),以及在代码变得 越来越复杂时如何设置格式,使其易于阅读

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

看星河的兔子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值