How to import python modules

1. The import statement

import math #导入python的内置库math,并导入了该模块内的所有内容
num=4
value=math.sqrt(num) #调用math模块中的sqrt函数对num的值求平方根
print(value)

Output result: 2
eg: no matter how many times you execute import, a module will only be imported once

Two, from...import* statement

from math import *
num=4
num1=pi #pi是math模块中的数学常量
value=sqrt(num) #不需再写出模块名
print(value)
print(num1)

Output: 2.0
3.141592653589793

Three, from...import statement

from math import sqrt #这样就只能调用这一种方法
num=4
value=sprt(num)
print(value)

Output result: 2.0

Guess you like

Origin blog.csdn.net/m0_52423924/article/details/122611773