hitman47silent994c4@gmail.com
method("Hello ","Mr.","Python Hunter")
Note:- Make sure the indention.
because the adage says
Hello and welcome again.
If you are reading this blog then surely you are having some difficulties to understand lambda in python.
Well, no need to feel down, it happens to every beginner as i also have been through this.
Let's understand the lambda expression.
Anonymous functions can be created with lambda expression, commonly known as lambda functions or anonymous functions.
Lambda expression gives us the facility to write very short function in one expression.
Here are some examples that will help you to comprehend the lambda expression. (Just for beginner)
NOTE: Every code snippet is tested in python version 3.x
Lambda expression without any argument.
method = lambda : 1+3 # or any other expression.
print(method())
Output:- 4
If you print what type of variable is that
print(type(method))
It will print <class 'function'>
print(method())
Output:- 4
If you print what type of variable is that
print(type(method))
It will print <class 'function'>
Just like that:
def method():
return 1+3;
Lambda expression that receives simple arguments.
method=lambda arg1,arg2,so_on: print(arg1,' ',arg2,' ',so_on)
print(method('','',''))
Output:- None
Output:- None
The above code is calling the method("","","") with three blank args it will print nothing
method("Hello ","Mr.","Python Hunter")
The above code is calling the method() with three args it will print "Hello Mr. Python Hunter"
The above lambda expression
lambda arg1,arg2,so_on: print(arg1,'',arg2,'',so_on)
is simillar to:
def method(arg1,arg2,so_on):
print(arg1,'',arg2,'',so_on)
Instead of using def keyword and defining a python function, lambda comes handy to make short functions in single line.
Lambda expression that receives varargs (variable arguments).
method=lambda *a:[x for x in a]
print(method(1,2,4,5,6))
The above lambda expression to receive varargs.
Just like below:
def method(*a):
for i in a:
print(i)
method(1,2,3,4,5,6)
Note:- Make sure the indention.
Now you see the difference, it took 3 lines to define a function but with lambda expression it took only one line.
This approach will come handy when you will be expert in python and want to save time while writing big codes.
Lambda expression that receives kargs (keyword arguments).
method=lambda **Karg:[Karg[x] for x in Karg]
print(type(method))
print(method(arg1="q",arg2="w",arg3="e"))
Just like below:
def method(**a):
for i in a:
print(a[i],i)
Using if else conditions in lambda expression.
The below code will prompt the user to enter a number and test it whether it is ever or odd.
Note below example works fine in python version 2.7
number = lambda T: str(T)+" is even" if T%2==0 else str(T)+" is odd"
x=input("Enter Number to check whether it is ever or odd: ")
print(number(x))
otherwise we have to write the following code:
def number(T):
if(T%2==0):
print(str(T)+ " is even")
else:
print(str(T)+ " is odd")
x=input()
number(x)
Limitation:
- You can not make any comment in lambda expression.
- You can not declare and assign value to any variable in lambda expression.
- You can not declare return any variable in lambda expression.
- You can not declare multiple statements in lambda expression.
i'm also a beginner and exploring python, and its my right to share the knowledge and i'm doing my best.
And please carry out some experiments on lambda expression to comprehend it to large extent.
And if you find anything peculiar, please drop an email to hitman47silent994c4@gmail.com because the adage says
Knowledge multiplies when shared. ☺
For beginner this post is helpful to understand the lambda expression, lambda expression is not a BIG GUN or huge topic it self.
Thanks for the tutorial...
ReplyDelete