Python Program on Lambda Function
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
In this article, we will explore a Python program that utilizes lambda functions to perform basic mathematical operations. Lambda functions, also known as anonymous functions, provide a concise way to define small, one-line functions without the need for a formal function definition. This program demonstrates the use of lambda functions for operations such as squaring, addition, subtraction, multiplication, division, and finding the greater of two numbers.
Prerequisites
- Familiarity with lambda functions.
- Understanding of basic mathematical operations.
- Knowledge of taking user input in Python.
Topic Explanation
This program defines five lambda functions, each tailored for basic mathematical operations: squaring, addition, subtraction, multiplication, and integer division. It collects user input for two numbers and employs these lambda functions to perform the specified mathematical computations, subsequently displaying the results.
By encapsulating these fundamental arithmetic operations within lambda functions, the program provides a practical illustration of their flexibility in tackling mathematical tasks. Users receive hands-on exposure to the application of lambda functions, enhancing their proficiency in utilizing these tools for efficient calculations within
Code:
# Define lambda functions for basic operations
square = lambda x: x * x # Lambda function to calculate the square of a number
add = lambda a, b: a + b # Lambda function to add two numbers
subtract = lambda a, b: a - b # Lambda function to subtract the second number from the first
multiply = lambda a, b: a * b # Lambda function to multiply two numbers
divide = lambda a, b: a // b # Lambda function to integer divide the first number by the second
greater = lambda a, b: a if a > b else b # Lambda function to find the greater of two numbers
# Input
m = int(input("Enter first number: ")) # User input for the first number
n = int(input("Enter second number: ")) # User input for the second number
# Calculate and output
print("Square of the first number is:", square(m)) # Output the square of the first number
print("Sum of the two numbers is:", add(m, n)) # Output the sum of the two numbers
print("Difference between the two numbers is:", subtract(m, n)) # Output the difference between the two numbers
print("Product of the two numbers is:", multiply(m, n)) # Output the product of the two numbers
# Check and output greater number
print("Greater number is:", greater(m, n)) # Output the greater of the two numbersOutput:
Enter first number: 5
Enter second number: 6
Square of the first number is: 25
Sum of the two numbers is: 11
Difference between the two numbers is: -1
The product of the two numbers is: 30
Greater number is: 6
Code Explanation:
- Defines 6 lambda functions for basic math operations like square, add, subtract, multiply, divide and finding greater of two numbers
- square lambda function takes one argument x and returns x*x
- add lambda function takes two arguments a and b and returns their sum
- subtract lambda function takes two arguments a and b and returns their difference
- multiply lambda function takes two arguments a and b and returns their product
- divide lambda function takes two arguments a and b and returns their integer quotient
- greater lambda function takes two arguments a and b, compares them and returns the greater of the two
- Takes two integer inputs m and n from user
- Calls the defined lambda functions by passing m and n and prints the results
- Calculates and prints – square of m, sum of m and n, difference between m and n, product of m and n and greater of m and n
Summary
In summary, this Python program effectively utilizes concise lambda functions to execute a variety of fundamental mathematical operations, such as squaring, addition, subtraction, multiplication, division, and determining the larger of two numbers. Its user-friendly design facilitates easy interaction by collecting user input. This makes it a versatile and practical tool for performing elementary arithmetic operations in Python. Whether for educational purposes or quick calculations, this program provides a convenient and efficient solution for users seeking to perform basic math computations with ease.
Did we exceed your expectations?
If Yes, share your valuable feedback on Google

