Author: Insist--
Personal homepage: insist--personal homepage
Column for this article: Python column
Column introduction: This column is a free column and will continue to update the basic knowledge of python . You are welcome to subscribe and pay attention.
Table of contents
3. Classification of Python modules
1. Import Python built-in modules
2. Customize the module and import it
Preface
In this article, I will take you to understand Python modules, including what a module is, its function, classification, and importing and using python built-in modules and custom modules.
1. What is a Python module?
Simply put, a Python module is a Python file. There are functions, classes, variables, etc. in the module, which we can use directly.
How to use it will be mentioned below
Python module (Module) is a Python file ending with .py. Modules can contain functions, classes, variables, etc. , and can contain executable code . Through modules, Python code can be easily organized and managed, and conflicts between function names and variable names can be avoided. Using modules also improves the readability and maintainability of your code.
2. The role of Python modules
There are many modules in Python, and each module can help us implement some functions. For example, we used a module (random) to generate random numbers in our previous article. Using modules can improve the maintainability of the code and avoid conflicts between function names and variable names. The code can be used multiple times and can also help us organize the code.
3. Classification of Python modules
Python modules can be divided into three categories, including Python built-in modules, third-party modules, and custom modules. Let’s take a closer look at these three categories of modules:
1. Python built-in modules
Python's built-in modules come with Python . The random module we mentioned above is a built-in module of Python. In addition, Python also has many built-in modules, such as time, logging, os modules, etc.
time : This module is mainly used to process time representation, time conversion, and time-related calculations.
logging : This module is mainly used to output running logs.
os : This module provides many functions for interacting with the operating system.
2. Third-party modules
Third-party modules are Python modules produced and released by unofficial companies for public use, such as Requests, Matplotlib modules, etc.
Requests : This module is mainly used to send HTTP requests and obtain response data.
Matplotlib : This module is used to create static, dynamic and interactive visualization graphics.
3. Custom modules
A custom module is a module we write ourselves , which encapsulates a certain piece of logic or certain functions for other functions to call. It can be part of Python code or a standalone Python program or library.
4. Python module
1. Import Python built-in modules
Importing Python modules is relatively simple. In Python, we can use the import statement to import modules.
For example: If you want to import Python's built-in random module, you can write:
import random # 注意:random是你要导入的模块名称
# 注意:import是导入模块所要使用的关键字
After importing, you can use the functions in the random module. For example, if you want to use the randint function in random, you can write like this:
Little knowledge: randint is a random number generation function provided by the randon module.
import random # 导入 random 模块
random.randint(1,100) #使用 random 模块中的 randint 生成随机数函数,并设置生成随机数的范围为1~100
Output result: After using a variable to receive the generated random number, we can use print to print out the random number. We can find that our output result is different every time and is within the range we set (1~100).
2. Customize the module and import it
In Python, in addition to importing modules written by others, we can also create our own modules (custom modules) and then import them in other scripts or programs. Here are the steps on how to create and import a custom module.
First, we need to create a Python file: right-click - New - python file - Name it
For example: we name it my_module, and in this file, we define a function:
def t1(a,b):
print(a + b)
Then, in another Python file, we can import and use the functions in this custom module with the following code:
import my_module # 导入自定义的模块
my_module.t1(8,7) # 使用模块内的函数,并传入参数
Running this python file, you can see the output is: