1. Introduction
In this article, we have explained in detail with codePython
The important role of the 5 practical functions of the Chinese Super League.
Although these functions are simple,Python
The most powerful function in this.
Python has many libraries and built-in functions, and understanding and using these functions correctly helps us program efficiently.
2. Lambda function
Lambda
The function isPython
One of the most powerful functions in it, it is sometimes called anonymous functions.
It is called anonymous function because we can instantiate and declare a function without a name. If you want to perform a single operation, uselambda
Functions are very useful to replace traditional functions.Lambda
Similar to a normal function, except that it can only return one expression.
Next we useLambda
Function to find expressions(a+b)^2
Value of
The code is as follows:
answer = lambda a, b: a**2 + b**2 + 2*a*b print(answer(3, 6))
Notes are as follows:
- The syntax of the Lambda function is:
lambda arguments: expression
-
Lambda
Functions do not require function names, and return statements that use Lambda keywords. - Note that the above Lambda function uses variables
answer
to call. - We can also use it in other functions
Lambda
function. - Lambda is similar to a normal function, except that it can only return one expression.
3. Map function
Map is used by programmers to simplify programsPython
Built-in function, this function can iterate over all specified elements without using any loops.
Next we use the Map function to match twolist
Sum the corresponding elements and generate a new list
def add_list(a,b): return a+b output = list(map(add_list,[2,6,3],[3,4,5])) print(output)
Notes are as follows:
- The syntax of the Map function is: map(function,iterables)
- In the above example, our custom function
add_list
The function is to sum two variables - The result of running the above example is another list [5, 10, 8]
- If we want to explore more
map
Function functions, we can uselambda
Replace the above function, of course, we can not only operate on list, but also ontuple
and set to operate.
4. Filter function
Filter isPython
Another built-in function in it is very useful when you need to distinguish other types of data.Filter
Functions are often used to extract data based on specific filtering conditions.
def is_positive(a): return a>0 output = list(filter(is_positive,[1,-2,3,-4,5,6])) print(output)
Notes are as follows:
- The syntax of the Filter function is:
filter(function,iterable)
- The above custom function must require a return
bool
Type value - The Filter function only returns elements that satisfy the return value of the custom function.
- The return value of the above example is [1,3,5,6]
5. Zip function
zip YesPython
Another built-in function in it is mainly used to extract data from different columns of the database and combine it into tuples.
user_id = ["12121","56161","33287","23244"] user_name = ["Mick","John","Tessa","Nick"] user_info = list(zip(user_name,user_id)) print(user_info)
Notes are as follows:
- The syntax of the Zip function is:
zip(*iterables)
- This function mainly functions to combine two given lists into a tuple.
- The output of the above example is [('Mick', '12121'), ('John', '56161'), ('Tessa', '33287'), ('Nick', '23244')]
6. Reduce function
Used when the same operation is required for all elements in a given listReduce
function.
The code is as follows:
import functools def sum_two_elements(a,b): return a+b numbers = [6,2,1,3,4] result = (sum_two_elements, numbers) print(result)
Notes are as follows:
-
Reduce
The syntax of the function is:(function, iterable)
- The Reduce function is not
Python
Built-in function, you need to import it when using itfunctools
Module - The output of the above code is 16, which implements the function of summing all elements of the list.
7. Summary
This article focuses on some powerful built-in functions in Python. These functions can greatly improve our coding efficiency and provide corresponding code examples.
This is the end of this article about five super useful functions that cannot be missed in Python. For more relevant content on immortal-level functions in Python, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!