创建一个列表
list1 = ['a','b','c',4,5,6]
列表中增加元素(增)
列表末尾增加元素
list1.append(8)
list1
['a', 'b', 'c', 4, 5, 6, 8]
指定位置增加元素
list1.insert(4,'d')
list1
['a', 'b', 'c', 4, 'd', 5, 6, 8]
列表中修改元素
将’a’修改为“hello world”
list1[0] = 'hello world'
list1
['hello world', 'b', 'c', 4, 'd', 5, 6, 8]
列表删除元素
del list1[-1]
list1
['hello world', 'b', 'c', 4, 'd', 5]
list1.remove('d')
list1
['hello world', 'b', 'c', 4, 5]
list3 = ['hello world','b','c',4,5]
pop_ele = list3.pop(2)
pop_ele
'c'
list3
['hello world', 'b', 4, 5]
列表查元素
list1[0:2]
['hello world']
list2 = [i for i in range(10)]
list2
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list2[0:9:2]
[0, 2, 4, 6, 8]
list2[-9:-1:2]
[1, 3, 5, 7]
3 in list2
True
11 in list2
False