Hi guys, we have already seen various sequences in Python and have also gone in detail about its mutability. Now, we will see some more built-in functions which are just like features of Python. Before going for these functions we are first gonna see that how does all the functions in Python work as objects.

Functions as Objects

In Python functions are treated as first-class objects. Using functions as arguments can be particularly convenient in conjunction with lists. It allows a style of coding called higher-order programming.

This style is such that any function can be simply used as an object of Python. We will see following example to understand that how does it work.

Click here to Copy the Code

Let’s see the output:

Click here to Copy the Code

In this example, you can see that how do we pass on the in-built functions like abs as an argument to the existing function and afterwards we were using the defined function for different kinds of operations as required just as we use an object. Now we go to map().

map()

The map() function applies a given function to each item of an iterable (list, tuple etc.) and returns a list of the results which significantly reduces the lines to code. Mostly lambda function is used in map(). Following is its syntax:

Click here to Copy the Code

Following is the example to understand its working:

Click here to Copy the Code

filter()

The filter() method filters the given iterable with the help of a function that tests each element in the iterable to be true or not. This reduces the IF statements required in a particular piece of code and reduces its complexity as well as speeds up the execution. Moreover, it also helps for rapid application development as it reduces number of lines of codes. Following is the syntax:

Click here to Copy the Code

Following is the example to understand its working:

Click here to Copy the Code

From its working, just like map(), filter() also works mainly with lambda functions. Also, from the above example, we can note following points about filter():

  1. Unlike map(), only one iterable is required.
  2. The function argument is required to return always a boolean type. If it doesn’t, filter simply returns the iterable passed to it. Also, as only one iterable is required, it’s implicit that function must only take one argument.
  3. filter passes each element in the iterable through function and returns only the ones that evaluate to true. I mean, it’s right there in the name — a “filter”.

reduce()

The function reduce(func, seq) continually applies the function func() to the sequence seq. It always returns a single value as shown in the example below:

Click here to Copy the Code

In the above example, we can see that it only returns with the maximum value from the list. This is how reduce as its name indicates can be used to reduce a set of data to a single value in order to find any unique feature from it. In current example, we found the maximum from the set of data, in the same way it can be used to extract any specific feature from the set of available data.

So that’s it for this post guys. In next post, we are gonna see about Exception Handling in Python.