Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 26, 2022 12:59 pm GMT

WHAT IS LAMBDA IN PYTHON

INTRODUCTION

Welcome again readers and Mery Christmas to you all. Today, let discuss about lambda in python. Python is a popular, high-level programming language known for its simplicity, readability, and flexibility, it also has a lot of built in functions or tools to help programmers and one of this built in tool or function is lambda. Now let start with the practical aspect

WHAT IS LAMBDA

A lambda function is small anonymous function. A lambda function can take any number of argument , but can only have one expression. Lambda functions are used to perform operations on data, often as a part of a larger program.

SYNTAX

lambda argument : expression

EXAMPLE

x = lambda a : a + 10print(x(5)) ## output: 15 

In the above example lambda takes in an argument and expression, in this case a is set to be argument with an initial value set to be 5 and expression is a + 10. You might be a little confuse if you are new to python but don't worry let break it down to it smallest detail.

BREAK DOWN

Firstly we know lambda takes in an argument and expressionNow, Let: argument = a (and the value of a = 5 )expression = a + 10 (but we know a is already 5, So it simply means 5 + 10 which is 15)

Example Two
Lambda functions are often used in combination with other built-in functions such as map, filter, and reduce.

numbers = [1, 2, 3, 4]doubled = list(map(lambda x : x * 2, numbers))print(doubled)  # Output: [2, 4, 6, 8]

Break Down
From our previous example, we know our argument here will be x and x is set to numbers ([1, 2, 3, 4]), in this case lambda map through each argument and multiply(*) it by the expression 2 which will give us an Output of [2, 4, 6, 8].

WHY DO YOU NEED LAMBDA FUNCTIONS?

The power of lambda is better shown when you use them as anonymous function inside another function.
Let say you have a function definition that takes one argument, and that argument will be multiplied with an unknown number.

Example

def lamfun(n):    return lambda a : a * ndoublenum = lamfun(2)print(doublenum(10))- # output: 20

Break Down
In the above example, we have a function called lamfun which takes in a argument n, now in the lamfun we have lambda a as the argument and a * n as the expression. The value is then return to the lamfun, outside the lamfun we have doublenum = lamfun(2) which means that value of n in lumfun is 2.

Now since we know n = 2 we can work with our lambda function,

  • argument = a

  • expression = a * n (where n is 2 ), so a * 2 is our new expression

To get the value of a let look at variable doublenum in our print statement doublenum take in 10 as an argument. This value 10 is then set to be the argument for a. Finally we have ;

  • argument = a = 10

  • expression = 10 * 2
    which gives us an output of 20.

There you have it Home Boy, lambda in python, I hope you learnt something useful today. See you later bye.


Original Link: https://dev.to/codewithkevin/what-is-lambda-in-python-2gfn

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To