Miscellaneous fixes (#31587)
* Miscellaneous fixes Fix some errors in document, and changed some sentence structures. * Restored title keyword * Added missing word
This commit is contained in:
committed by
The Coding Aviator
parent
1a20fbff6e
commit
177a014fdc
@ -5,7 +5,7 @@ title: Lambda Expressions
|
||||
|
||||
Lambda Expressions are used when an operation only has to be performed once, meaning that there is no need for defining a function as it will not be used again. Lambda expressions also known as anonymous functions, as they are not named (defined).
|
||||
|
||||
Lambda functions can contain only one expression, so they are not best suited for functions with control-flow statements.
|
||||
Lambda functions can contain only one expression, so they are not well suited for functions with control-flow statements.
|
||||
|
||||
#### Syntax of Lambda Function
|
||||
`lambda arguments: expression`
|
||||
@ -21,11 +21,11 @@ print(square(3)) # Output: 9
|
||||
# Traditional function to calculate square of a number
|
||||
def square1(num):
|
||||
return num ** 2
|
||||
print(square(5)) # Output: 25
|
||||
print(square1(5)) # Output: 25
|
||||
|
||||
```
|
||||
In the above lambda example `lambda x: x ** 2` yields an anonymous function object which can be associated with any name.
|
||||
So, we associated the function object with `square` and hence from now on we can call the `square` object like any traditional function. e.g. `square(10)`
|
||||
So, we associated the function object with `square` and hence from now on we can call the object with `square` like any traditional function. e.g. `square(10)`
|
||||
|
||||
## Examples
|
||||
|
||||
@ -57,7 +57,7 @@ print(res) # Output: 8
|
||||
|
||||
### Use-case
|
||||
|
||||
Let's say you want to filter out odd numbers from a `list`. You could use a `for` loop:
|
||||
Say you want to filter out odd numbers from a `list`. You could use a `for` loop:
|
||||
|
||||
```python
|
||||
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
@ -77,7 +77,7 @@ You could write this as a one-liner with list-comprehensions
|
||||
filtered = [x for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] if x % 2 != 0]
|
||||
```
|
||||
|
||||
But you might be tempted to use the built-in `filter` function. Why? The first example is a bit to verbose, the one-liner can be harder to understand, where as `filter` offers the best of both words. What is more, the built-in functions are usually faster.
|
||||
However, another option is to use the built-in `filter` function. Why? The first example is a bit to verbose, while the one-liner can be harder to understand. `filter` offers the best of both words, and the built-in functions are usually faster.
|
||||
|
||||
```python
|
||||
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
@ -87,10 +87,10 @@ filtered = filter(lambda x: x % 2 != 0, my_list)
|
||||
list(filtered)
|
||||
# [1, 3, 5, 7, 9]
|
||||
```
|
||||
NOTE: in Python 3 built in function return generator objects, so you have to call `list`, while in Python 2 they return a `list`, `tuple`or `string`.
|
||||
NOTE: In Python 3 built in functions return generator objects, so you have to call `list`, while in Python 2 they return a `list`, `tuple`or `string`.
|
||||
|
||||
What happened? You told `filter` to take each element in `my_list` and apply the lambda expressions. The values that return `False` are filtered out.
|
||||
|
||||
#### More Information:
|
||||
- [Official Doc](https://docs.python.org/3/reference/expressions.html#lambda)
|
||||
- [Further Read](https://dbader.org/blog/python-lambda-functions)
|
||||
- [Further Reading](https://dbader.org/blog/python-lambda-functions)
|
||||
|
Reference in New Issue
Block a user