《Python编程:从入门到实践》第三章 列表简介 习题答案

#3.1 names=[\'lpr\',\'tjl\',\'gnl\',\'by\',\'dqy\']; print(names[0]); print(names[1]); print(names[2]); print(names[3]); print(names[4]); #3.2 print(names[0]+",Hello!"); print(names[1]+",Hello!"); print(names[2]+",Hello!"); print(names[3]+",Hello!"); print(names[4]+",Hello!"); #3.3 transport=[\'bike\',\'high-way\',\'bus\',\'taxi\',\'plane\']; print("I would like to own a "+transport[0]); print("I would like to own a "+transport[1]); print("I would like to own a "+transport[2]); print("I would like to own a "+transport[3]); print("I would like to own a "+transport[4]); #3.4 friends=[\'by\',\'xdy\',\'lyx\']; print("Hello,"+friends[0]+"! I would like to invent you to have dinner with me!"); print("Hello,"+friends[1]+"! I would like to invent you to have dinner with me!"); print("Hello,"+friends[2]+"! I would like to invent you to have dinner with me!"); #3.5 print(friends[1]+" cannot come here for dinner!"); friends[1]=\'dqy\'; print("Hello,"+friends[0]+"! I would like to invent you to have dinner with me!"); print("Hello,"+friends[1]+"! I would like to invent you to have dinner with me!"); print("Hello,"+friends[2]+"! I would like to invent you to have dinner with me!"); #3.6 print("Hello,my friends!I have got a bigger desk for dinner just now,let\'s invent more 3 fridens!"); friends.insert(0,\'lpr\'); friends.insert(2,\'nsx\'); friends.append(\'gj\'); print("Hello,"+friends[0]+"! I would like to invent you to have dinner with me!"); print("Hello,"+friends[1]+"! I would like to invent you to have dinner with me!"); print("Hello,"+friends[2]+"! I would like to invent you to have dinner with me!"); print("Hello,"+friends[3]+"! I would like to invent you to have dinner with me!"); print("Hello,"+friends[4]+"! I would like to invent you to have dinner with me!"); print("Hello,"+friends[5]+"! I would like to invent you to have dinner with me!"); #3.7 print("Sorry,I could only invent 2 friends for dinner!"); sorry=friends.pop(); print(sorry+",I am so sorry that you can\'t have dinner with me tonight!"); sorry=friends.pop(); print(sorry+",I am so sorry that you can\'t have dinner with me tonight!"); sorry=friends.pop(); print(sorry+",I am so sorry that you can\'t have dinner with me tonight!"); sorry=friends.pop(); print(sorry+",I am so sorry that you can\'t have dinner with me tonight!"); print("-----------------------------------------"); print(friends[-1]+",you can have dinner here,enjoy yourselves!"); print(friends[-2]+",you can have dinner here,enjoy yourselves!"); print("-----------------------------------------"); del friends[0]; del friends[0]; print(friends);

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzdyfp.html