Instagram
youtube
Facebook
Twitter

Python Lambda Function

All the languages like C, C++, Java, C# had lambda function. Just like these languages python also had a lambda function in its syntax. Python lambda functions are concise, anonymous one-liner functions. The syntax of the lambda function is concise but more restrictive than regular python function. In this tutorial, we will learn about the python lambda function.

What are Python Lambda Functions?

Lambda functions in python are linear anonymous functions, as it is cleared by the word “anonymous” this function is nameless. Python lambda function can have a number of arguments but have only a single expression.

What is the difference between def Functions and Lambda Functions?

Python Def Define Function

Python Lambda Function

It is easy to interpret these functions

It might be complex to interpret these functions
 

It includes a return statement

No need to include return statement

It can’t be nameless

It can be nameless

It is defined by def keyword

It is defined by the lambda keyword.

Syntax of Python Lambda Function

Lambda arguments: expresson

Python lambda function can have a number of arguments but have only a single expression.

Some examples of Python Lambda Functions

Program 1: use the Lambda function to find the square of any number.

a = lambda x:x*x
print(a(5))
print(a(25))

Output: 

25
625

 

Program 2: use the Lambda function to find the square root of any number.

import math
a = lambda x:math.sqrt(x)
print(a(25))
print(a(49))

Output: 

5.0
7.0

 

Program 3: use the Lambda function to uppercase a string.

a = lambda x:x.upper()
print(a("hello world"))
print(a("codersdaily"))

Output:

HELLO WORLD
CODERSDAILY

 

Program 4: use the Lambda function to find even and odd numbers.

a = lambda x: "even number!" if x%2==0 else "odd number!"
print(a(5))
print(a(88))

Output:

odd number!
even number!