Home >Backend Development >Python Tutorial >What is a Python import statement? What are the functions of the import statement in Python?
Python import statement is used to import modules. There are a large number of modules available in the python module library. The function of the import statement is to import the specified module that the file needs to use. to the current program.
The function of the import statement
The function of the import statement is used to import modules, which can appear in the program any location.
Python import statementGrammar
Use the import statement to import the module, Python import statementThe syntax is as follows:
import module 关键字 模块名
The usage method is as follows :
import math #导入math模块 math.floor() #调用math模块中的floor()函数
If you want to import multiple modules at the same time, just separate them with commas before the module names:
import module1,module2,module3.......
The method of importing multiple modules at the same time is readable for beginners And comprehensibility is not as good as the first one. Therefore, when you want to import multiple modules, it is still recommended to use the first method. Import each module separately. It may be more troublesome to operate, but it is easier to understand.
Python import statementImport module order
In the process of writing code, we may need a variety of modules. It should be noted that it is best to put the imported module in The beginning of the code.
Why should the import module be used at the beginning of the program?
The interpreter follows the scope principle when executing statements. Because this is related to scope, if a module is imported at the top level, its scope is global; if a module is imported inside a function, its scope is only local and cannot be used by other functions. If other functions also use this module, it will be troublesome to import it again.
When using the Python import statement it is best to import modules in this order:
1, python standard library module
2, python third-party module
3. Custom modules
Related links:
Click to visit:What are modules in Python? Understand the module issues in Python in 3 minutes
The above is the detailed content of What is a Python import statement? What are the functions of the import statement in Python?. For more information, please follow other related articles on the PHP Chinese website!