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:

f(2)

Out: 0.9092974268256817

f(4)

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:

u(3)

Out: 1

u(-2)

Out: 0

u(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.

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:

f(2)

Out: 0.9092974268256817

f(4)

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:

u(3)

Out: 1

u(-2)

Out: 0

u(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.

## 0 comments:

## Post a Comment