Home  >  Article  >  Backend Development  >  5 commonly used Python standard libraries

5 commonly used Python standard libraries

Guanhui
GuanhuiOriginal
2020-05-28 16:50:4115513browse

5 commonly used Python standard libraries

5 commonly used Python standard libraries

1. Provides many function libraries os associated with the operating system;

The os package is the interface between Python and the operating system. We can use the os package to implement many functions of the operating system, such as managing system processes, changing the current path (equivalent to 'cd'), changing file permissions, etc., to create. But please note that the os package is built on the operating system platform, and many functions cannot be implemented on Windows systems. In addition, when using the os package, be aware that some of its functions have been replaced by other packages.

We manage the files stored on the disk through the file system. Finding, deleting, copying files, and listing files are common file operations. These functions can often be seen in the operating system (such as ls, mv, cp and other Linux commands), but now they can be used through the glob package, shutil package, os.path package, and some functions of the os package in the Python standard library. Implemented inside Python.

2. The library sys is usually used for command line parameters;

The sys package is used to manage Python’s own running environment. Python is an interpreter and a program that runs on the operating system. We can use the sys package to control many parameters of this program, such as the memory and CPU that Python can occupy, the path that Python scans, etc. Another important function is to interact with Python's own command line and read commands and parameters from the command line.

3. Random library for generating random numbers;

The random function in the Python standard library can generate random floating point numbers, integers, strings, and even help You randomly select an element in the list sequence

, shuffle a set of data, etc.

4. The math library provides mathematical constants and mathematical functions;

In the standard library, Python defines some new number types (decimal package, fractions package), To make up for the possible shortcomings of the previous numeric types (integer, float). The standard library also contains the random package, which is used to handle random number-related functions (generating random numbers, random sampling, etc.). The math package adds some important mathematical constants and mathematical functions, such as pi, trigonometric functions, etc.

(Although numpy is not a package in the standard library, its good support for array operations makes it widely used in Python-based scientific research and calculations, so you can pay appropriate attention.)

5. Date and time operation library datetime.

# Date and time management is not complicated, but it is easy to make mistakes. The management of date and time in Python's standard library is quite complete (use the time package to manage time, use the datetime package to manage date and time), you can not only query and transform date and time (for example: corresponding to July 18, 2012 is the day of the week), and can also perform calculations on date and time (for example, what date and time will be 378 hours after 2000.1.1 13:00). Through these standard libraries, you can also control the text format of date and time output as needed (for example: output '2012-7-18' or '18 Jul 2012')

Recommended tutorial: "Python Tutorial

The above is the detailed content of 5 commonly used Python standard libraries. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn