python的标准库_Python 标准库

标准库

标准库是Python官方内置的一些模块,我们安装Python的时候,就已经把这些库集成到我们的电脑本地,可以直接使用import导入使用。

以random为例,一个随机模块。这些本来复杂的工作,被经过抽象的封装,我们拿来即用,非常方便。

>>> import random

# 随机

>>> random.random()

0.329038924789

# 范围限定

>>> random.uniform(10,20)

15.357293066092108

# 范围限定 | 整数

>>> random.randint(10,20)

17

# 奇数 | 1

random.randrange(0, 100, 1)

23

# 偶数 | 2

random.randrange(0, 100, 2)

26

# 序列随机 | 筛选一个

random.choice('abcdefg')

'g'

# 序列随机 | 筛选多个

random.sample([1,2,3], 2)

[2,1]

模块路径

sys.path

>>> import sys

>>> sys.path # 如果模块放置在列表里面的这些目录,就能使用import导入使用 | 下面这些为默认的目录 | 第一个 '' 指的是当前目录

['', '/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python36.zip', '/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6', '/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload', '/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages']

# 新增地址

>>> sys.path.append('/Users/Jason/Desktop/Python/') # 只在当前文件有效,永久有效需要设置 PYTHONPATH 环境变量

模块信息

dir函数,也可以用来获取模块信息。

>>> dir(sys)

['__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe', '_git', '_home', '_xoptions', 'abiflags', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'last_traceback', 'last_type', 'last_value', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'set_asyncgen_hooks', 'set_coroutine_wrapper', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions']

Python官方内置的标准库非常多,也被分成多种应用类型,如果有兴起了解更多,可以参考这本书 Python 标准库。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值