摘要: Must know functions in Python explained with code

 


images/20210819_1_1.jpeg

▲Photo by Christopher Gower(來源:Unsplash)

In this article, five important functions are explained in detail with code. These functions are simple yet the most powerful function in Python. Python has many libraries and built-in functions. Understanding these functions and using them properly helps with efficient programming.

1. Lambda Function:

One of the most powerful functions in Python also known as the anonymous function. It is known as anonymous because we can instantiate and declare a function without a name. If you have a single operation to be executed, the lambda function is extremely useful instead of declaring a traditional function. Lambda is similar to the function, except it can only return only one expression.

images/20210819_1_2.jpg

Note:

  • The syntax for lambda function is lambda arguments: expression
  • Lambda doesn’t require a name and, returns statement lambda keyword is used.
  • Also, notice that the function is called a reference variable called the answer.
  • You can also use lambda functions inside other functions.
  • Lambda is similar to the function, except it can only return only one expression.

2. Map Function:

The map is a built-in Python function used by programmers to make the program simpler. This function iterates up on all the specified elements without the usage of any loops.

images/20210819_1_3.jpg

Note:

  • Syntax for this function is map(function,iterables)
  • In this example, notice that a user-defined function add_list has been used to add two variables.
  • The output for this example will be another list [5, 10, 8].
  • To explore more of the map capabilities try replacing the function with lambda and instead of just a list you can also try to work with tuples, sets.

3. Filter Function:

The filter is a built-in Python function, which is useful when it is required to segregate any kind of data. It is used to extract or filter the data based on the given condition.

images/20210819_1_4.jpg

Note:

  • Syntax for filter is filter(function,iterable)
  • A user-defined function is required to return a boolean value.
  • The elements for which the function returns true, only those elements are returned by the filter function.
  • The output for the used example is a list [1, 3, 5, 6].
  • Unlike map, the filter takes only one iterable in this case, we can make a list of positive and negative numbers.

4. Zip Function

The zip is a built-in function that is used to extract data from different columns of the database and change it into a tuple.

images/20210819_1_5.jpg

Note:

  • Syntax for this function is zip(*iterables)
  • It ideally combines two given data or lists into a tuple.
  • The output for this example will be [(‘Mick’, ‘12121’), (‘John’, ‘56161’), (‘Tessa’, ‘33287’), (‘Nick’, ‘23244’)].

5. Reduce function:

This function is used when it is required to apply the same operation to all the elements in the given list.

images/20210819_1_6.jpg

Note:

  • The syntax for reduce function is functools.reduce(function, iterable)
  • This function is not built-in, to use it import the functools module.
  • Output for this program is 16, it returns the sum of the list.

轉貼自Source: medium.com

若喜歡本文,請關注我們的臉書 Please Like our Facebook Page:    Big Data In Finance

 


留下你的回應

以訪客張貼回應

0
  • 找不到回應

Advertisement