type()查看类型
//取整除
**幂
成员运算符:
in x在y序列中,就返回true
反之 not in
身份运算符:
is
is not
逻辑运算符
and
or
not
字符编码 问题
通用序列操作
索引
>>> 'hello'[1]
'e'
>>> 'hello'[-2]
'l'
>>>
分片
>>> 'hello'[2:4]
'll'
[a:b] 相当于a<=x<b
>>> number=[1,2,3,4,5,6,7,8,9,10]
>>> number[-3:-1]
[8, 9]
>>> number[-3:0] #只要分片中最左边的索引比右边的索引晚出现在序列中,结果就是空序列
[]
>>> number[-3:]
[8, 9, 10]
number[2:0:-1] #步长
[3, 2]
乘法
>>> 'hello'*5
'hellohellohellohellohello'
>>> number*5
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
长度len,最大值max,最小值min
列表 [ ]
一个列表的元素可以赋不同类型的值
不能为一个不存在的元素位置赋值
list.append(obj)
在列表末尾添加元素
del list[n] 删除列表元素
list()将字符串转换为列表
嵌套列表
列表方法
1。append
2.list.count(obj)
3.list.extend()
4.list.index(obj) 匹配
5.list.insert() 插入
6.list.pop(obj=list[-1]) 移除元素 不传参数,默认移除最后一个元素(可以实现栈的数据结构操作)
7.list.remove
8. .reverse() 反转
9. .sort() 排序
高级排序
key关键字
10 .clear() .copy()
元组 ( )
元组不能修改
tuple() 将序列转换为元组
字典 { }
映射类型
字典格式
d={key:value1,key2:value2}
键(key)不可变
dict()函数将序列转换为字典
>>> student=[('ab',12),(22,'a')]
>>> detail=dict(student)
>>> print(detail)
{'ab': 12, 22: 'a'}
修改字典
>>> detail['aa']='123'
>>> print(detail)
{'ab': 12, 22: 'a', 'aa': '123'}
del 删除字典
注意;
不允许一个键出现两次
键不可改变,可以用数字,字符串或元组充当,不能用列表
len()字典元素个数
dict内部存放的顺序和键放入的顺序没有关系
clear() copy()
fromkeys() 创建一个新字典 dict.fromkeys(seq[,value])
get()返回指定键的值
key in dict() 判断键是否在字典中
items() 遍历(键/值)元组数组
keys() 返回一个字典所有的键
update()更新字典值
values() 以列表的形式返回字典所有值