We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
So till now we have seen functions with a name.
So there are functions in Python called
anonymous or lambda functions which do not have a name. These are extremely useful, and they're extensively used in data science and machine learning in general. But let's see, these are extremely powerful. I told you earlier, right, that some of my most favorite functions in Python are filter, map, and reduce function functions. We'll see how lambda functions are used along with functions like filter, map, and reduce to make code much more elegant, simple, and concise. Okay, so let's understand what they are, right? So typically, let's take examples. I think that'll explain things much better. Imagine here what I'm creating here is a simple function, and how is this function? I'm creating a function called double. Okay, what does that do? It basically doubles the number that I give, right? So if I say print double five, it doubles five into two. It returns five into two, which is ten. Now the same thing, I can write it using lambda functions or using anonymous functions like this. Okay, I can say double equals to. This is the function name, right? This is the function name. This is the function name. Here I'm using the keyword lambda, and I'm saying the input, this is your argument. This is your argument, and this is what it will output. The structure is elegant, right? You have the function name equals to. This equals to is important. You're using the word lambda, which is a keyword in python x. You're using a column here. So what you're saying here is I'm defining a function called double using anonymous functions, using lambda functions such that if you input x to me, I'll return x into two. See, all of this function definition is done in one line. Literally, these are called lambda functions. Very, very elegant. And here, remember here I've given a name called double to this function. I can avoid this altogether. I'll show you examples of how to avoid this altogether. But here I've given the name. Okay, so lambda functions could be anonymous, or they could be named anonymous, basically in English, means that there is no name to it. Okay? We can choose to have a name like this, or we can choose not to have. I'll show you examples in a little while. Okay, so when I say print double five, it again returns ten. So my function definition is literally this single line. It's literally this single line. What I'm saying here is my input is x, and I'm returning x into two. Literally, in this short snippet, in this one line, you're defining a whole function that's the elegance. Now, as I told you earlier that I really like using lambda functions with filter, map and reduce. Let's see the examples. Imagine I have a list containing five elements. 12345. Okay? And imagine if I want to produce a list of only even numbers in my bigger list, which means I want to find a subset of my list which satisfies a condition. What do we use there? We use filter, right? We use filter. Now look at how I do it. So here, what I'm doing here is I'm using the filter function as we know. What did we do earlier? In the filter function, we used to give a function name here, followed by an iterable data structure, like a list, right? So here I used to give a function name earlier. If I do not use my lambda functions, what I would do, I would create a function called ease even, which will return true. See what I would do? I would create a function called ease even, right? What it will do if x percentage two equals to zero, right? Return true, else return false. Right? This is how my function would have been defined. And here what I would have written. I would have written ease even if I did not use lambda functions. This is how it would have worked out, right? But here I'm using a lambda function, and this lambda function has no name. Look at this, look at this. Very important. This whole thing has no name. I'm just giving the function definition here. No name, it has no name. It's an anonymous function. So it's also an anonymous function. Right? Now let's see how I've defined it. I'm saying that my input, my argument is x and what you should return, you should return whether x percentile two or x mod two equals to zero. What does this mean? It means this will return true if x is even. What does x e even mean? Which means it's exactly divisible by two, right? So I have defined this whole thing, this whole thing I have defined, in literally this short sentence here. I'm saying my input argument is x, and what you have to return is true or false, and return true. This statement is true if x is exactly divisible by two, which means it's an even number, or else it will return false. Whatever logic I've written here, I'm writing it in single line, and this function has no name. These are called anonymous functions using lambda. These are extremely useful when you have to write short snippets of functions. These are extremely useful because this is a short snippet of function, right? Instead of, again defining a function, everything. There's a much more elegant way of doing it. Now let's see how to use it with map. You remember we took elements in a list and we computed the square of each of these elements. We did that right. Now let's see how to do it with lambda functions. Very simple. This is the only change. Earlier I used to use a function here. Now I'm saying lambda of x. This is the argument. So whatever value you give, give, return the square of the number. Just literally in this short snippet, you have the whole function defined. And look at it now to compute this exponent of each value. You just got it done in two lines of code. Actually, to be frank, it's only one line of code, right? So if you get used to using functions like map and using constructs like anonymous functions or lambda functions, code can be made very, very concise, very, very elegant. Those people who do not know Python, for those people, this code could become slightly tricky to read. But for people who know Python, this code is the pinnacle, is the best in elegance of Python coding. Okay, now let's look at the reduce example. You remember we multiplied all the elements in a list. Same thing I can do here. I have a list in the reduce function. I have just this one line here. Remember, I have two arguments. I have two arguments here, not one argument. So in lambda functions, you can have as many arguments as you want. So I have two arguments, and I'm written in x multiplied by y. And I'm using the reduce function from funk tools here. Like the previous case, we saw these examples when we learned the inbuilt functions, right? In types of functions, we learned about the inbuilt functions where we learned map, reduce and filter, exactly the same thing. Lambda functions make your code much more concise, much more elegant, and much more fun when you know python.