While programming, we can face several situations where we need to use the same mathematical statement again and again. In such cases, using the same statement multiple times decreases the readability of our source code. In this article, we will discuss how we can use the lambda function in place of repetitive statements to remove such redundancy in our code in python.
What Is A Function In Python?
A function in python is a group of statements that are used to perform an atomic task. A function may accept zero or more arguments and may also return a value or an object.
For instance, The square() function defined below takes a number as an input argument and returns its square as the output value.
def square(n):
return n ** 2
You may observe that we have defined the function using the def statement. On the contrary, A lambda function is defined using the lambda statement.
What Is A Lambda Function In Python?
A lambda function is similar to a function in python. But it has some restrictions.
A function in python can have multiple statements, while loop, if-else statement, and other programming constructs to perform any task. On the other hand, a lambda function can have only one statement.
We define a lambda function using the lambda keyword in python. The syntax for declaring a lambda function is as follows.
myFunction = lambda [arguments]: expression
Here,
- “myFunction” is the name of the lambda function that will be created using this statement.
- “lambda” is the keyword used for defining the lambda function.
- “Arguments” are the parameters of the lambda function. Here, we have kept “arguments” in a square bracket [] as a lambda function may or may not have input arguments.
- “expression” is generally a mathematical expression or a function call like print().
How To Use A Lambda Function?
In python, we can use lambda functions to replace single-line mathematical statements. For example, we can create a lambda function that takes a number and calculates its square as follows.
square = lambda n: n ** 2
print("Square of 4 is: ", square(4))
Output:
Square of 4 is: 16
Another way to use a lambda function is to reduce the redundancy in code. For instance, suppose that you need to calculate the square of four numbers and print their squares one by one as follows.
def square(n):
return n ** 2
print("Square of {} is:{} ".format(4, square(4)))
print("Square of {} is:{} ".format(3, square(3)))
print("Square of {} is:{} ".format(5, square(5)))
print("Square of {} is:{}".format(10, square(10)))
Output:
Square of 4 is:16
Square of 3 is:9
Square of 5 is:25
Square of 10 is:100
Here, you can observe that the print statements are causing the code to be redundant. In such cases, we can create a lambda function that takes the input arguments that are being passed to the format() method in the print function. Then, we can use this lambda function to remove the redundancy in the code as follows.
def square(n):
return n ** 2
print_square = lambda x, y: print("Square of {} is:{} ".format(x, y))
print_square(4, square(4))
print_square(3, square(3))
print_square(5, square(5))
print_square(10, square(10))
Output:
Square of 4 is:16
Square of 3 is:9
Square of 5 is:25
Square of 10 is:100
Conclusion
In this article, we have discussed lambda functions in python. We also looked at some situations where we can use a lambda function in python. I would suggest you read this article on closures in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.