How to import modules
✅import module_name
✅from nodule_name import name1,name2…
✅from module_name import *
✅from module_name import func as domo_func
time module
The time module is a time-related module
()
Delayed execution time
import time print('hello python world') (2.0) # Delay execution for 2 secondsprint('life is short,i use python')
()
Second time stamp
import time # The timestamp represents the offset calculated in seconds starting from 00:00:00 on January 1, 1970print(())
()
Check local time
import time print(())
()
Custom time format Receives time tuples and returns the local time represented by strings, the format is determined by the parameter format
import time print(('%Y-%m-%d %H:%M:%S'))
Common formatting parameters in strfttime
parameter | describe |
---|---|
%Y | Local full year, e.g. 2022 |
%y | Remove the years of the century, such as 22 |
%M | Minutes |
%m | month |
%d | What day of a month |
%H | What hour of the day |
%S | Number of seconds |
%A | Local full week name, such as Saturday |
%a | Local simplified week names, such as Sat |
%B | Local full month name, such as March |
%b | Locally simplified month names, such as Mar |
%p | Show whether the local time is am or pm |
%x | Local corresponding date, for example, 03/05/22 |
%X | Local corresponding time, for example, 18:32:07 |
%j | What day of the year |
datetime()
💡datetime is also a time-related module, but not a module in time, you need to import it?
() Output the current time
import datetime print(())
random module
random module is a random module
()
Randomly generated number of [0,1)
import random print(())
()
Randomly generate integers
import random print((1, 5))
()
Randomly take elements in sequence
import random print(('1234hello python world'))
()
import random li = [1, 4, 7, 5, 3, 0] # Unorder the passed container, note 1: What is changed is the container itself. Note 2: Containers cannot be tuples(li) # Randomly arrange list elementsprint(li)
()
Random integer
import random print((1, 10, 2)) # Randomly fetched1,3,5,7,9
()
Random sampling
import random li = [1, 4, 6, 5, 18, 2, 9, 7] print((li, 3)) # Random fromliTake three elements in the list
💖 Extension: Use random module to implement 6-bit random verification code
import random def rd(): res = '' for i in range(6): num = (0, 9) res += str(num) print(res) rd()
import random res = (range(0, 9), 6) f_li = list(map(str, res)) # Map --> Pass each element in res to str one by one for strong rotation, and returnprint("".join(f_li))
json module
JSON is a widely used lightweight data format. The json module provides the processing function of JSON data. Since JSON and dictionary formats are very similar, the json module is also used to convert json and dictionary. But be aware that the data in json must be wrapped in double quotes
()
Convert json to dictionary (applicable to statements)
import json json_data = '{"name": "hacker"}' dict_data = (json_data) print(dict_data)
()
Convert dictionary to json (applicable to statements)
import json dict_data = {"name": "hacker"} json_data = (dict_data) print(json_data)
()
Convert json to dictionary (for files)
()
Convert dictionary to json (for files)
OS module
OS modules are modules related to operating systems
()
Used to print the current working directory
import os print(()) # D:\pythonExample\object
()
Change the current working directory
import os print(()) # D:\python instance\object(r'D:\python instance\python') # Add r to prevent escapeprint(()) # D:\pythonExample\python
()
Create multiple folders in recursive form
import os # If the folder exists, an error will be reported(r'/home/response/hacker')
()
Recursively delete multiple empty folders
import os # Non-empty deletion will report an error(r'/home/response/hacker')
()
Create a single folder
import os # Only create single folders(r'hacker')
()
Delete a single folder
import os # Only create single folders(r'hacker')
()
Determine whether the file or folder exists in this path
import os # When it exists, it returns True, but when it does not exist, it returns Falseprint(('images'))
()
Splicing path
import os print(((), "happy"))
sys module
The sys module is a module that interacts with the python interpreter
[]
[0]—>Program itself file path
[1]—> Parameters passed during external runtime
()
Used to exit the program
The above are the commonly used built-in modules for Python compiled by bloggers
This is the end of this article about the super detailed summary of Python’s commonly used built-in modules. For more related contents of Python’s commonly used built-in modules, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!