1.列表
1.1创建列表
list = ['a','b','c','d']
print(list)
>['a','b','c','d']
print(list[0])
>a
print(list[-1])
>d
print(list[-2])
>c
1.2修改列表中的元素
list = ['a','b','c','d']
print(list)
>['a','b','c','d']
list[0] = 'A'
print(list)
>['A','b','c','d']
1.3在列表的末尾添加元素
list = ['a','b','c','d']
print(list)
>['a','b','c','d']
list.append('e')
print(list)
>['a','b','c','d','e']
1.4在列表中插入元素
list = ['a','b','c','d']
print(list)
>['a','b','c','d']
list.insert(1,'A')
print(list)
>['a','A','b','c','d','e']
1.5删除列表中的元素
list = ['a','b','c','d']
print(list)
>['a','b','c','d']
del list[1]
print(list)
>['a','c','d']
1.6弹出列表末尾的元素
list = ['a','b','c','d']
print(list)
>['a','b','c','d']
poped_list = list.pop()
print(list)
>['a','b','c']
1.7弹出列表任意位置的元素
list = ['a','b','c','d']
print(list)
>['a','b','c','d']
poped_list = list.pop(1)
print(list)
>['a','c','d']
1.8根据值删除列表中的元素
list = ['a','b','c','d']
print(list)
>['a','b','c','d']
list.remove('c')
print(list)
>['a','b','d']
1.9列表永久排序
list = ['g','c','u','z','x','f','h']
#正序
list.sort()
print(list)
>['c', 'f', 'g', 'h', 'u', 'x', 'z']
#倒序
list.sort(reverse=True)
print(list)
>['z', 'x', 'u', 'h', 'g', 'f', 'c']
1.9列表临时排序
list = ['g','c','u','z','x','f','h']
list.sort()
print(list)
#正序
list_sorted = sorted(list)
print(list_sorted)
>['c', 'f', 'g', 'h', 'u', 'x', 'z']
>['c', 'f', 'g', 'h', 'u', 'x', 'z']
#倒序
list_sorted = sorted(list,reverse=True)
print(list_sorted)
>['z', 'x', 'u', 'h', 'g', 'f', 'c']
1.10列表倒序
list = ['a','b','c','d']
list.reverse()
print(list)
>['d', 'c', 'b', 'a']
1.11获取列表长度
list = ['a','b','c','d']
print(len(list))