Python Lambda Function without Arguments
Lambda Function without Arguments in Python
In Python, you can define a lambda function without any arguments by using an empty parameter list.
For example, consider the following lambda function.
lamda: "Hello World"
This lambda function has no arguments, and when you call this lambda function, it always returns "Hello World"
.
Therefore, a lambda function without arguments returns a specified constant value.
Examples
1. Lambda Function without arguments that always returns "Hello World"
In the following program, we define a lambda function without any arguments, and that always returns a string value of "Hello World"
. We shall assign the lambda function to a variable named greeting
. Call the lambda function with greeting()
and print the returned value.
Python Program
greeting = lambda: "Hello World"
# Call the lambda function
result = greeting()
print(result)
Output
Hello World
2. Lambda Function without arguments that always returns 3.14
In the following program, we define a lambda function without any arguments, and that always returns a floating point value of 3.14
.
We shall assign the lambda function to a variable named pi
.
Python Program
pi = lambda: 3.14
# Call the lambda function
result = pi()
print(f"PI is {result}")
Output
PI is 3.14
Summary
In this tutorial of Python Lambda Functions, we learned how to define a lambda function without arguments, and does this lambda function work, with the help of example programs.