Warning: mysqli_connect(): (HY000/1040): Too many connections in /var/www/webnuz/inc/base_connector.php on line 2 Failed to connect to MySQL: Too many connections Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /var/www/webnuz/inc/func_sysvars.php on line 5 Lambda expressions in python - by Dev To
Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 29, 2021 04:11 pm GMT

Lambda expressions in python

Lambda expressions in python are one-time anonymous functions which we don't need more than once.

Consider the following example where we use a function to multiply a list of numbers with the mathematical constant pi:

Alt Text

Output:

[3.14, 6.28, 9.42]
Enter fullscreen mode Exit fullscreen mode

What if I say that we could do all of this in one line.
Well, it turns out we can do it.
We can convert the above example into a single line of code using Lambda expressions.

Alt Text

Output:

[3.14, 6.28, 9.42]
Enter fullscreen mode Exit fullscreen mode

Lambda function

Syntax: lambda arguments : expression

  • A lambda function can take any number of arguments, but can only have one expression:

Alt Text

Output:

94
Enter fullscreen mode Exit fullscreen mode

Why do we need to use Lambda functions?

  • Well, it contributes to the code by reducing the number of lines of functions which we may use only once.
  • To take it to the next level, we can return an anonymous function inside another example:

Alt Text

Output:

62.800000000000004119.3200000000000131.400000000000002
Enter fullscreen mode Exit fullscreen mode

As you can see, we got to use the same function to calculate different values.
You get to create a function definition that takes one argument, and that argument will be multiplied with an unknown number (which you can specify in the future).

That's the power of lambda expressions.

Code along and learn more...


Original Link: https://dev.to/aswin2001barath/lambda-expressions-in-python-5ffg

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To