标准模块与内建函数
标准模块与内建函数
系统标准模块
Python标准库中提供了大量的模块,辅助开发人员的软件开发工作
了解 PYTHON 官方标准模块,有助于对 PYTHON 的理解和操作
dir() # 查看所有内建模块 |
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__'] |
dir(__builtins__) |
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', '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'] |
系统内建函数
名称 |
描述 |
Filter(func, seq) |
过滤器函数 参数 func:执行函数 参数 seq:参数列表 作用:该函数会将参数序列 seq 中的数据,依次传入函数 func 中执行得到结果 def show_msg(num): if num % 2 == 0: return True return False res = filter(show_msg, [1, 2, 3, 3, 4, 5, 6, 67, 5, 4]) print(list(res)) |
zip(seq1, seq2) |
打包函数 参数 seq1:要打包的第一个序列 参数 seq2:要打包的第二个序列 作用:将参数中的两个序列,按照 k:v 的形式进行打包组合, 如果两个序列长度不一致,按照短序列打包 a = "hello world" b = "1234567" print(list(zip(a, b))) |
map(func, seq) |
映射函数 参数 func:要执行的目标函数 参数 seq:一个参数列表,将参数列表中的数据依次交给函数 执行,最后收集执行结果 def show_msg(num): return True if num % 2 == 0 else False
res = map(show_msg, range(0, 20)) print(list(res)) |
abs(x) |
获取x的绝对值 |
all(seq) |
如果一个序列中的所有数据都等价于True,返回一个结果True |
any(seq) |
如果一个序列中的任意一个数据等价于 True,则返回一个结果 True |
ascii(obj) |
将一个对象转义成 ascii 码的表示形式,必要的时候可以使用转义字符 |
bin(x) |
将整数 x 转换为二进制串表示形式 |
bool(x) |
强制类型转换,将一个数据 x 转换成等价的 True/False |
bytes(x) |
将一个对象 x 转换成字节串的表示形式 |
callable(obj) |
测试对象是否可调用 |
complex(x) |
强制类型转换,转换成复数的表示形式 |
chr(x) |
返回 unicode 编码为 x 的字符 |
delattr(obj, name) |
删除对象属性,等价于 del obj.name |
dir(obj) |
查看对象 obj 的成员属性和方法列表 |
divmod(x, y) |
返回整除后的商和余数的元祖 |
enumerate(seq) |
返回一个带了索引的可迭代对象 |
eval(expression) |
计算字符串表达式 expression 中的结果 |
exit() |
退出当前解释器环境 |
exec(x) |
执行代码或者代码对象 x |
float(x) |
强制类型转换,转换成 x 对应的浮点数 |
frozenset([x]) |
创建一个不可变的集合对象 |
getattr(obj, name) |
获取对象的属性的值,等价于 obj.name |
globals() |
返回包含当前作用域内的所有全局变量的名称和值 |