- Home
- About us
- Contact us
- Sitemap
- Disclaimer
- Privacy Policy
- Terms and Condition
-
Settings
- Dark mode
Lambda Expressions in python for beginners
Lambda Expressions
One of Python's most useful (and for beginners, confusing) tools is the lambda expression. lambda expressions allow us to create "anonymous" functions. This basically means we can quickly make ad-hoc functions without needing to properly define a function using def.
Function objects returned by running lambda expressions work exactly the same as those created and assigned by defs. There is a key difference that makes lambda useful in specialized roles:
lambda's body is a single expression, not a block of statements.
- The lambda's body is similar to what we would put in a def body's return statement. We simply type the result as an expression instead of explicitly returning it. Because it is limited to an expression, a lambda is less general than a def. We can only squeeze design, to limit program nesting. lambda is designed for coding simple functions, and def handle the larger tasks.
def square(num):
result = num**2
return result
print(square(2))
OUTPUT
4
We could simplify it:
def square(num):
return num**2
print(square(2))
OUTPUT
4
We could actually even write this all on one line.
def square(num): return num**2
print(square(2))
OUTPUT
4
This is the form of a function that a lambda expression intends to replicate. A lambda expression can then be written as:
lambda num: num ** 2
OUTPUT
<function __main__.<lambda>>
# You wouldn't usually assign a name to a lambda expression, this is just for demonstration!
square = lambda num: num ** 2
OUTPUT
print(square(2))
So why would use this? Many function calls need a function passed in, such as map and filter. Often you only need to use the function you are passing in once, so instead of formally defining it, you just use the lambda expression. Let's repeat some of the examples from above with a lambda expression
print(list(map(lambda num: num ** 2, my_nums)))
OUTPUT
[1, 4, 9, 16, 25]
print(list(filter(lambda n: n % 2 == 0,nums)))
OUTPUT
[0, 2, 4, 6, 8, 10]
Here are a few more examples, keep in mind the more complex a function is, the harder it is to translate into a lambda expression, meaning sometimes it's just easier (and often the only way) to create the def keyword function.
Lambda expression for grabbing the first character of a string:
lambda s: s[0]
OUTPUT
<function __main__.<lambda>>
Lambda expression for reversing a string:
lambda s: s[::-1]
OUTPUT
<function main__.<lambda>>
You can even pass in multiple arguments into a lambda expression. Again, keep in mind that not every function can be translated into a lambda expression.
lambda x,y : x + y
OUTPUT
<function main__.<lambda>>
You will find yourself using lambda expressions often with certain non-built-in libraries, for example, the pandas library for data analysis works very well with lambda expressions.
Post a Comment
Post a Comment
if you have any doubts, please let me know