Python编程 从入门到实践 第四章习题

4-1: 比萨
pizzas = ['seafood' , 'beef', 'cheese']
for pizza in pizzas:
    print('I like ' + pizza + ' pizza.')
print('I really like pizza!')

输出:

I like seafood pizza.
I like beef pizza.
I like cheese pizza.
I really like pizza!

4-6: 奇数

odd_number = list(range(1,20,2))
print(odd_number)
输出:
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

4-8: 立方

cube = []
for value in range(1,11):
    cube.append(value**3)

print(cube)

输出:

[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

4-9: 立方解析

cube = [value**3 for value in range(1,11)]
print(cube)

输出:

[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

4-10: 切片

pizzas = ['seafood' , 'beef', 'cheese', 'chicken','corn']

print('The first three items in the list are:')
print(pizzas[0:3])

print('\nThree items from the middle of the list are:')
print(pizzas[1:3])

print('\nThe last three items in the list are:')
print(pizzas[-3:])

输出:

The first three items in the list are:
['seafood', 'beef', 'cheese']

Three items from the middle of the list are:
['beef', 'cheese']

The last three items in the list are:
['cheese', 'chicken', 'corn']

4-11 你的比萨和我的比萨

pizzas = ['seafood' , 'beef', 'cheese']
friend_pizzas = pizzas[:]

pizzas.append('corn')
friend_pizzas.append('chicken')

print('My favourite pizzas are:')
for pizza in pizzas:
    print(pizza)

print("\nMy friend's favourite pizzas are: ")
for pizza in friend_pizzas:
    print(pizza)

输出:

My favourite pizzas are:
seafood
beef
cheese
corn

My friend's favourite pizzas are: 
seafood
beef
cheese
chicken

4-13: 自助餐

fruit_store = ('tomato', 'celery', 'leek', 'ginger', 'broccoli')

print('Original fruits:')
for fruit in fruit_store:
    print(fruit)

fruit_store = ('tomato', 'spinach', 'carrot', 'ginger', 'broccoli')
print('\nModified fruits:')
for fruit in fruit_store:
    print(fruit)

输出:

Original fruits:
tomato
celery
leek
ginger
broccoli

Modified fruits:
tomato
spinach
carrot
ginger
broccoli



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值