This article focuses on five of the most important functions in Python, with code examples to show why they are useful. These functions are simple to write, but can be useful in optimizing the structure of your code. Python has many libraries and built-in functions, and understanding these functions and using them in everyday programming will help you program effectively.

1. The Lambda function

Anonymous functions are an important function in Python. We use anonymous functions in many ways. Anonymous functions simply declare functions without giving them names. Using lambda functions is extremely effective if there is a simple requirement that needs to be implemented. Lambda functions are very much like functions, except that lambda can only return one expression.

result = lambda a,b: (a+b)**2
print result(10.20) # 900
Copy the code

Directions for use

  • Lambda function of the formlambda arguements: expression
  • Lambda functions do not need names; they need to be declaredlambdaThe keyword
  • The variable returned by a lambda expression can be called as a function, as inThe result (10, 20)
  • Lambda expressions can also be used inside other functions

2. The Map function

Map is a built-in Python function that programmers can use to make their programs simpler. This function iterates over all specified elements without using any loop.

def add_list(self, a, b) :
  return a + b

def test_add_list(self) :
  output = list(map(self.add_list, [1.2.3], [4.5.6]))
  print output # [5, 7, 9]
Copy the code

Instructions:

  • Syntax for using mapmap(function, iterables)
  • Different functions can be used to extend the specific functionality of the Map

3. The Filter function

The filter function is also a built-in Python function, which is useful when you need to separate the desired data from a pile of data. The filter function mainly extracts or filters data based on certain given conditions.

def is_positive(self, num) :
  return num > 0

def test_filter(self) :
  res = filter(self.is_positive, [1, -2, -1.2.3.4, -3])
  print res  # [1, 2, 3, 4]
Copy the code

Instructions:

  • Syntax for using filterfilter(function, iterables)
  • The function used by filter needs to return a Boolean type
  • Iterables are filtered only if they are true in the function
  • Unlike the map function, the filter function supports only one list for filtering, whereas the map function supports multiple list data

4. Zip function

The zip function is also a built-in Python function that is used to extract data from different columns of data to form a tuple.

def test_zip(self) :
  user_name = ['zs'.'ls'.'ww']
  user_id = [1.2.3]
  user_age = [20.21.22]
  print zip(user_age, user_id, user_name)  # [(20, 1, 'zs'), (21, 2, 'ls'), (22, 3, 'ww')]
Copy the code

Instructions:

  • Syntax for using zipzip(*seq)
  • Zip can combine two or multiple groups of data to form a tuple

5. Reduce function

The Reduce function is also a built-in Python function, used if you want to apply the same operation to all elements in a given list.

def test_reduce(self) :
  l = [1.2.3.4.5.6.7.8.9.10]
  # lambda form
  print reduce(lambda a, b: a + b, l) # 55
Copy the code

Instructions:

  • Syntax for using reducereduce(function, iterable)

6. Summary

Here we mainly introduce five important built-in functions in Python, including lambda function, map function, filter function, zip function and Reduce function. These functions can help simplify our program in our daily programming process, make the code look more clean, and reduce the amount of code.

Keep thinking, keep coding! 2021-07-25

Personal official account: AaronCoding