0% found this document useful (0 votes)
5 views15 pages

Python Lambda function

The document provides an overview of lambda functions in Python, highlighting their definition, syntax, and differences from regular functions. It includes examples of how to use lambda functions for various applications, such as mathematical operations and conditional logic. Additionally, it presents activities for practicing lambda functions, like calculating the area of a circle.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views15 pages

Python Lambda function

The document provides an overview of lambda functions in Python, highlighting their definition, syntax, and differences from regular functions. It includes examples of how to use lambda functions for various applications, such as mathematical operations and conditional logic. Additionally, it presents activities for practicing lambda functions, like calculating the area of a circle.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Dr.

Madhuri Mahawar

Department of Mechatonics Engineering

Sanjivani College of Engineering, Kopargaon


Example

Create a function to find the square of a number


def square(x):
return x * x

result = square(5)

print(result)
Lambda Functions

square = lambda x: x * x

result = square(5)
Lambda Functions: Definition

• A lambda function is a small anonymous function that can have any number of
input arguments but contains only a single expression.

• Lambda functions are commonly used when a function is required only once or
for a short operation.
Lambda Functions: Syntax

• General Syntax:

lambda arguments : expression


Lambda Functions: Understanding the Syntax

1. lambda: Keyword used to define an anonymous (unnamed) function.

2. arguments: Input parameter(s) passed to the lambda function. A lambda


function can have zero, one, or multiple arguments.

3. expression: A single expression that is evaluated automatically and whose


result is returned.
Difference Between lambda and def Keyword

Feature lambda Function Regular Function (def)


Definition Single expression with lambda. Multiple lines of code.

Name Anonymous (or named if assigned). Must have a name.

Can include multiple


Statements Single expression only.
statements.

Documentation Cannot have a docstring. Can include docstrings.

Better for reusable and


Reusability Best for short, temporary functions.
complex logic.
Lambda with List Comprehension

• Combining lambda with list comprehensions enables us to apply transformations


to data in a concise way.

numbers = [1, 2, 3, 4, 5]

result = [(lambda x: x*x)(x) for x in numbers]

print(result)
Lambda with if-else

• lambda functions can incorporate conditional logic directly, allowing us to


handle simple decision making within the function

maximum = lambda a, b: a if a > b else b

print(maximum(10, 20))
Lambda Functions: Application

• Write short, one-time functions.

• Sort data using custom criteria.

• Pass functions to map( ), filter( ), and sorted( ).

• Handle GUI events in Tkinter.

• Perform quick mathematical operations.


Lambda Functions: Example

• Write a Python program to add two numbers using a lambda function.

add = lambda a, b: a + b

print("Sum =", add(15, 25))


Lambda Functions: Example

• Write a Python program to determine whether a number is even or odd using a


lambda function.

even_odd = lambda x: "Even" if x % 2 == 0 else "Odd"

print(even_odd(17))

print(even_odd(24))
Lambda Functions: Example

• A temperature sensor records a temperature value. Write a Python program using a


lambda function to display "High Temperature" if the reading is greater than 40°C;
otherwise display "Normal Temperature".

temperature_status = lambda t: "High Temperature" if t > 40 else "Normal


Temperature"

print(temperature_status(38))

print(temperature_status(45))
Lambda Functions: Activity

• Write a Python program using a lambda function to calculate the area of a circle
for a given radius.

area = lambda r: 3.14159 * r * r

radius = 7

print("Area =", area(radius))


Thank You

You might also like