Importing modules in Python code requires an import statement with the following syntax structure:
import module_name
?? The syntax of a function in a reference module is as follows:
module_name.function_name
?? If some of the functions in the module are used extensively in a Python program, it can be cumbersome to add a "module name" Each time the function is called, so in this case, you could use the from...import ... Statement exposes the functions in the module directly. The syntax structure of the statement is as follows:
from module_name import function_name
?? If you want to import all the functions in the module, you can replace the function_name with the model number (*) so that we can use all the functions in the module directly.
from module_name import *
?? In addition, import and From...import ... The statement can be written anywhere in the Python code, but be sure to perform an import or From...import before referencing the corresponding module function ... statement, or an exception is thrown when the function is called.
?? The following code uses import and From...import ... Statements refer to the Math module and the SQRT function in the math module, respectively. If you want to call other functions in the math module, you must precede the function name with "math." Prefix, but you can call the SQRT function directly to calculate the square root of a number.
print("import和from...import...演示")# 导入math模块import mathprint(math.floor(20.6))# 导入math模块中的sqrt函数from math import sqrtprint(sqrt(12))# 导入math模块中的所有函数from math import *print(sin(3.14/2))
The program runs as shown in the results.
"Python from rookie to Master" has been published, buy send video lessons
Python from rookie to expert (4): Import python module