第四章课后作业

Chapter Four

4-1 比萨

pizzas = ["Black Pepper Beef", "New Orleans", "Stuffed Crust Jumbo Shripm Crown"]
for pizza in pizzas:
    # print(pizza)
   print("I like " + pizza + " pizza.")
print("I really love the taste of pizza")

4-2 动物

animals = ["Tiger", "Lion", "Leopard"]
for animal in animals:
    # print(animal)
    print("A " + animal + " could be very dangerous.")
print("Any of these animals are very dangerous.")

4-3 数到20

for value in range(1, 21):
    print(value)

4-4 一百万

nums = list(range(1, 1000001))
for num in nums:
    print(num)

4-5 计算1 ~ 100000的总和

nums = list(range(1, 1000001))
print("Max: " + str(max(nums)))
print("Min: " + str(min(nums)))
print("Sum: " + str(sum(nums)))

4-6 奇数

odds = list(range(1, 20, 2))
for odd in odds:
    print(odd)

4-7 3的倍数

nums = list(range(3, 32, 3))
for num in nums:
    print(num)

4-8 立方

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

4-9 立方解析

nums = [value**3 for value in range(1, 11)]
for num in nums:
    print(num)

4-10 切片

nums = [value**3 for value in range(1, 11)]
for num in nums:
    print(num)
print("The first three items in  the list are:")
for num in nums[:3]:
    print(num)
print("Three items  from the middle of the list are:")
for num in nums[4:7]:
    print(num)
print("The last three items in the list are:")
for num in nums[-3:]:
    print(num)

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

pizzas = ["Black Pepper Beef", "New Orleans", "Stuffed Crust Jumbo Shripm Crown"]
for pizza in pizzas:
    # print(pizza)
   print("I like " + pizza + " pizza.")
print("I really love the taste of pizza")
friend_pizzas = pizzas[:]
pizzas.append("Strange Pizza")
friend_pizzas.append("Normal Pizza")
print("My favorite pizzas are:")
for pizza in pizzas:
    print(pizza)
print("My friend's favorite pizzas are")
for pizza in friend_pizzas:
    print(pizza)

4-12 使用多个循环

my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]

my_foods.append('cannoli')
friend_foods.append('ice cream')

print("My favorite foods are:")
for food in my_foods:
    print(food)

print("\nMy friend's favorite foods are:")
for food in friend_foods:
    print(food)

4-13 自助餐

foods = ("rice", "noodle", "bread", "beef", "pork chop")
for food in foods:
    print(food)
# foods[1] = "cake"     'tuple' object does not support item assignment
print()
foods = ("cake", "chicken", "bread", "beef", "pork chop")
for food in foods:
    print(food)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值