布尔运算
运算 | 结果: |
---|
x or y | if x is false, then y, else x |
x and y | if x is false, then x, else y |
not x | if x is false, then True , else False |
比较运算— and, or,not
运算 | 含意 |
---|
< | 严格小于 |
<= | 小于或等于 |
> | 严格大于 |
>= | 大于或等于 |
== | 等于 |
!= | 不等于 |
is | 对象标识 |
is not | 否定的对象标识 |
数字类型—int,float,complex
运算 | 结果 |
---|
x + y | x 和 y 的和 |
x - y | x 和 y 的差 |
x * y | x 和 y 的乘积 |
x / y | x 和 y 的商 |
x // y | x 和 y 的商数 |
x % y | x / y 的余数 |
-x | x 取反 |
+x | x 不变 |
abs(x) | x 的绝对值或大小 |
int(x) | 将 x 转换为整数 |
float(x) | 将 x 转换为浮点数 |
divmod(x, y) | (x // y, x % y) |
pow(x, y) | x 的 y 次幂 |
x ** y | x 的 y 次幂 |
round(x,n) | x 舍入到 n 位小数,半数值会舍入到偶数。 如果省略 n,则默认为 0。 |
序列类型—list,tuple,range
通用序列操作
运算 | 结果: |
---|
x in s | 如果 s 中的某项等于 x 则结果为 True ,否则为 False |
x not in s | 如果 s 中的某项等于 x 则结果为 False ,否则为 True |
s + t | s 与 t 相拼接 |
s * n 或 n * s | 相当于 s 与自身进行 n 次拼接 |
s[i] | s 的第 i 项,起始为 0 |
s[i:j] | s 从 i 到 j 的切片 |
s[i:j:k] | s 从 i 到 j 步长为 k 的切片 |
len(s) | s 的长度 |
min(s) | s 的最小项 |
max(s) | s 的最大项 |
s.index(x[, i[, j]]) | x 在 s 中首次出现项的索引号(索引号在 i 或其后且在 j 之前) |
s.count(x) | x 在 s 中出现的总次数 |
list的方法
list.append(x)
list.extend(iterable)
list.insert(i, x)
list.remove(x)
list.pop([i])
list.clear()
list.index(x[, start[, end]])
list.count(x)
list.sort(*, key=None, reverse=False)
list.reverse()
list.copy()
可变序列类型
运算 | 结果: |
---|
s[i] = x | 将 s 的第 i 项替换为 x |
s[i:j] = t | 将 s 从 i 到 j 的切片替换为可迭代对象 t 的内容 |
del s[i:j] | 等同于 s[i:j] = [] |
s[i:j:k] = t | 将 s[i:j:k] 的元素替换为 t 的元素 |
del s[i:j:k] | 从列表中移除 s[i:j:k] 的元素 |
s.append(x) | 将 x 添加到序列的末尾 (等同于 s[len(s):len(s)] = [x] ) |
s.clear() | 从 s 中移除所有项 (等同于 del s[:] ) |
s.copy() | 创建 s 的浅拷贝 (等同于 s[:] ) |
s.extend(t) 或 s += t | 用 t 的内容扩展 s (基本上等同于 s[len(s):len(s)] = t ) |
s *= n | 使用 s 的内容重复 n 次来对其进行更新 |
s.insert(i, x) | 在由 i 给出的索引位置将 x 插入 s (等同于 s[i:i] = [x] ) |
s.pop() 或 s.pop(i) | 提取在 i 位置上的项,并将其从 s 中移除 |
s.remove(x) | 删除 s 中第一个 s[i] 等于 x 的项目。 |
s.reverse() | 就地将列表中的元素逆序。 |
映射类型 — dict
'clear',
'copy',
'fromkeys',
'get',
'items',
'keys',
'pop',
'popitem',
'setdefault',
'update',
'values'
文本序列类型-str
['capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
关键字
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
内置函数
'abs', 'aiter', 'all', 'anext', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip'