教材第四章课后习题

2019-04-14 17:50发布

代码: #4.1 print(4.1) list1 = ['chicken','beef','fish'] for i in list1: print("I like " + i + " pizza.") print("I really love pizza!") #4.2 print(4.2) list2 = ['dog','cat','wolf'] for i in list2: print("A " + i +" is a good pet.") print("These are all good pets.") #4.3 print(4.3) for i in range(1,21): print(i,end=' ') #4.4 print(4.4) list4 = list(range(1,1000001)) #print(list4) 太长了,就不打印了 #4.5 print(4.5) list5 = list(range(1,1000001)) print(min(list5)) print(max(list5)) print(sum(list5)) #4.6 print(4.6) list6 = list(range(1,21,2)) for i in list6: print(i,end=" ") print() #4.7 print(4.7) list7 = list(range(3,31,3)) for i in list7: print(i,end=" ") print() #4.8 print(4.8) list8 = [] for i in range(1,11): list8.append(i**3) for i in list8: print(i,end=" ") print() #4.9 print(4.9) list9 = [value**3 for value in range(1,11)] for i in list8: print(i,end=" ") print() #4.10 print('4.10') list10 = ["one","two","three","four","five"] print("first three items:",list10[:3]) print("middle three items:",list10[1:4]) print("last three items:",list10[-3:]) #4.11 print(4.11) list11 = list1[:] list1.append("rabbit") list11.append("null") print("My favorite pizzas are:") for i in list1: print(i,end=" ") print() print("My friend's favourtie pizzas are:") for i in list11: print(i,end=" ") print() #4.12 #这个题目与4.11相似,略 #4.13 print(4.13) list13 = ("one","two","three","four","five") for i in list13: print(i,end=" ") print() #list13[2] = "233" 会报错,就注释掉了,否则影响输出 list13 = ("one","two","three","4","5") for i in list13: print(i,end=" ") print() #4.14 #略 #4.15 #略 输出: 4.1 I like chicken pizza. I like beef pizza. I like fish pizza. I really love pizza! 4.2 A dog is a good pet. A cat is a good pet. A wolf is a good pet. These are all good pets. 4.3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 4.4 4.5 1 1000000 500000500000 4.6 1 3 5 7 9 11 13 15 17 19 4.7 3 6 9 12 15 18 21 24 27 30 4.8 1 8 27 64 125 216 343 512 729 1000 4.9 1 8 27 64 125 216 343 512 729 1000 4.10 first three items: ['one', 'two', 'three'] middle three items: ['two', 'three', 'four'] last three items: ['three', 'four', 'five'] 4.11 My favorite pizzas are: chicken beef fish rabbit My friend's favourtie pizzas are: chicken beef fish null 4.13 one two three four five one two three 4 5