4-1比萨
pizzas=["pepperoni pizza","fruit pizza","chicken pizza","bacon pizza"]
for pizza in pizzas:
print("I like "+pizza+".")
print("I really love pizza!")
4-2动物
animals = ["dog","cat","rabbit"]
for animal in animals:
print("A "+animal+" would like to make a great pet!")
print("Any of these animals would make a great pet!")
4-3数到20
for i in range(1,21):
print(i)
4-5计算1~1000000的总和
num=range(1,1000001)
print(min(num))
print(max(num))
print(sum(num))
4-7 3的倍数
for i in range(3,31,3):
print(i)
4-8立方
num=[i**3 for i in range(1,11)]
for i in num:
print(i)
4-9立方解析
print([i**3 for i in range(1,11)])
4-10切片
num=[2,89,32,16,93,64,23,90,45]
for i in num[:3]:
print(i)
temp=int(len(num)/2)
print("The middle three items in the list are:")
for i in num[temp-1:temp+2]:
print(i)
print("The last three items in the list are:")
for i in num[-3:]:
print(i)
4-11你的比萨和我的比萨
pizzas=["pepperoni pizza","fruit pizza","chicken pizza","bacon pizza"]
friend_pizzas=pizzas[:]
pizzas.append("vegetable pizza")
friend_pizzas.append("fish pizza")
print("My favorite pizzas are:")
for a in pizzas:
print(a)
print("\nMy friend's favorite pizzas are:")
for a in friend_pizzas:
print(a)
4-13自助餐
food=("apple","rice","chicken","fish","tomato")
for i in food:
print(i)
#food[0]="banana"
food=("banana","rice","chicken","fish","potato")
for i in food:
print(i)