Python Tutorial

Thursday, June 13, 2013

lambda keyword

Python supports an interesting syntex creation of anonymous functions at runtime, using a construct called "lambda" .

f = lambda x: 2*x + 5
print f(3)
print f(5)

f = lambda x : x%2 and "Odd" or "Even"
print "5 is",f(5)
print "10 is",f(10)

Output:
11
15
5 is Odd
10 is Even