Python import import module and function method Python language foundation [1]

1 Python language base import module

Importing modules in Python code requires the use of the import statement syntax. The result is as follows

import module_name

The syntax for using a function in a module is as follows

module_name.function.name

If some functions of a module are used a lot in a Python program, it is very troublesome to add the module name every time the function is called

So in this case, you can use from import to directly expose the functions in the module

The syntax of this statement is as follows

from module_name import function_name

The above statement imports a function in the module_name module. If you want to import all the function methods in the module_name module, you can write it like this

from module_name import *

2 Example applications

The math module is a related module in python for numerical computation

2.1 Import the entire math module
# 导入 math 模块
import math
# 向下取整数 
print(math.floor(10.6))

insert image description here

2.2 Import the sin function method in the math module
# 导入 math 模块 sin 函数方法
from math import sin
# 正弦值
print(sin(10.6))

insert image description here

2.3 Import all functions in math

# 导入 math 模块  中的所有的函数
from math import *
# 返回数字x的平方根。
print(sqrt(4))


insert image description here

Guess you like

Origin blog.csdn.net/zl18603543572/article/details/122261190