Day #52 - Lambda functions in Python

Day #52 - Lambda functions in Python

ยท

3 min read

Introduction

Welcome to my 52nd blog post on the Python journey. On day 52, I learned about a way to create anonymous functions. These functions don't have the name, they are just expressions. Anonymous functions are called as LAMBDA functions. Now Let's dive into more details and understand Lambda functions in Python.

So let's get started......

Lambda Functions in Python

  • It is an anonymous function without a name.

  • It is defined using the lambda keyword and has the below syntax:

lambda arguments: expression
  • Lambda functions are used in a situation where the small function is required to be written for a short period.

  • Commonly used as arguments to higher-order functions, such as map, filter, and reduce.

Example -

# Function to double the input
def dble(x):
  return x * 2

# Lambda function to double the input
doble = lambda x: x * 2

print(dble(8))
print(doble(10))
16
20

The above lambda function has the same functionality as the double function defined earlier. However, the lambda function is anonymous, as it does not have a name.

Multiple arguments - Lambda functions

Lambda functions can have multiple arguments, just like regular functions.

Example -

# Function to calculate the product of two numbers
def multiply(x, y):
    return x * y

# Lambda function to calculate the product of two numbers
lambda x, y: x * y

Lambda function with function arguments

Lambda functions can also include multiple statements, but they are limited to a single expression.

Example:

# Lambda function to calculate the product of two numbers,
# with additional print statement
lambda x, y: print(f'{x} * {y} = {x * y}')

In the above example, the lambda function includes a print statement, but it is still limited to a single expression.

More Example

def appl(fx, value):
  return 6 + fx(value)

double = lambda x: x * 2
cube = lambda x: x * x * x
avg = lambda x, y, z: (x + y + z) / 3

print(double(5))
print(cube(5))
print(avg(3, 5, 10))
print(appl(cube, 3)) #cube using normal methods
print(appl(lambda x: x*x*x, 3))  #cube using lambda function
print(appl(lambda x: x * x , 2))

Output

10
125
6.0
33
33
10

Resources Used

You can watch the video of Day#52 by clicking on the below link ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

Conclusion

Thanks, guys for going through this blog post. On day 52, I learned about creating an anonymous function i.e function with no name using ' LAMBDA ' keyword. They are more often used in a situation where small function is required for short time.

Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #52

See you in the next one.....


About Me

Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.

I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.

So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.


ย