《Python编程:从入门到实践》第四章 操作列表 习题答案
#4.1 pizzas = ['KFC','MDL','DKS'] '''1''' for pizza in pizzas: print(pizza); '''2''' for pizza in pizzas: print("I like "+ pizza +" pizza!") '''3''' print("My favourite pizza is KFC and I really love pizza!") #4.2 animals = ['cat','tiger','dog'] '''1''' for animal in animals: print(animal) '''2''' for animal in animals: print("A " + animal +" would like a great pet!") '''3''' print("Any of these animals would like a great pet!") #4.3 for i in range(1,21): print(i) #4.4 numberList = [] for i in range(1,1000001): numberList.append(i) print(numberList) #4.5 numberList = [] for i in range(1,1000001): numberList.append(i) print(min(numberList)) print(max(numberList)) print(sum(numberList)) #4.6 oddNumber = [] for odd in range(1,21,2): oddNumber.append(odd) for odd in oddNumber: print(odd)
#4.7 divideByThere = [] for i in range(3,31,3): divideByThere.append(i) for i in divideByThere: print(i) #4.8 number = [] for i in range(1,11): number.append(i**3) for i in number: print(i) #4.9 cube = [i**3 for i in range(1,11)] print(cube)
#4.10 cube = [i**3 for i in range(1,11)] print("The first there items in the list are:" + str(cube[0:3])) print("The items from the middle of the list are:" + str(cube[4:7])) print("The last there items in the list are:" + str(cube[-3:])) #4.11 pizzas = ['KFC','MDL','DKS'] friend_pizzas = pizzas[:] print(friend_pizzas) pizzas.append('JGM') friend_pizzas.append('BSK') print("My favourite pizzas are:") for pizza in pizzas: print(pizza) print("My friend's favourite pizzas are:") for pizza in friend_pizzas: print(pizza) #4.12 略
#4.13 foods = ('noodle','humburger','pizza','donut','sanwiches') for food in foods: print(food) #foods(0) ='egg' foods = ('egg','humburger','pizza','donut','dumplings') print(foods) #4.14略 #4.15略