Home > Backend Development > Python Tutorial > A must read for beginners! Top 10 underrated Python libraries!

A must read for beginners! Top 10 underrated Python libraries!

WBOY
Release: 2023-04-12 18:19:05
forward
1999 people have browsed it

A must read for beginners! Top 10 underrated Python libraries!

In the process of learning python, everyone will understand that a powerful function of python lies in various powerful third-party library functions. You only need to install us through pip install required library functions.

People often only pay attention to the python libraries they have installed, but ignore the library functions that come with python, or the libraries that come with python. Today I will introduce to you the top ten undervalued python libraries. With a library or a function that comes with python.

1.Counter

The main purpose of Counter can be seen from the name. It is used for counting statistics. When performing data analysis or statistics, we often need to use counting. , and Counter will help us write more concise code, as shown in the figure below.

A must read for beginners! Top 10 underrated Python libraries!

In the above program, you can see that using Counter can complete character statistics with a simple two-line program, and can also perform sorting and output operations, which greatly simplifies the code. .

2.NamedTuple

nametuple inherits the tuple class. Using nametuple, you can create a class object, and the object has accessible attributes. What is the use of this? We use the following program to explain.

A must read for beginners! Top 10 underrated Python libraries!

In the above program, the first program in the comment does not use nametuple, and when there are too many features, we want to use one or some of them. At this time, it is not easy for us to obtain the index of the feature, which causes unnecessary trouble. When we use nametuple, we can easily access the characteristics of a specific data through the characteristic name.

3.DefaultDict

Defaultdict is a dictionary with a default initial value. When the key value we access defaultdict does not exist, it will return the default value, such as the following program:

A must read for beginners! Top 10 underrated Python libraries!

In the program, we created a defaultdict of type int. When we want to store "python" in the dictionary, dict will report an error, and defaultdict will have an initialized default The value is 0, allowing us to easily store values ​​by key value. Here we create a defaultdict of type int, so the default initial value is 0. When we create a defaultdict of list, the default initial value is an empty list.

4.Glob

The Glob module is mainly used to match file information under the path we specify, and return the matched absolute path of the file in the form of a list, as shown in the following figure:

A must read for beginners! Top 10 underrated Python libraries!

In the program, we matched all jpg and png images under the path and returned their absolute paths.

5.Math

The Math module is a simple mathematical function module that comes with Python, including exponential, logarithm, sine and cosine functions.

A must read for beginners! Top 10 underrated Python libraries!

6.Argparse

The use of the Argparse module, on the one hand, allows us to write command line interfaces, greatly beautify our programs, and make our programs look beautiful. It looks more beautiful, and on the other hand, it also makes it more convenient for us to modify the program.

A must read for beginners! Top 10 underrated Python libraries!

In the above program, we defined two parameters a and b and stipulated that they must be input. argparse will automatically parse our input data and convert it into the specified Type (int), then our main program adds the two numbers and outputs the result. Here I just briefly show you the function of argparse. Its powerful charm needs to be reflected in a large number of programs.

7.Copy

For the copy module, it mainly involves the contents of shallow copy and deep copy. Here you need to understand the difference between shallow copy and deep copy. For deep copy, whether it is variable Whether it is an object or an immutable object, a new variable is created. For shallow copies, it is more complicated, as shown in the following figure:

A must read for beginners! Top 10 underrated Python libraries!

Here we use a program to The above diagram is explained as follows:

A must read for beginners! Top 10 underrated Python libraries!

As shown in the figure above, for immutable objects, shallow copy just copies the same address to the copy object. But when we modify the value of simple_str, copy_simple_str does not change. This is because the string is an immutable type, so when we modify the value of simple_str, the compiler will open up a new space, save the original value of simple_str, and let copy_simple_str point to it. For complex variable types, it can be seen from the results that the sub-objects are not completely copied, so modifying copy_complex_dict will also modify the sub-objects in complex_dict.

8.itertools

The itertools module contains many useful iterator functions, and skillful use of them can greatly improve work efficiency. Here, the editor simply gives a few examples of using itertools:

A must read for beginners! Top 10 underrated Python libraries!

#In the above program, permutations are used to generate all permutations and combinations, and the count function is combined with zip to generate serial number. It should be noted that the permutations and count here generate iterators, which can greatly save memory space.

9.enum

In python, the enum module also implements support for enumeration types. In the enumeration collection, the enumeration members should be unique and immutable.

A must read for beginners! Top 10 underrated Python libraries!

In the above figure we define a Week enumeration class, and then use this class to calculate the day of the week after today.

10.calendar

The calendar module is used to process calendar-related functions. For example, our most common judgment is whether it is a leap year. calendar only needs one line of program to help us complete the calculation:

A must read for beginners! Top 10 underrated Python libraries!

In the above program, we use calendar to output whether it is a leap year, print the monthly calendar, determine the day of the week, etc. Using calendar can achieve twice the result with half the effort in dealing with calendar issues.

The above 10 python modules are very useful python built-in modules, but they are rarely used in daily programming. Through the editor’s explanation, I hope everyone can learn more about python’s built-in modules. Powerful module, you can continuously optimize your own program in future programming.

The above is the detailed content of A must read for beginners! Top 10 underrated Python libraries!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template