Module - definition and related use

modular

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
print("start")
time.sleep(2)   #Pause time, the number in () represents the pause time
print("end")

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):
    i+=1
end=time.time()#End time
print(end-star)
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
print(math.pi)
print(math.cos(math.pi/2))

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
x=['a','d','f','g',',t']
print(random.choice(x))#Returns a random element from a non empty sequence
import random
x=range(0,11)
y=list(x)
random.shuffle(y)#The operation object is a list (the value of z)
print(y)
#If you write this
z=random.shuffle(y)
print(z)#Finally, run None
import random
x=range(0,11)
y=list(x)
print(y)
random.shuffle(y)#The operation object is a list (the value of y)
print(y)
#If you write this
z=random.shuffle(y)
print(z)#Finally, run None
x=['a','d','f','g',',t']
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
print(y)

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
print(random.random())
print(random.uniform(3,4))

practice:

  1. Generate a random even number between 100 and 200.
import random
print(random.randrange(100,200,2))
  1. Randomly arrange the numbers between 1 and 100.
import random
x=range(1,101)
y=list(x)
random.shuffle(y)
print(y)

re module
Provides regular expression tools for advanced string processing
Concept:
● 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).

use:
● 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.

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

There are many functions. The following are common:

functiondescribe
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
print(re.search('2022',x))
print(re.match("I",x))
print(re.split("",x))
print(re.findall("2",x))
exampledescribe
[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

practice:
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