Python Import Importing Module

Source: Internet
Author: User
Tags floor function

The import statements in Python are for importing modules, and there are a large number of modules available in the Python module library, which you need to import into the current program using an import statement.

Import Statement function

The import statement acts as a means of importing a module, which can appear anywhere in the program.

Import statement syntax

Import the module using the import statement, the import statement syntax is as follows:
Import Module
Keyword Module name

Use methods such as:
Import Math #入导math模块
Math.floor () #调用math模块中的floor () function

If you want to import multiple modules at the same time, you only need to delimit them with commas before the module name:
Import Module1,module2,module3 .....

The method of importing multiple modules at the same time is less readable and understandable to beginners than the first. Therefore, when you want to import more than one module, it is recommended to use the first way, each module is a separate import, you may feel that the operation is troublesome, but easy to understand.

Import Statement Importing Module order

In the process of writing code, we may need a variety of modules, it is important to note that the import module should be placed at the beginning of the code.


Why do you want to put import module in the beginning of the program to use it?
The interpreter follows the scope principle when executing a statement. Because this is related to the scope, if the module is imported at the top level, its scope is global, and if the module is imported inside the function, its scope is only local and cannot be used by other functions. If other functions also need to use this module, it will be more cumbersome to import again.

It is best to follow this order when importing a module with an import statement:

1. Python Standard library module
2. Python third-party module
3. Custom Modules

From-import statement function

The Python from import statement is also a way to import the module, or, more specifically, the specified function method within the specified module.

From-import statement syntax

From module import name
Keyword Module Name keyword method name

For example, the floor function method in the math module of the onboarding function:
From math import Floor

Python from use methods such as:

From math import Floor #导入math模块中的floor函数方法
Floor () #调用floor () function method

Python Import Importing Module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.