Python Exercise 1——List

1. Create a list: colors , the internal elements are: red, yellow, blue, white, black, green, gray ,

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

2. Insert an element in front of black : orange

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

3. Change orange to orange.

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

4. Insert a subtable after blue: [ " pink", "purple"]

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

5. Return the index value of white .

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

6. Create a new list 11, 22, 33, 44 and merge it into colors .

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

7. Remove all elements with index values ​​4-8 .

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

8. Take out the last 2 elements.

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

9. Use a loop to traverse all elements of the list and output the element value and corresponding index value.

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

10. Delete the gray element.

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

11. Delete colors.


  del colors

Guess you like

Origin blog.csdn.net/m0_63715487/article/details/129227357