Python编程:从入门到实践 动手试一试 4.1-4.13

#4.1 pizza

pizzas = ['durian', 'fruit', 'sausage', 'beef', 'pepperoni']
for pizza in pizzas:
    print("I like " + pizza + " pizza!")
print("I really love pizza!")

#4.2 animals

animals = ['dog', 'cat', 'snake', 'puppy', 'kitten']
for animal in animals:
    print("A " + animal + " would make a great pet for people.")
print("Any of these animals would make a great pet!"

#4.3 counting to 20

numbers = []
for number in range(1,21):
    numbers.append(number)
print(numbers)

numbers_2 = list(range(1,21))
print(numbers_2

#4.4 a million

a_million_list = []
for number in range(1,10000001)
	a_million_list.append(number)
print(a_million_list)

#4.5 calculating the sum of a million

a_million = list(range(1,10000001))
print(min(a_million))
print(max(a_million))
print(sum(a_million)

#4.6 odd number

odd_number = []
for odd in range(1,20,2):
    odd_number.append(odd)
print(odd_number

#4.7 the truple of 3

truple_of_three = []
for truple in range(3,31,3):
    truple_of_three.append(truple)
print(truple_of_three

#4.8 cube

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

#4.9 cubic parsing

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

#4.10 slice

pizzas = ['durian', 'fruit', 'sausage', 'beef', 'pepperoni']
print("The first three items in the list are: ")
print(pizzas[0:4])

print("Three items from the middle of the list are: ")
print(pizzas[1:4])

print("The last three items in the list are: ")
print(pizzas[-3:]

#4.11 your pizzas and my pizzas

friend_pizzas = pizzas[:]

pizzas.append("pork")
friend_pizzas.append("apple")

print("My favorite pizzas are: ")
for pizza in pizzas:
    print(pizza.title())

print("\nMy friend's favorite pizzas are: ")
for friend in friend_pizzas:
    print(friend.title())

#4.12 using multiple loop

#unnecessarily to do so, repeatitive work.

#4.13 buffet

foods = ('beef', 'lamb', 'vegetable', 'apple', 'banana')
for food in foods:
    print(food)

print("\nThe original foods are: ")
print(foods)

foods = ('beef', 'lamb', 'chicken', 'grape', 'banana')
print("\nThe modified foods are: ")
for food in foods:
    print(food)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值