HOW TO CREATE A LAMBDA FUNCTION IN ONE LINE – 13

OVERVIEW

In this blog post you will learn,

  • What is lambda function?
  • Creating your first in line function
  • Including the conditions and loops

What is Lambda function

In python, when you create an anonymous function without defining or assigning any name to it is know as lambda function. To create a normal function we use def keyword and can put any number or arguments as well as expressions. Here, lambda keyword is used. While creating a lambda function you can add as many arguments you want but you can add only one expression.

Syntax:

You can understand this from the syntax below:

lambda arguments: expression

CREATING YOUR FIRST IN-LINE FUNCTION

Now, you must be thinking if we can add only one expression to the lambda function then how can we add multiple statements here like conditional statements, loops, etc. To make you clear about that first let’s understand how to create a function for adding two numbers and returning the output.

add = lambda x, y: x + y
print("Addition of two numbers is", add(4, 6))

Below is the output for the above code,

Addition of two numbers is 10
Output for addition of two numbers using lambda function

Here you can see, to create a program for adding two numbers we created a function in just one line with one expression.

INCLUDING THE CONDITIONS/LOOPS

The above program is just to return one output based on the values provided, but if I want to apply a condition here for finding the greatest number among three numbers then, this is how you can do it.

greatest = lambda x, y, z: x if x > y and x > z else y if y > z and y > x else z
print("Greatest numbers is", greatest(6, 7, 11))

Here is the output for the above code,

Greatest numbers is 11
Output for finding greatest number among three numbers using lambda function

Here, you can see that how you can use the in-line conditional statements to fulfil the requirement of putting multiple conditional statements and to get a decision made with lambda function.

Now, let’s understand how to use the loops with lambda function to perform the repetition process. To understand that let’s take an example to print the table of any number a user will input.

table = lambda x: [print(f"{x} X {i} = {x * i}") for i in range(1, 11)]
table(int(input("Enter the number: ")))

Below is the output for the above code for lambda function by using loop,

Enter the number: 8
8 X 1 = 8
8 X 2 = 16
8 X 3 = 24
8 X 4 = 32
8 X 5 = 40
8 X 6 = 48
8 X 7 = 56
8 X 8 = 64
8 X 9 = 72
8 X 10 = 80

Now I hope you have understood that how you can use lambda function in different ways is the most efficient way. In the similar way you can use this concept in different normal or real-time projects to make them more efficient and reliable.

For more clarification watch our video content here,

Time to wrap up now. Hope you liked our content on CREATING AN IN-LINE FUNCTION IN PYTHON. See you in our next blog, thanks for reading our blog and do leave a comment below to help us improve the content to serve you all of our experience with you. Stay tuned with us for more python contents.

Also check out our other playlist Rasa ChatbotInternet of thingsDockerPython Programming, etc.
Become a member of our social family on youtube here.

Stay tuned and Happy Learning.

Ashish Saini
Ashish Saini

I am a software developer for Python/Machine Learning. Also, I’m Rasa Hero at Rasa. Also, I’m a youtuber where I regulary uploads the videos to help this generation learn about the technology just like they are playing games.

Leave a Reply