python第四章上机实验_Python:从入门到实践--第四章--列表操作--练习

#1.想出至少三种你喜欢的水果,将其名称存储在一个列表中,再使用for循环将每种水果的名称都打印出来。#要求:(1)修改这个for循环,使其打印包含名称的句子,而不是仅仅是水果的名称。对于每种水果,都显示一行输出。例如:I like apple#(2)在程序末尾添加一行代码,它不在for循环中,指出你有多喜欢水果,输出应包含针对每种水果的消息,还有一个总结性性句子,如I really love fruits!

fruits= ['apple','banana','orange','watermelon']for fruit infruits:print('I like'+ fruit + '\n')print('I really love fruits!' + '\n')#2.想出至少三种有共同特征的动物,将这些动物的名称存储在一个列表中,在使用for循环将每种动物的名称都打印出来#要求:(1)修改这个程序,使其针对每种动物打印一个句子,如"A dog would make a great pet"#(2)在程序的末尾添加一行代码,指出这些动物的共同之处。

animals= ['lion','tiger','leopard']for animal inanimals:print(animal + "like eating mealt." + '\n')print("Any of these animals are fierce!")#1.数到20:使用一个for循环打印数字1-20(含)

for number in range(1,21):print(number ,end = ' ')print('\n')#2.一百万:创建一个列表,其中包含数字1-1000,再使用一个for循环将这些数字打印出来;再使用min()和max()核实该列表确实是从1开始,到1000结束#另外,对于这个列表调用sum()函数,看看python将一百万数字相加徐亚多长时间#numbers = list(range(1,1000001))#for number in numbers:#print(number,end = ' ')

#print('\n')#print(min(numbers))#print(max(numbers))#print(sum(numbers))

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

odds_number = list(range(1,21,2))for odd inodds_number:print(odd,end=' ')print('\n')#4的倍数:创建一个列表,其中包含3-30内能被3整除的数字;再使用一个for循环将这个列表中的数字都打印出来。

numbers = range(3,31,3)for number innumbers:print(number,end=' ')print('\n')#5.立方:使用列表解析生成一个列表,其中包含前10个整数的立方,在使用一个for循环将这些数字打印出来

numbers = [values**3 for values in range(1,11)]for cube innumbers:print(cube,end = " ")#1.切片:选择在本章编写的程序,在末尾添加几行代码,完成如下要求#打印消息“The first three items in the list are:”,再使用切片来打印列表中间的三个元素#打印消息“Three items from the middle of the list are :”.再使用切片来打印列表中间的三个元素#打印消息“The last three items in the list are :”,再使用切片来打印列表末尾的三个元素

foods= ['rice','noodle','apple','laoganma','cake','bread','ice cream']print("The first three items in the list are:")print(foods[0:3])print("Three items from the middle of the list are:")print(foods[2:5])print("The last three items in the list are:")print(foods[-3:])print('\n')#2.你的水果和我的水果:创建水果列表副本,并将其存储到变量friend_fruits中,完成如下要求#在原来的水果列表中添加另一种水果#在friend_fruits列表中添加另一种水果#核实你有两个不同的列表#使用两个for循环,将各个食品列表都打印出来

My_fruits= ['apple','banana','watermelon']

Friend_fruits=My_fruits[:]

My_fruits.append('orange')

Friend_fruits.append('strawberry')print("My favorite fruits are:")print(My_fruits)print("My friend's favorite fruits are:")print(Friend_fruits)for my inMy_fruits:print(my)for friend inFriend_fruits:print(friend)#食物:将任意五种食物存储在一个元组#尝试修改其中的一个元素,核实python确实拒绝你这样做#替换其中的两个食物,并用for循环将他们打印出来

foods= ('rice','meat','apple','orange juice','milk')print(foods)#foods[1]='water'

foods= ('rice','meat','apple','ice cream','water')print(foods)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值