标签:动物 highlight number 元组 print 奇数 first 生成 一行代码
pizza = ["Zunbao","Bishengke","Bipizza"]
for i in pizza:
print(i)
print("I like pepperoni pizza\n")
print("I really love pizza")
animals = ["tiger","rabbit","panda"]
for i in animals:
print(i)
print("A dog would make a great pet\n")
print("Any of these animals would make a great pet!")
for i in range(1,20+1):
print(i)
millions = list(range(1,1000000)) #用list把数字转换成列表格式
for i in millions:
print(i)
millions = list(range(1,1000000)) print(min(millions)) print(max(millions)) print(sum(millions))
numbers= list(range(1,20,2)) #通过range的第三个参数,也就是步长为2,来获取奇数
for i in numbers:
print(i)
numbers= list(range(3,30,3))
for i in numbers:
print(i)
squares=[]
for value in range(1,10+1):
square = value**3
squares.append(square)
print(squares)
square=[value**3 for value in range(1,10+1)] print(square)
4-10 切片:选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。
a. 打印消息“The first three items in the lis tare:”,再使用切片来打印列表的前三个元素。
b. 打印消息“Three items from the middle of the list are:”,再使用切片来打印列表中间的三个元素
c. 打印消息“The last three items in the list are:”,再使用切片来打印列表末尾的三个元素
my_foods = [‘pizza‘, ‘falafel‘, ‘carrot cake‘,‘fruit‘,‘coffee‘,‘rice‘,‘meat‘]
print("The first three items in the list are:")
print(my_foods[:3])
print("\nThree items from the middle of the list are:")
print(my_foods[2:5])
print("\nThe last three items in the list are:")
print(my_foods[4:])
#输出结果:
The first three items in the list are:
[‘pizza‘, ‘falafel‘, ‘carrot cake‘]
Three items from the middle of the list are:
[‘carrot cake‘, ‘fruit‘, ‘coffee‘]
The last three items in the list are:
[‘coffee‘, ‘rice‘, ‘meat‘]
pizza = ["Zunbao","Bishengke","Bipizza"]
friend_pizzas = pizza[:]
pizza.append("Niu")
friend_pizzas.append("Liang")
print("My favorite pizzasare:")
for i in pizza:
print(i)
print("\nMy friend‘s favorite pizzasare:")
for j in friend_pizzas:
print(j)
#输出结果:
My favorite pizzasare:
Zunbao
Bishengke
Bipizza
Niu
My friend‘s favorite pizzasare:
Zunbao
Bishengke
Bipizza
Liang
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 my_food in my_foods:
print(my_food)
print("\nMy friend‘s favorite foods are:")
for friend_food in friend_foods:
print(friend_food)
#输出结果:
My favorite foods are:
pizza
falafel
carrot
cake
cannoli
My friend‘s favorite foods are:
pizza
falafel
carrot
cake
ice cream
foods =(‘rice‘,‘meat‘,‘seafood‘,‘vegetable‘,‘fruit‘)
print("The original foods are:")
for food in foods:
print(food)
#foods[0]=‘coffee‘
#print(foods[0])
foods=(‘coffee‘,‘meat‘,‘seafood‘,‘vegetable‘,‘tea‘)
print("\nThe modify foods are:")
for food in foods:
print(food)
#输出结果:
The original foods are:
rice
meat
seafood
vegetable
fruit
The modify foods are:
coffee
meat
seafood
vegetable
tea
标签:动物 highlight number 元组 print 奇数 first 生成 一行代码
原文地址:https://www.cnblogs.com/heiqiuxixi/p/12326441.html