python习题1———列表

1、创建一个列表:colors,内部元素为:red,yellow,blue,white,black,green,gray

 colors = ['red','yellow','blue','white','black','green','gray']
 print('第1题:',colors)

2、在black前面插入一个元素:orange

colors.insert(4,'orange')
  print('第2题:',colors)

3、把orange改为橙色。

colors. Insert橙色'
  print('第3题:',colors)

4、在blue后面插入一个子表:[“pink”,”purple”]

list = ['pink','purple']
  colors.insert(3,list)
  print('第4题:',colors)

5、返回white的索引值。

 print('第5题:',colors.index('white'))

6、创建一个新列表11,22,33,44,并合并到colors中。

 newlist= [11,22,33,44,]
  print('第6题:',colors + nub)

7、取出索引值4-8的所有元素。

 print('第7题:',colors[4:8])

8、取出最后2个元素。

  print('第8题:',colors[-2:])

9、使用循环,遍历列表所有元素,并输出元素值和对应的索引值。

 for i in colors:
    print('第九题:','元素值是:',i,'索引值是:',colors. Index(i))

10、删除gray元素。

# del colors[8]            # 方法1
  # colors.pop(8)            # 方法2
  colors.remove("gray")      # 方法3

11、删除colors。


  del colors

猜你喜欢

转载自blog.csdn.net/m0_63715487/article/details/129227357