《Python编程:从入门到实践》第四章 操作列表 习题答案

#4.1
pizzas = ['KFC','MDL','DKS']
'''1'''
for pizza in pizzas:
    print(pizza);
'''2'''
for pizza in pizzas:
    print("I like "+ pizza +" pizza!")
'''3'''
print("My favourite pizza is KFC and I really love pizza!")

#4.2
animals = ['cat','tiger','dog']
'''1'''
for animal in animals:
    print(animal)
'''2'''
for animal in animals:
    print("A " + animal +" would like a great pet!")
'''3'''
print("Any of these animals would like a great pet!")

#4.3
for i in range(1,21):
    print(i)

#4.4
numberList = []
for i in range(1,1000001):
    numberList.append(i)
print(numberList)

#4.5
numberList = []
for i in range(1,1000001):
    numberList.append(i)
print(min(numberList))
print(max(numberList))
print(sum(numberList))

#4.6
oddNumber = []
for odd in range(1,21,2):
    oddNumber.append(odd)
for odd in oddNumber:
    print(odd)
#4.7
divideByThere = []
for i in range(3,31,3):
    divideByThere.append(i)
for i in divideByThere:
    print(i)

#4.8
number = []
for i in range(1,11):
    number.append(i**3)
for i in number:
    print(i)

#4.9
cube = [i**3 for i in range(1,11)]
print(cube)
#4.10
cube = [i**3 for i in range(1,11)]
print("The first there items in the list are:" + str(cube[0:3]))
print("The items from the middle of the list are:" + str(cube[4:7]))
print("The last there items in the list are:" + str(cube[-3:]))

#4.11
pizzas = ['KFC','MDL','DKS']
friend_pizzas = pizzas[:]
print(friend_pizzas)
pizzas.append('JGM')
friend_pizzas.append('BSK')
print("My favourite pizzas are:")
for pizza in pizzas:
    print(pizza)
print("My friend's favourite pizzas are:")
for pizza in friend_pizzas:
    print(pizza)

#4.12
#4.13
foods = ('noodle','humburger','pizza','donut','sanwiches')
for food in foods:
    print(food)
#foods(0) ='egg'
foods = ('egg','humburger','pizza','donut','dumplings')
print(foods)

#4.14略
#4.15略

 

转载于:https://www.cnblogs.com/zijeak/p/11473686.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值