python编程:从入门到实践 第四章课后题

4-1

pizzas = ['Seafood pizza','Sausage pizza','Cheese pizza','Beef pizza']
for pizza in pizzas:
    print(pizza)
    
for pizza in pizzas:
    print("I like " + pizza + '.')

print("I really love pizza!")

4-2

animals = ['dog','cat','bird']
for an in animals:
    print(an)
for an in animals:
    print("A " + an + " would make a great pet.")

print("Any of these animals would make a great pet!")

4-3

for i in range(1,21):
    print(i,end=' ')

4-4

numbers = list(range(1,1000001))
for i in numbers:
    print(i,end=' ') 
#ctrl + c can interrupt

4-5

import time
numbers = list(range(1,1000001))
print(min(numbers))
print(max(numbers))
t1 = time.clock()
s = sum(numbers)
t2 = time.clock()
print(s)
print(t2-t1)

4-6

for i in range(1,21,2):
    print(i,end=' ')

4-7

n3 = list(range(3,31,3))
for i in n3:
    print(i,end=' ')

4-9

n3f = [x**3 for x in range(1,11)]
for i in n3f:
    print(i,end=' ')

4-10

n3 = [x**3 for x in range(1,11)]
print("The first three items in the list are: ", n3[0:3])
print("Three items from the middle of the list are: ", n3[3:6])
print("The last three items in the list are: ",n3[-3:])

4-13

foods = ('beef','break','rice','noodle','chicken')
for food in foods:
    print(food)

foods = ('beef','pork','rice','noodle','ice cream')
for food in foods:
    print(food)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值