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.
Let’s see the output:
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:
Following is the example to understand its working:
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:
Following is the example to understand its working:
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():
- Unlike map(), only one iterable is required.
- 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.
- 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:
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.
Hello there! This is kind of off topic but I need
some guidance from an established blog. Is it very hard to set
up your own blog? I’m not very techincal but I can figure things
out pretty fast. I’m thinking about setting up my own but I’m not sure where to begin. Do you have any points or suggestions?
Many thanks
P.S. If you have a minute, would love your feedback on my new website re-design. You can find it
by searching for “royal cbd” – no sweat if you can’t.
Keep up the good work!
Noticed sir..