4-1 比萨
#4-1 比萨
lists = ['chicken','beef','Peking Duck']
for list in lists:
print("I like " + list + " pizza.")
print"\nThe chicken pizza is delicious!\nThe beef pizza tastes great!\nThe Pecking Duck pizza is a little spicy!"
print"I really love pizza!"
4-2 动物
#4-2 动物
lists = ['cat','dog','bird']
for list in lists:
print("A " + list + " would make a great pet")
print "Any of these animals would make a great pet!"
4-3 数到20
#4-3 数到20
for value in range(1,21):
print value
4-4 一百万
#4-4 一百万
numbers = list(range(1,1000001))
print numbers
4-5 计算1~1000000的总和
#4-5 计算1~1000000的总和
digits = list(range(1,1000001))
print max(digits)
print min(digits)
print sum(digits)
4-6 奇数
#4-6 奇数
even_numbers = list(range(1,21,2))
for number in even_numbers:
print number
4-7 3的倍数
#4-7 3的倍数
numbers = list(range(3,31,3))
for number in numbers:
print number
4-8 立方
#4-8 立方
cubics = []
for value in range(1,11):
cubic = value**3
cubics.append(cubic)
print cubics
4-9 立方解析
#4-9 立方解析
cubics = [value**3 for value in range(1,11)]
print cubics
4-10 切片
#4-10 切片
numbers = list(range(3,31,3))
print numbers
print("The first three items in the list are: " + str(numbers[:3]))
print("Three items from the middle of the list are: " + str(numbers[2:5]))
print("The last three items in the list are: " + str(numbers[-3:]))
4-11 你的比萨和我的比萨
#4-11 你的比萨和我的比萨
lists = ['chicken','beef','Peking Duck']
for list in lists:
print("I like " + list + " pizza.")
#创建副本
friend_pizzas = lists[:]
#在原来的比萨列表中添加
lists.append('vegetables')
#在朋友的列表中添加
friend_pizzas.append('ham')
#核实两个列表
print "\nMy favorite pizzas are:"
for list in lists:
print list
print "\nMy friend's favorite pizzas are:"
for pizza in friend_pizzas:
print pizza
4-12 使用多个循环
同4-11
4-13 自助餐
#4-13 自助餐
dimensions = ('vegetable','beef','chicken','carrot','Peking Duck')
for dimension in dimensions:
print dimension
dimensions = ('vegetable','beef','chicken','ham','Peking Duck')
print"\n"
for dimension in dimensions:
print dimension