Python study notes - the import and use of standard libraries and extensions objects

Python default installation comprising only the base or core module is only loaded when starting the basic module, and to explicitly import standard library loading and third-party extensions (required properly installed) when required, which can reduce the running pressure and it has strong scalability.

From the "bucket theory" point of view, the idea of ​​"least privilege" principle followed in this design is consistent with the security configuration also helps improve system security.

1, import module name [as alias]

>>> import math                    #导入标准库math
>>> math.sin(0.5)                  #求0.5(单位是弧度)的正弦
0.479425538604203
>>> import random                  #导入标准库random
>>> n = random.random()            #获得[0,1) 内的随机小数
>>> n = random.randint(1,100)      #获得[1,100]区间上的随机整数
>>> n = random.randrange(1, 100)   #返回[1, 100)区间中的随机整数
>>> import os.path as path         #导入标准库os.path,并设置别名为path
>>> path.isfile(r'C:\windows\notepad.exe')
True
>>> import numpy as np             #导入扩展库numpy,并设置别名为np
>>> a = np.array((1,2,3,4))        #通过模块的别名来访问其中的对象
>>> a
array([1, 2, 3, 4])
>>> print(a)
[1 2 3 4]

2, from the module name import object name [as alias]

>>> from math import sin         #只导入模块中的指定对象,访问速度略快
>>> sin(3)
0.1411200080598672
>>> from math import sin as f    #给导入的对象起个别名
>>> f(3)
0.1411200080598672
>>> from os.path import isfile
>>> isfile(r'C:\windows\notepad.exe')
True

3, from the name of the module import *


>>> from math import *         #导入标准库math中所有对象
>>> sin(3)                     #求正弦值
0.1411200080598672
>>> gcd(36, 18)                #最大公约数
18
>>> pi                         #常数π
3.141592653589793
>>> e                          #常数e
2.718281828459045
>>> log2(8)                    #计算以2为底的对数值
3.0
>>> log10(100)                 #计算以10为底的对数值
2.0
>>> radians(180)               #把角度转换为弧度
3.141592653589793

 

Published 152 original articles · won praise 124 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_44762986/article/details/104724326