《python编程:从入门到实践》第四章练习参考答案

本文提供了《Python编程:从入门到实践》第四章的练习题参考答案,涵盖了基础语法和关键概念的应用,帮助读者巩固Python编程基础。
4-1 比萨 :想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用for 循环将每种比萨的名称都打印出来。
pizzas = ['mushrooms', 'onions', 'pineapple']

for pizza in pizzas:
	print(pizza)

#修改这个for 循环,使其打印包含比萨名称的句子,而不仅仅是比萨的名称。对于每种比萨,都显示一行输出,如“I like pepperoni pizza”。
for pizza in pizzas:
	print("I like " + pizza + "pizza.")

#在程序末尾添加一行代码,它不在for 循环中,指出你有多喜欢比萨。输出应包含针对每种比萨的消息,还有一个总结性句子,如“I really love pizza!”。
print("I really love " + pizzas[0] + 'pizza.')
print("I really love " + pizzas[1] + 'pizza.')
print("I really love " + pizzas[2] + 'pizza.')


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

for animal in animals:
	print(animal)

#修改这个程序,使其针对每种动物都打印一个句子,如“A dog would make a great pet”。
for animal in animals:
	print("A " + animal + " would make a great pet.")

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


Python编程:从入门实践》是一本适合编程初学者的书籍,书中包含大量练习题以帮助读者巩固所学知识。以下是一些章节的练习答案示例: ### 第2章:变量和简单数据类型 ```python # 2-1 message = "hello" print(message) # 2-2 message = "nihao1" print(message) message = "nihao2" print(message) # 2-3 name = "lihua" print("Hello " + name + ", would you like to learn some Python today") # 2-4 name = "hua Li" print(name + "\n" + name.title() + "\n" + name.upper() + "\n" + name.lower()) # 2-5 print('Albert Einstein once said, "A person who never made a mistake never tried anything new"') # 2-6 famous_person = "Albert Einstein" message = '"A person who never made a mistake never tried anything new"' print(famous_person + " once said," + message) # 2-7 person = "\tli hua\t" print(person + person.rstrip() + person.lstrip() + person.strip()) # 2-8 print(5 + 3) print(10 - 2) print(2 * 4) print(16 / 2) # 2-9 number = 7 print("I like " + str(number)) # 2-10 # 向大家问好 print("hello") # 2-11 import this ``` ### 第3章:列表简介 ```python # 3-1 friends = ['Alice', 'Bob', 'Charlie'] for friend in friends: print(friend) # 3-2 friends = ['Alice', 'Bob', 'Charlie'] for friend in friends: print("Hello " + friend + ", how are you?") # 3-4 guests = ['Alice', 'Bob', 'Charlie'] for guest in guests: print("Dear " + guest + ", you are invited to dinner.") # 3-5 guests = ['Alice', 'Bob', 'Charlie'] unavailable_guest = 'Bob' new_guest = 'David' guests.remove(unavailable_guest) guests.append(new_guest) for guest in guests: print("Dear " + guest + ", you are invited to dinner.") # 3-6 guests = ['Alice', 'Bob', 'Charlie'] new_guests = ['David', 'Eve', 'Frank'] guests.insert(0, new_guests[0]) guests.insert(len(guests) // 2, new_guests[1]) guests.extend(new_guests[2:]) for guest in guests: print("Dear " + guest + ", you are invited to dinner.") ``` ### 第4章:操作列表 ```python # 4-1 pizzas = ['Pepperoni', 'Cheese', 'Veggie'] for pizza in pizzas: print("I like " + pizza + " pizza.") print("I really love pizza!") # 4-2 animals = ['Dog', 'Cat', 'Bird'] for animal in animals: print("A " + animal + " would make a great pet.") print("Any of these animals would make a great pet!") # 4-3 for number in range(1, 21): print(number) # 4-4 for number in range(1, 1000001): print(number) # 4-5 numbers = list(range(1, 1000001)) print(min(numbers)) print(max(numbers)) print(sum(numbers)) # 4-6 for number in range(1, 21, 2): print(number) # 4-7 for number in range(3, 31, 3): print(number) # 4-8 cubes = [number ** 3 for number in range(1, 11)] for cube in cubes: print(cube) # 4-9 cubes = [number ** 3 for number in range(1, 11)] print(cubes) # 4-10 pizzas = ['Pepperoni', 'Cheese', 'Veggie', 'Hawaiian', 'BBQ'] print("The first three items in the list are: " + str(pizzas[:3])) print("Three items from the middle of the list are: " + str(pizzas[1:4])) print("The last three items in the list are: " + str(pizzas[-3:])) # 4-11 my_pizzas = ['Pepperoni', 'Cheese', 'Veggie'] friend_pizzas = my_pizzas[:] my_pizzas.append('Hawaiian') friend_pizzas.append('BBQ') print("My favorite pizzas are: " + str(my_pizzas)) print("My friend's favorite pizzas are: " + str(friend_pizzas)) ``` 这些示例涵盖了书中部分章节的练习答案,可以帮助读者更好地理解和掌握Python编程的基础知识。希望这些内容对你有所帮助[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值