There are many powerful modules in Python. Some of these modules exist in the Python system library and some exist in third-party libraries . These modules provide us with a variety of functions.
Let's take an example to describe the module. When we prepare a new house, we will purchase some items in it, such as a rice cooker, microwave oven, telephone, computer, etc. The rice cooker provides us with functions such as cooking rice, cooking porridge, and steaming rice. Microwave ovens provide us with functions such as low-temperature heating, medium-temperature heating, and high-temperature heating. Mobile phones provide us with functions such as answering calls, making calls, and saving numbers. Computers provide us with functions such as programming, information retrieval, and network maintenance. If we buy another juicer, the juicer will provide us with functions such as squeezing juice, making milkshakes, and making jam.
For the above example, we can regard rice cookers, microwave ovens, phones, computers and other items that are initially prepared at home as internal modules. We can directly use their functions at home. That is to say, we only need to take this item We can use it as soon as it comes out, and the juicer we bought later can be regarded as a module we introduced from a third-party library. We only need to buy it from a third-party library before we can take it out and use a series of it. Function. Let us summarize, the internal module is a module that is stored internally and can be called directly. After calling this module, we can use its functions, while the modules in the third-party library need to be downloaded first, and then we can call them after downloading. use.
Look at the diagram below:
Let's look at another example. We have mentioned the random module many times before. The random module provides us with a series of functions. Let's simply use this module here.
importrandom#Introduce this module from the system library test=random.randint(1,10)#Use the randint() method in the random module print('Generate a random number: ', test)
Output result:
Generate a random number: 3
It should be noted that when we use the functions in the module, we must first introduce this module. If not, the following situation will occur:
Traceback(mostrecentcalllast):FileC:/ts.py,line2,in<module>test=random.randint(1,10)#Use the randint() method in the random module NameError:name'random'isnotdefined
After we import the module name, we can use all methods in the module.
Let's introduce several ways to call the module.
This method is our common calling method, which directly calls all methods in the module, and we can use it directly in the program.
For example:
After we call random, we can see a series of methods it contains when using it.
importmathasttest=t.cos(2.5)print(test)
The output is:
-0.8011436155469337
This method is equivalent to giving the module another name. We can use the methods in the module through this alias.
fromrandomimportrandinttest=randint(1,10)print(test)
The output is:
9
This method only calls a function in the module, and we can use it directly when using this function.
If you use other methods in this module, an error message will appear.
That’s it for the basic content of the module. Regarding the calling method of the module, we make appropriate choices when using it. Generally speaking, we will use the third method to use a certain method to avoid the problem of variable names and An error occurs if the names in the modules are the same. In the next section, we will introduce custom modules.