Python Lambda

Python is a general-purpose and widely used programming language of recent times. However, Python provides many built-in functions to perform a bunch of specific tasks. However, we can create our own functions to perform a specific task. In Python, the lambda keyword creates an anonymous function. A function without a name is called an anonymous function. Normally, a regular function in Python is created using the def keyword. It has a name and parentheses. While the anonymous function is used with the lambda keyword. Therefore, the anonymous function is also known as the lambda function. This article explains the Python lambda function with examples.

Syntax of the lambda function

The lambda function can accept several arguments and only one expression. The expression performs the work, and the result is returned. The syntax of the lambda function is:

lambda argument(s) : expression

It is important to remember that a lambda function is only restricted to one expression. The lambda function can be implemented in Python script when we need to create the function object. Let’s implement the lambda function in our Python script.

Example 1: Calculate the sum of numbers using lambda function

Let’s create a lambda function to calculate the numbers. The lambda function takes the two numbers as an argument and returns the result.

# a program to create the lambda function
#creating the lambda function
sum_val = lambda a,b : a+b
#passing the arguments and printing the sum value
print(sum_val(5,10))

Output

The output displays the sum value of two numbers.

Let’s define a normal function to understand the difference between normal and lambda function.

#creating a normal function
def sum_val(a,b):
    return a+b
#passing the arguments and printing the sum value
print("The sum value is: ",sum_val(5,10))

Output

Both of the function returns the same value when similar arguments are passed. In the normal function, we use the def keyword and need to write the function name. After the sum expression, we put the return statement at the end to return the result value. While in the lambda function, the return statement is not included. It returns the output value after evaluating or executing the expression.

Example 2: Implementing multiple lambda functions

Let’s implement the lambda functions with a different number of the argument.

#a program to implement the many lambda functions

#a lambda function to subtract the number
a = lambda a,b,c: a-b-c

#passing the arguments and printing the difference value
print("The subtraction value is: ",a(200,10,2))

#a lambda function to multiply two numbers
b = lambda a,b:a*b
#passing the arguments and printing the difference value
print("The multiplication value is: ",b(10,5))

#a lambda function to calculate the square value
c = lambda a:a*a
#passing the arguments and printing the difference value
print("The square value is: ",c(10))

#a lambda function to calculate the cube value
c = lambda a:a*a*a
#passing the arguments and printing the difference value
print("The cube value is: ",c(10))

#a lambda function to determine the even number
c = lambda a:(a%2==0)
#passing the arguments and printing the difference value
print("The given number is even: ",c(1001))

#a lambda function to determine the leap year
c = lambda a:(a%4==0)
#passing the arguments and printing the difference value
print("The given year is leap year: ",c(2020))

Output

The output of different lambda functions is displayed.

The lambda function also returns the Boolean function when we a condition in the expression.

Using lambda function inside a normal function

A lambda function can be implemented inside a normal or regular Python function. The sum_val function takes a number as an argument and returns the lambda function. It is stored in a variable named value. The second argument is passed to the lambda function, and it returns the sum value.

#a program to implement the lambda function inside a normal function

#declaring a normal function
def sum_val(num1):
    #declaring a lambda function inside the function
    return lambda num2: num1+num2
value = sum_val(10)
print("The sum value is: ",value(200))

Output

Conclusion

An anonymous in Python is called a lambda function. An anonymous function is created when we have to use the function object. This article explains the lambda function through examples.



from Linux Hint https://ift.tt/3eWuvCV

Post a Comment

0 Comments