首页 > temp > 简明python教程 >
-
Python列表练习题
1、创建一个空列表,命名为names,往里面添加 Lihua、Rain、Jack、Xiuxiu、Peiqi和Black元素。

2、往(1)中的names列表里Black前面插入一个Blue。

3、把names列表中Xiuxiu的名字改成中文。

4、往names列表中Rain后面插入一个子列表["oldboy","oldgirl"]。

5、返回names列表中Peiqi的索引值(下标)。

6、创建新列表[1,2,3,4,2,5,6,2,],合并到names列表中。

7、取出names列表中索引4-7的元素。

8、取出names列表中索引2-10的元素,步长为2。

9、取出names列表中最后3个元素。

10、循环names列表,打印每个元素的索引值和元素。

11、循环names列表,打印每个元素的索引值和元素,当索引值为偶数时,把对应的元素改成-1。

12、names列表里有3个2,请返回第二个2的索引值,不要人肉,要动态找。

13、现有商品列表如下:
products = [["iphone",6888],["MacPro",14800],["小米6",2499],["Coffee",31],["Book",60],["Nike",699]],需打印出以下格式:

参考答案如下:

14、根据(13)里的products列表写一个循环,不断询问用户想买什么,用户选择一个商品编号,就把对应的商品添加到购物车里,最终用户输入q退出时,打印购买的商品列表。
参考答案如下:
