Lists can be modified, strings and tuples cannot.
List method in python:
1.list.append(x) adds an element to the end of the list
list=[1,2,3,4,5]
print(list)
x=6
list.append(x)
print(list)
2.List.extend(L) extends the list by adding all the elements of the specified list
list=[1,2,3,4,5]
print(list)
L='abc'
list.append(L)
print(list)
3.list.insert(i, x) insert an element at the specified position, x is inserted behind the specified position
list=[1,2,3,4,5]
print(list)
L='abc'
list.insert(list[2],L)
print(list)
4. list.remove(x) delete the element whose value is x, and report an error if it does not exist
list=[1,2,3,4,5]
print(list)
list.remove(2)
print(list)
5.list.pop([i]) Remove the element from the specified position in the list. If no index is specified, a.pop() returns the last element. The element is removed from the list.
list=[1,2,3,4,5]
print(list)
list.pop(0)
print(list)
list=[1,2,3,4,5]
list.pop(1)
print(list)
list=[1,2,3,4,5]
list.pop(2)
print(list)
list=[1,2,3,4,5]
list.pop(3)
print(list)
list=[1,2,3,4,5]
list.pop(4)
print(list)
list=[1,2,3,4,5]
list.pop( ) #空的情况默认删除最后一个
print(list)
list=[1,2,3,4,5]
list.pop(-1)
print(list)
6.list.clear() removes all items in the list, which is equal to del
7.list.index(x) returns the index of the first value x in the list, if not, it returns an error.
list=[1,2,3,4,5]
print(list)
print(list.index(2))
list=[1,2,2,2,2,2,23,4,5]
print(list)
print(list.index(2))
8.list.count(x) Count the number of times x appears in the list
list=[1,2,2,2,2,2,23,4,5]
print(list)
print(list.count(2))
9.list.sort() sorts the elements in the list
list=[9,8,7,5,0,4]
print(list)
print(list.sort())#不能这样写,输出为None
list.sort()
print(list)
10.list.reverse() Reverse the elements in the list
list=[9,8,7,5,0,4]
print(list)
#print(list.reverse()) #不能写print里面
list.reverse()
print(list)
11.list.copy() returns a shallow copy of the list.
Note: Methods like insert, remove, reverse (or sort, etc. to modify the list) have no return value.
list=[9,8,7,5,0,4]
print(list)
print(list.reverse())
print(list.sort())
print(list.reverse())