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: 自助餐
扫描二维码关注公众号,回复:
1873762 查看本文章
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