List list common function exercises

Record the practice questions of the commonly used functions of the list list here. Since the content of the study notes is long, it is recorded separately as an external link to the study notes of the list list.

Title : There is a list, the content is: [21, 25, 21, 23, 22, 20], which records the age of a group of students,

Please pass the function (method) of the list, to it:

1. Define this list and receive it in a variable
2. Append a number 31 to the end of the list
3. Append a new list [29, 33, 30] to the end of the list
4. Take the first element (should be: 21)
5. Take out the last element (should be: 30)
6. Find element 31, the subscript position in the list

Implementation code :

# 定义一个列表list
list1 = [21, 25, 21, 23, 22, 20]
# 追加数字31到列表尾部
list1.append(31)
print(list1)
# 追加新列表[29,33,30]到列表尾部
list1.extend([29, 33, 30])
print(list1)
# 取出第一个元素21
num1 = list1[0]
print(num1)
# 取出最后一个元素30
num2 = list1[-1]
print(num2)
# 查找元素31在列表中的下标位置
index = list1.index(31)
print(index)

Running result :
[21, 25, 21, 23, 22, 20, 31]
[21, 25, 21, 23, 22, 20, 31, 29, 33, 30]
21
30
6
[21, 25, 21, 23 , 22, 20, 31, 29, 33, 30]

Guess you like

Origin blog.csdn.net/weixin_44996886/article/details/132701498