Thursday, April 18, 2024
HomeProgramming LanguagesPython Lambda Function with Examples

Python Lambda Function with Examples

-

In this article, we will discuss briefly about Python Lambda function along with an example.

A Lambda function is an anonymous function which takes arguments and returns an evaluated expression. Let us see how the syntax of Python Lambda function looks like.

Lambda Function Syntax

lambda function expression in diagram

Related Links

Before seeing examples, the following are the characteristics of a Python Lambda function which are noteworthy:

  1. A Lambda function can have any number of arguments but only one expression, which will be evaluated and returned.
  2. We are free to use lambda functions wherever function objects are required.
  3. return keyword is not used in Lambda function as by default lambas return a value.

Now let us create a Lambda function using Python.

Examples

Below is the lambda function to print a square of a given number:


# python program to print a square using lambda function
square = lambda x : x**2
print(square(3))

Let us see a Lambda function accepting multiple arguments.


# python program to find volume of a cube
volume = lambda l,b,h : l*b*h
print(volume(3,2,4))

The output is 24.

Usage of Lambdas in Built-in Functions

There are some built-in functions like map(), filter(), reduce() etc. which takes a lambda as an argument.

Let us see how filter function works.

A filter() takes two parameters as arguments – a lambda function and a list. It filters the passed list and returns the values which matches the condition set by a lambda function.

Let us see an example where we want to find out the below average marks in a list.


# python program to print below average marks
marks = [100, 76, 45, 28, 52, 17, 83]
below_average = list(filter(lambda x : x<35, marks))
print(below_average)

Output:


[28, 17]

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

LATEST POSTS

Packages in Java

Hello World! Today's blog is about packages in java. Here, we will learn about what are packages and how we can use packages along with...
00:11:41

Check Array Formation Through Concatenation | Leetcode Problem 1640

In this video, we will explain Check Array Formation Through Concatenation, Leetcode's 1640th problem in 2 different approaches along with a coding solution. Problem statement You are given...

Linked List Deletion – Linked List Tutorial Series #3

In this article, we will discuss about linked list deletion. We will see how to delete a node in a Singly linked list. This article will...

Linked List Insertion – Linked List Tutorial Series #2

Linked List Insertion, unlike array or list insertion, is not pretty straight forward. In this article, we will discuss about Singly Linked List insertion. Usually...

Follow us

1,358FansLike
10FollowersFollow
401SubscribersSubscribe

Most Popular