从第四章开始,自己做的,如有错误还望评论区告知
#例题4-1
#定义包含三个披萨名字的列表,并采用for循环打印出来
#例题4-1
#定义包含三个披萨名字的列表,并采用for循环打印出来
name_pizza = ["榴莲披萨","鸡肉披萨","水果披萨"]
for pizza in name_pizza:
print("我喜欢吃%s!" %pizza)
print('我真的喜欢吃披萨!!!')
#例题4-2
#例4-2
animal_name = ["老虎","狮子","狐狸"]
for animal in animal_name:
print('%s是人类的好朋友'%animal)
print('它们都有四条腿')
#例题4-3
#例题4-3
#使用for循环打印(1,20)
for number in range(1,21):
print(number)
#例题4-4
#例题4-4
#首先创建一个列表包含1-1000000,然后使用for循环打印出来
a = []
for number in range(1,1000001):
a.append(number)
print(a)
for x in a:
print(x)
#例题4-5
#例题4-5
#首先创建一个列表包含1-1000000,然后使用for循环打印出来
a = list(range(1,1000001))
print(min(a))
print(max(a))
print(sum(a))
#例题4-6
#例题4-6
#首先创建一个列表包含1~20内的奇数,然后使用for循环打印出来
a = list(range(1,20,2))
print(a)
for numbers in a:
print(numbers)
#例题4-7
#例题4-7
#首先创建一个列表包含3~30内能被3整除的数字,然后使用for循环打印出来
a = list(range(3,33,3))
print(a)
for numbers in a:
print(numbers)
#例题4-8
#方法1
a = []
for number in range(1,11):
b = number**3
a.append(b)
print(a)
for number in a:
print(number)
#方法2
numbers=[number**3 for number in range(1,11)]
print(numbers)
#例题4-9
#例题4-10
#例题4-11
#例题4-11
my_pizza = ["榴莲披萨","鸡肉披萨","水果披萨"]
friend_pizza = my_pizza[:]
print(friend_pizza)
#第一小问
my_pizza.append("纯芝士批萨")
print(my_pizza)
#第二小问
friend_pizza.append("夏威夷批萨")
print(friend_pizza)
#例题4-12
my_pizza = ["榴莲披萨","鸡肉披萨","水果披萨"]
friend_pizza = my_pizza[:]
print(friend_pizza)
#第一小问
my_pizza.append("纯芝士批萨")
print(my_pizza)
#第二小问
friend_pizza.append("夏威夷批萨")
print(friend_pizza)
for food in friend_pizza:
print(food)
for food in my_pizza:
print(food)
#例题4-13
menu = ("西红柿炒鸡蛋","辣椒炒肉","糖醋里脊")
print(type(menu))
for food in menu:
print(food)
#menu[0] = "番茄炒鸡蛋"
#菜单更新了
menu = ("西红柿炒鸡蛋","干锅花菜","小炒肉")
print(menu)
for food in menu:
print(food)