Python编程从入门到实践(第四章练习)

以下代码如有错误,希望大家批评指出,谢谢~

  • 练习4-1:比萨 想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用for循环将每种比萨的名称打印出来。
food = ['sweet','salt','pepperoni']
for pizza in food:
    print(pizza)

·修改这个for循环,使其打印包含比萨名称的句子,而不仅仅是比萨的名称。对于每种比萨,都显示一行输出,下面是一个例子。
I like pepperoni pizza.

food = ['sweet','salt','pepperoni']
for pizza in food:
    print(f"I like {pizza} pizza")

·在程序末尾添加一行代码,它不在for循环中,指出你有多喜欢比萨。输出应包含针对每种比萨的消息,还有一个总结性句子,下面是一个例子。
I really love pizza!

food = ['sweet','salt','pepperoni']
for pizza in food:
    print(f"I like {pizza} pizza")
print("\nI really love pizza")
  • 练习4-2:动物 想出至少三种有共同特征的动物,将其名称存储在一个列表中,再使用for循环将每种动物的名称打印出来。
animals = ['panda','dog','cat']
for animal in animals:
    print(animal)

·修改这个程序,使其针对每种动物都打印一个句子,下面是一个例子。
A dog would make a great pet.

animals = ['panda','dog','cat']
for animal in animals:
    print(f"A {animal} would make a great pet")

·在程序末尾添加一行代码,指出这些动物的共同之处,如打印下面这样的句子。
Any of these animals would make a great pet!

animals = ['panda','dog','cat']
for animal in animals:
    print(f"A {animal} would make a great pet")
print("\nAny of these animals would make a great pet!")
  • 练习4-3:数到20 使用一个for循环打印数1~20(含)
for a in range(20):
    print(a+1)
  • 练习4-4:一百万 创建一个包含数1~1000000的列表,再使用一个for循环将这些数打印出来。
numbers = list(range(1,1000000))
for number in numbers:
    print(number)
  • 练习4-5:一百万求和 创建一个包含数1~1000000的列表,再使用min()和max()核实该列表是从1开始、到1000000结束的。另外,对这个列表调用函数sum(),看看Python将一百万个数相加需要多长时间。
numbers = list(range(1,1000000))
a = min(numbers)
b = max(numbers)
print(a)
print(b)
c = sum(numbers)
print(c)
  • 练习4-6:奇数 通过给函数range()指定第三个参数来创建一个列表,其中包含1~20的奇数,再使用一个for循环将这些数打印出来。
numbers = list(range(1,20,2))
for number in numbers:
    print(number)
  • 练习4-7:3的倍数 创建一个列表,其中包含3~30能被3整除的数,再使用一个for循环将这个列表中的数打印出来。
numbers = list(range(3,31,3))
for number in numbers:
    print(number)
  • 练习4-8:立方 将同一个数乘三次称为立方。例如,在python中,2的立方用2**3表示。请创建一个列表,其中包含前10个整数(1~10)的立方,再使用一个for循环将这些立方数打印出来。
squares = []
for value in range(1,11):
    squares.append(value**3)
    
for num in squares:
    print(num)
  • 练习4-9:立方解析 使用列表解析生成一个列表,其中包含前10个整数的立方。
squares = [value**3 for value in range(1,11)]
    
for num in squares:
    print(num)
  • 练习4-10:切片 选择你在本章编写的一个程序,在末尾添加几行代码,以完成以下任务。
    ·打印消息"The first three items in the list are:",再使用切片来打印列表的前三个元素。
items = ['book','pen','errase','cup','lamp']
print("The first three items in the list are:")
for item in items[:3]:
    print(item)

·打印消息"Three items from the middle of the list are:",再使用切片来打印列表的中间三个元素。

items = ['book','pen','errase','cup','lamp']
print("Three items from the middle of the list are:")
for item in items[1:4]:
    print(item)

·打印消息"The last three items in the list are:",再使用切片来打印列表的末尾三个元素。

items = ['book','pen','errase','cup','lamp']
print("The last three items in the list are:")
for item in items[-3:]:
    print(item)
  • 练习4-11:你的比萨,我的比萨 在你为完成练习4-1而编写的程序中,创建比萨列表的副本,并将其赋给变量friend_pizzas,再完成如下任务。
food = ['sweet','salt','pepperoni']
friend_pizzas = food[:]

·在原来的比萨列表中添加一种比萨。

food = ['sweet','salt','pepperoni']
friend_pizzas = food[:]
food.append('carrot')

·在列表friend_pizzas中添加另一种比萨。

food = ['sweet','salt','pepperoni']
friend_pizzas = food[:]
food.append('carrot')
friend_pizzas.append('fruit')

·核实有两个不同的列表。为此,打印消息"My favorite pizzas are:",再使用一个for循环来打印第一个列表;打印消息"My friend’s favorite pizzas are:",再使用一个for循环来打印第二个列表。核实新增的比萨被添加到了正确的列表中。

food = ['sweet','salt','pepperoni']
friend_pizzas = food[:]
food.append('carrot')
friend_pizzas.append('fruit')
print("My favorite pizzas are:")
for a in food:
    print(a)
    
print("\nMy friend's favorite pizzas are:")
for b in friend_pizzas:
    print(b)
  • 练习4-12:使用多个循环 在本节中,为节省篇幅,程序foods.py的每个版本都没有使用for循环来打印列表。请选择一个版本的foods.py,在其中编写两个for循环,将各个食品列表打印出来。
my_foods = ['pizza','falafel','carrot cake']
friend_foods = my_foods[:]
print("My favorite foods are:")
for a in my_foods:
    print(a)
    
print("\nMy friend's favorite foods are:")
for b in friend_foods:
    print(b)
  • 练习4-13:自助餐 有一家自助式餐馆,只提供五种简单的食品。请想出五种简单的食品,并将其存储在一个元组中。
foods = ('pizza','rice','noddle','soup','cake')

·使用一个for循环将该餐馆提供的五种食品都打印出来。

foods = ('pizza','rice','noddle','soup','cake')
for food in foods:
    print(food)

·尝试修改其中的一个元素,核实Python确实会拒绝你这样做。
在这里插入图片描述
·餐馆调整了菜单,替换了它提供的其中两样食品。请编写一个这样的代码块:给元组变量赋值,并使用一个for循环将新元组的每个元素打印出来。

foods = ('pizza','rice','noddle','soup','cake')
foods = ('coffee','rice','noddle','soup','carrot')
for food in foods:
    print(food)
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值