Coding With Fun
Home Docker Django Node.js Articles FAQ

python's 5 advanced usages, efficiency improvement is no problem!


May 30, 2021 Article blog


Table of contents


 python's 5 advanced usages, efficiency improvement is no problem!1

The high-level characteristics of any programming language are usually discovered through extensive experience. F or example, you're writing a complex project and looking for answers to a question on stackoverflow. Then you suddenly find a very elegant solution that uses Python features you never knew about!

This way of learning is so interesting: by exploring, you stumble upon something.

Here are Python's 5 advanced features and how they are used.

Foreword egg

Lambda function

The Lambda function is a relatively small anonymous function -- anonymous means that it does not actually have a function name.

Python functions are usually defined using the def a_function_name() style, but for lambda functions we don't name them at all. This is because the function of the lambda function is to perform some simple expression or operation without having to fully define the function.

Lambda functions can use any number of arguments, but expressions can have only one.

 python's 5 advanced usages, efficiency improvement is no problem!2

See how easy it is! W e performed some simple mathematical operations without having to define the entire function. This is one of Python's many features that make it a clean, simple programming language.

Map function

Map() is a built-in Python function that applies functions to elements in various data structures, such as lists or dictionaries. This is a very clean and readable way to perform this operation.

 python's 5 advanced usages, efficiency improvement is no problem!3

Filter function

The filter built-in function is very similar to the map function, which also applies functions to sequence structures (lists, tuples, dictionaries). The key difference between the two is that filter() returns only the element that the application function returns True.

See the following example for details

 python's 5 advanced usages, efficiency improvement is no problem!4

Not only did we evaluate true or False for each list element, but the filter() function also ensured that only elements that matched true were returned. It's easy to handle the two steps of checking expressions and building a return list.

Itertools module

Python's Itertools module is a collection of tools for dealing with iterators. An iterator is a type of data that can be used in for loop statements, including lists, tuples, and dictionaries.

Using the functions in the Itertools module allows you to perform many iterator operations that typically require multiple lines of functions and complex list understanding. For the magic of Itertools, see the following example:

 python's 5 advanced usages, efficiency improvement is no problem!5

 python's 5 advanced usages, efficiency improvement is no problem!6

Generator function

The Generator function is an iterator-like function, i.e. it can also be used in for loop statements. This greatly simplifies your code and saves a lot of memory compared to a simple for loop.

For example, we want to add all the numbers from 1 to 1000, and the first part of the following block shows you how to use the for loop to do this calculation.

If the list is small, such as 1000 rows, the memory required for the calculation is OK. B ut if the list is huge, such as a billion floats, there's a problem with that. W ith this for loop, there will be a large number of lists in memory, but not everyone has unlimited RAM to store so many things. The same is true of the range() function in Python, which builds lists in memory.

The second part of the code shows the use of the Python generator function to sum the list of numbers. T he generator function creates elements and stores them in memory only when necessary, one at a time. T his means that if you're creating a billion floats, you can only store them in memory one at a time! The xrange() function in Python 2.x is to use generator to build a list.

The above example shows that if you want to generate a list for a large range, you need to use the generator function. This approach is especially important if you have limited memory, such as using mobile devices or edge computing.

That is, if you want to iterate multiple times on the list and it is small enough to fit into memory, it's best to use the range function in the for loop or Python 2.x. Because generator and xrange functions generate new list values each time you access them, the Python 2.x range function is a static list, and integers are already in memory for quick access.

 python's 5 advanced usages, efficiency improvement is no problem!7

Recommended lessons: Python3 Getting Started, Python3 Advanced, Python Static Reptiles