Python from rookie to expert (4): Import python module

Source: Internet
Author: User
Tags square root

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

Related Article

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.