Lambda functions in python programming language are expression used to implement functions. The best part of lambda functions is that they allows us to write functions very elegantly.

Consider the following function f(x):

This function can be elegantly written in the form of lambda function as follows:

from math import sin, pi

f = lambda x: sin(x) if 0 <= x <= pi else 0

The result of using this function is as follows:

Out: 0.9092974268256817

Out: 0

Consider another popular mathematical function, the unit step function:

We can write this step function as lambda function as follows:

u = lambda t: 1 if t >= 0 else 0

The result of using this function are as follows:

Out: 1

Out: 0

Out: 1

So this python programming tutorial showed you how you can implement any function as lambda function. We could have implemented the same functions above using the python def statement. The Implementing bounded functions in python blog post shows how to implement these functions using the def statement instead of lambda functions.


Post a Comment