Built-in Python Libraries
This article will introduce several commonly used built-in libraries (standard libraries) in Python.
This article is merely a brief example of commonly used built-in libraries; for complete usage, please refer to the official Python documentation
1. JSON (json
library)
Used for generating and parsing JSON data.
Python |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | import json
d = {
'name': 'Tom',
'age' : 25,
'job' : 'Student',
'desc': 'A student',
}
print('Output as JSON string :', json.dumps(d))
print('Output as JSON string (specified separator compact form):', json.dumps(d, separators=(',', ':')))
print('Output as JSON string (specified other separators) :', json.dumps(d, separators=(', ', ' = ')))
print('Output as JSON string (Chinese etc. not converted to ASCII code):', json.dumps(d, ensure_ascii=False))
print('Output as JSON string (specified indentation) :', json.dumps(d, indent=2))
s = '{"name": "Tom", "age": 25, "job": "Student", "desc": "A student"}'
print('Parsed as Python object:', json.loads(s))
|
Output:
Text Output |
---|
| Output as JSON string : {"name": "Tom", "age": 25, "job": "Student", "desc": "\u4e00\u4f4d\u5b66\u751f"}
Output as JSON string (specified separator compact form): {"name":"Tom","age":25,"job":"Student","desc":"\u4e00\u4f4d\u5b66\u751f"}
Output as JSON string (specified other separators) : {"name" = "Tom", "age" = 25, "job" = "Student", "desc" = "\u4e00\u4f4d\u5b66\u751f"}
Output as JSON string (Chinese etc. not converted to ASCII code): {"name": "Tom", "age": 25, "job": "Student", "desc": "A student"}
Output as JSON string (specified indentation) : {
"name": "Tom",
"age": 25,
"job": "Student",
"desc": "\u4e00\u4f4d\u5b66\u751f"
}
Parsed as Python object: {'name': 'Tom', 'age': 25, 'job': 'Student', 'desc': 'A student'}
|
2. Regular Expressions (re
library)
Regular expressions are more complex; the following are some simple examples. For complete official documentation, please refer to re — Regular expression operations
Python |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | import re
# Search for strings
m = re.search('[0-9]+','abcd1234xyz')
if m:
print('Search for content matching regular expression:', m.group(0))
else:
print('Cannot find specified content')
# Check if it matches
m = re.match('[0-9]+','abcd1234xyz')
if m:
print('String can match regular expression:', m.group(0))
else:
print('String does not match regular expression')
# Replace strings
result = re.sub('[0-9]+', '_', 'abc123abc')
print('Replaced string:', result)
|
Output:
Text Output |
---|
| Search for content matching regular expression: 1234
String does not match regular expression
Replaced string: abc___abc
|
3. Time (time
library)
The built-in time library in Python is relatively complex to use; it is recommended to only use it for simple purposes.
For complex time processing requirements, please use the third-party arrow
library.
Python |
---|
| import time
print('Current timestamp (seconds) :', time.time())
print('Current timestamp (nanoseconds):', time.time_ns())
|
Output:
Text Output |
---|
| Current timestamp (seconds) : 1662041320.7340803
Current timestamp (nanoseconds): 1662041320734110181
|
4. Paths (os.path
library)
Path libraries can conveniently generate and parse file paths when handling files.
Python |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 | from os import path
p = '/data/sample/file.txt'
print('Return filename :', path.basename(p))
print('Return file directory :', path.dirname(p))
print('Split file directory and filename:', path.split(p))
print('Combine file directory and filename:', path.join('/data/sample', 'doc', 'file.txt'))
print('Simplify redundant paths:', path.normpath('/data/tmp/../sample/file.txt'))
files = [
'/data/sample/file.txt',
'/data/sample/file2.txt',
'/data/file3.pdf'
]
print('Query common part of file paths:', path.commonprefix(files))
print('Check if file or directory exists:', path.exists(p))
print('Return file size :', path.getsize(p))
print('Return last read time :', path.getatime(p))
print('Return last modification time:', path.getmtime(p))
print('Check if it is a regular file:', path.isfile(p))
print('Check if it is a directory:', path.isdir(p))
|
Output:
Text Output |
---|
1
2
3
4
5
6
7
8
9
10
11
12 | Return filename : file.txt
Return file directory : /data/sample
Split file directory and filename: ('/data/sample', 'file.txt')
Combine file directory and filename: /data/sample/doc/file.txt
Simplify redundant paths: /data/sample/file.txt
Query common part of file paths: /data/
Check if file or directory exists: True
Return file size : 20
Return last read time : 1662041767.7110395
Return last modification time: 1662041767.7110395
Check if it is a regular file: True
Check if it is a directory: False
|
5. Mathematics (math
library)
The math library contains more functions related to mathematical operations. If you want to use more advanced mathematical features, please use the third-party math library numpy
.
Python |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 | import math
print('Natural constant e:', math.e)
print('Pi constant pi :', math.pi)
print('Ceiling function :', math.ceil(1.2))
print('Floor function :', math.floor(1.9))
print('Exponentiation :', math.pow(2, 10))
print('Logarithm :', math.log(1024, 2))
print('Square root :', math.sqrt(100))
print('Radians to degrees:', math.degrees(0.25 * math.pi))
print('Degrees to radians:', math.radians(45))
print('Trigonometric function sin (argument in radians) :', math.sin(0.25 * math.pi))
print('Trigonometric function cos (argument in radians) :', math.cos(0.25 * math.pi))
print('Trigonometric function tan (argument in radians) :', math.tan(0.25 * math.pi))
print('Trigonometric function asin (argument in radians):', math.asin(0.25 * math.pi))
print('Trigonometric function acos (argument in radians):', math.acos(0.25 * math.pi))
print('Trigonometric function atan (argument in radians):', math.atan(0.25 * math.pi))
|
Output:
Text Output |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | Natural constant e: 2.718281828459045
Pi constant pi : 3.141592653589793
Ceiling function : 2
Floor function : 1
Exponentiation : 1024.0
Logarithm : 10.0
Square root : 10.0
Radians to degrees: 45.0
Degrees to radians: 0.7853981633974483
Trigonometric function sin (argument in radians) : 0.7071067811865475
Trigonometric function cos (argument in radians) : 0.7071067811865476
Trigonometric function tan (argument in radians) : 0.9999999999999999
Trigonometric function asin (argument in radians): 0.9033391107665127
Trigonometric function acos (argument in radians): 0.6674572160283838
Trigonometric function atan (argument in radians): 0.6657737500283538
|
6. Random (random
library)
Includes various random generation functions.
Python |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | import random
l = [1, 2, 3, 4, 5]
print('Return a random element from the list :', random.choice(l))
print('Return 3 random elements from the list:', random.sample(l, 3))
random.shuffle(l)
print('Return elements randomly shuffled (shuffled) list:', l)
print('Generate a real number in the [0, 1) interval :', random.random())
print('Generate a real number in the [5, 10] interval :', random.uniform(5, 10))
print('Generate an integer in the [5, 10] interval :', random.randint(5, 10))
print('Generate an integer in the [0, 10) interval :', random.randrange(10))
print('Generate an integer in the [5, 10) interval :', random.randrange(5, 10))
print('Generate an integer in the [5, 10) interval (step of 2) :', random.randrange(5, 10, 2))
|
Output:
Text Output |
---|
| Return a random element from the list : 3
Return 3 random elements from the list: [3, 4, 2]
Return elements randomly shuffled (shuffled) list: [5, 1, 3, 2, 4]
Generate a real number in the [0, 1) interval : 0.6214407642048402
Generate a real number in the [5, 10] interval : 9.359073537753634
Generate an integer in the [5, 10] interval : 8
Generate an integer in the [0, 10) interval : 8
Generate an integer in the [5, 10) interval : 6
Generate an integer in the [5, 10) interval (step of 2): 7
|
7. Base64 (base64
library)
Used for encoding and decoding data with Base64.
Python |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | import base64
s = 'Hello, World. This is a Python tutorial'
b64 = base64.b64encode(s.encode()).decode()
print('Encode string using Base64 :', b64)
b64 = 'SGVsbG8sIFdvcmxk44CC6L+Z6YeM5pivIFB5dGhvbiDmlZnnqIs='
s = base64.b64decode(b64.encode()).decode()
print('Decode string using Base64 :', s)
s = 'Hello, World. This is a Python tutorial'
b64 = base64.urlsafe_b64encode(s.encode()).decode()
print('Encode string using URL-safe Base64 :', b64)
b64 = 'SGVsbG8sIFdvcmxk44CC6L-Z6YeM5pivIFB5dGhvbiDmlZnnqIs='
s = base64.urlsafe_b64decode(b64.encode()).decode()
print('Decode string using URL-safe Base64 :', s)
|
b64encode(...)
、b64decode(...)
and urlsafe_b64encode(...)
、urlsafe_b64decode(...)
have different processing methods, do not mix them
Output:
Text Output |
---|
| Encode string using Base64 : SGVsbG8sIFdvcmxkLiBUaGlzIGlzIGEgUHl0aG9uIHR1dG9yaWFs
Decode string using Base64 : Hello, World. This is a Python tutorial
Encode string using URL-safe Base64 : SGVsbG8sIFdvcmxkLiBUaGlzIGlzIGEgUHl0aG9uIHR1dG9yaWFs
Decode string using URL-safe Base64 : Hello, World. This is a Python tutorial
|
8. Hash Algorithms (hashlib
library)
Hash algorithms library for processing data with MD5, SHA1, etc.
Python |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | import hashlib
s = 'Hello, World. This is a Python tutorial'
h = hashlib.md5()
h.update(s.encode())
print('Return hexadecimal MD5 of the string:', h.hexdigest())
h = hashlib.sha1()
h.update(s.encode())
print('Return hexadecimal SHA1 of the string:', h.hexdigest())
h = hashlib.sha256()
h.update(s.encode())
print('Return hexadecimal SHA256 of the string:', h.hexdigest())
h = hashlib.sha512()
h.update(s.encode())
print('Return hexadecimal SHA512 of the string:', h.hexdigest())
|
Output:
Text Output |
---|
| Return hexadecimal MD5 of the string: c2e7c7ebfc834c05b35be14e749651d0
Return hexadecimal SHA1 of the string: 30a36af04729d938179fd67a7f4bda6920e5e160
Return hexadecimal SHA256 of the string: b5ac91d338061d3a3cfd458b05ea3e15f3b565a18d28f5ee732a3803e381e6fc
Return hexadecimal SHA512 of the string: 2c40122b8faf2d72677cafffc7a24756a5d2d9a8a7094e1b82d0bfb0e76b3b491b4347bdaf15332f10c79f05a2ff6ced9b240129b7db47ca632bb65219f4d47a
|