Concept: In python, the file ending with .py is called a module, which can define classes, functions, attributes, etc. in the module
classification:
1). Standard library modules: modules available in the python environment after installation, these modules are the most commonly used modules;
For example: random, os, os.path, math,...
2). Third-party modules: valuable code written by others (for the world), if we need to use it,
Just need to install via pip
3). Custom module: In the process of project development, the programmers in the team define themselves, which can be used by themselves or others
Import the module:
1). Precise import:
For example:
import time
from random import randint
2). Fuzzy import:
For example:
from math import *
from os import *
Alias the imported module or its functions and attributes:
Use the as keyword to achieve
【note】:
Once an alias is created, the previous name cannot be used
Custom module:
You need to import the custom module to the current module, and then you can use the content at will
Code if name == main: The function of this code is to define the code that does not want to be loaded
" The concept of "package": package
To create a python package is to create a python package,
The role of the package: Incorporate multiple related modules to facilitate future maintenance and management
For the init.py and pycache directories, we don’t need to pay attention to it, but don’t delete it
Third-party modules:
Open cmd --> enter pip -V (this operation checks to see if the installation is complete pip)
The main operations involved are as follows:
1). View all currently installed third-party modules: pip list
2). View the detailed information of a third-party module: pip show module name
3). Install a third-party module: pip install module name for example: pip install redis
4). Delete a third-party module: pip uninstall module name For example: pip uninstall redis
# Demonstrate the import of standard library modules
import random
from random import shuffle
from math import pi,e
from time import*from random import randint as r
import os as f
# import func
# from func import my_sum
print(random.randint(1,3))
lt=[1,2,3,4,5,6]shuffle(lt)print(lt)print(pi,e)print('I slept...')sleep(2)print('I woke up...')print(r(5,10))'''
If you alias a module or function, the original name cannot be used;
So the following code will report an error
'''
print(randint(3,7))print(f.getcwd())print(func.my_sum(10,20))print(func.my_max(10,20))print(my_sum(100,200))
Recommended Posts