SoFunction
Updated on 2025-03-02

Five super useful functions that cannot be missed in Python

1. Introduction

In this article, we have explained in detail with codePythonThe important role of the 5 practical functions of the Chinese Super League.
Although these functions are simple,PythonThe 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

LambdaThe function isPythonOne 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, uselambdaFunctions are very useful to replace traditional functions.LambdaSimilar to a normal function, except that it can only return one expression.

Next we useLambdaFunction to find expressions(a+b)^2Value 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
  • LambdaFunctions do not require function names, and return statements that use Lambda keywords.
  • Note that the above Lambda function uses variablesanswerto call.
  • We can also use it in other functionsLambdafunction.
  • 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 programsPythonBuilt-in function, this function can iterate over all specified elements without using any loops.

Next we use the Map function to match twolistSum 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 functionadd_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 moremapFunction functions, we can uselambdaReplace the above function, of course, we can not only operate on list, but also ontupleand set to operate.

4. Filter function

Filter isPythonAnother built-in function in it is very useful when you need to distinguish other types of data.FilterFunctions 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 returnboolType 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 YesPythonAnother 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 listReducefunction.

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 notPythonBuilt-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!