Module - definition and related use


Classification: standard module, user-defined module, third-party module
Function: meet specific purposes and needs; For example: network management, access, encryption processing, mathematical calculation, data analysis, image processing.
How to construct a module: Standard Module - directly use the command import; Custom module - placed in py file;
Multiple modules - form a package and put it in a folder.

usage method:
import keyword reference
Use the from... import... Statement

Syntax usage:
Import module # import
modular. Function name # usage

Module function: use import to import. When necessary, use [module name + function name] to call

os module - provides many functions that interact with the operating system
sys module - system related parameters and functions
Time module - time related functions
math module - provides access to the underlying c library functions of floating-point mathematics
Random module - provides tools for random
re module -- provides regular expression tools for advanced string processing

Example: os module:

import os 
print(os.getcwd()) #Get the current working path

sys module:

import sys
print(sys.argv)   #Get a list of files with command line arguments
print(sys.platform)#Get the current system platform
print(sys.path)   #Get a list of paths where the Python interpreter automatically finds the required modules

time module:

import time
time.sleep(2)   #Pause time, the number in () represents the pause time

star=time.time()#start time
print(time.time())#Returns the timestamp of the current time (floating-point second rate experienced after 1970)
for i in range(10000):
end=time.time()#End time
import time
print(time.localtime())#Returns the current time, date, etc

be careful:
0 tm_year (year)
1 tm_mon (month)
2 tm_mday (day)
3 tm_hour
4 tm_min (min)
5 tm_sec (seconds)
6 tm_wday (weekday day of the week), 0 is Monday, and so on
7 tm_yday (day of the year)
8 tm_isdst (whether it is daylight saving time), 0 Non daylight saving time, 1 daylight saving time, - 1 uncertain whether it is

math module

import math

Random module (random function)

1. Generate integer
• randrange(start, stop[, step])
• randint(a, b)

import random
print(random.randrange(1,20,7))#1,8,15;; 1 starts, 20 ends, and the interval is 7
print(random.randint(1,100))#Integer;; Range [1100]; Not [1100)

2. Sequence
• choice(seq)
• shuffle(x)
• sample(population, k)

import random
print(random.choice(x))#Returns a random element from a non empty sequence
import random
random.shuffle(y)#The operation object is a list (the value of z)
#If you write this
print(z)#Finally, run None
import random
random.shuffle(y)#The operation object is a list (the value of y)
#If you write this
print(z)#Finally, run None
import random
y=random.sample(x,3)#For sampling, the second parameter is the maximum scale value of the first parameter, which cannot be exceeded.
#Returns a list of unique element k lengths selected from an overall sequence or collection. For random sampling without repetition

3. Real value
• random() -- generate a real value within 0 to 1, excluding 1
• uniform(a, b) - generate a real value between a and B ranges

import random


  1. Generate a random even number between 100 and 200.
import random
  1. Randomly arrange the numbers between 1 and 100.
import random

re module
Provides regular expression tools for advanced string processing
● also known as regular expression.
● Regular Expression
Abbreviated as regex, regexp or RE.
● often used to retrieve and replace those
Text that matches a pattern (rule).

● logic of string operation
● composed of defined specific characters
Rule string.
● regular strings are used to express pairs of words
An operation logic of a string.

● verify the validity of data
● replace text content
● extract substrings from strings
● reptiles

There are many functions. The following are common:

search(pattern,string)Find pattern in string
match(pattern,string)Match pattern at beginning of string
split(pattern,string)Split string according to pattern
findall(pattern,string)Return matches as a list
compile(pattern)Create schema object
x="I will make a lot of money in 2022!"
import re
[aeiou]Match any letter in brackets
[0-9]Match any number. Similar to [0123456789]
[a-z]Match any lowercase letters
[A-Z]Match any uppercase letters
[a-zA-Z0-9]Match any letters and numbers
[^aeiou]Matches all characters except aeiou letters
[^0-9]Matches characters other than numbers
[Pp]ythonMatching the meaning of "Python" or "Python" [] means or. The elements and objects in it are returned if any

Use a period to segment a text:

x="I will make a lot of money in 2022. It will work this year. I can't stop my confused steps."
import re
print(re.split(". ",x))

Third party modules:
Installation using pip
View in cmd

pip --version -- returns the current version
pip list - see which packages are currently installed
pip install package name - installs the specified package (the first version is installed by default. If it is incompatible, the old version is required, which can be entered in this way - pip install package name = = 1.19.3)

pip install numpy==1.19.3  #numpy is the package name

You can also upgrade to the latest version:

pip install --upgrade numpy

Uninstall package:

pip uninstall numpy

Keywords: Python Back-end

Added by Ambush Commander on Sat, 12 Feb 2022 05:12:54 +0200