|
@@ -71,7 +71,7 @@ Notice that the single statement is used in-place and not as a separate block.
|
|
|
|
|
|
## Lambda Forms
|
|
|
|
|
|
-A `lambda` statement is used to create new function objects. Essentially, the `lambda` takes a parameter followed by a single expression only which becomes the body of the function and the value of this expression is returned by the new function.
|
|
|
+A `lambda` statement is used to create new function objects. Essentially, the `lambda` takes a parameter followed by a single expression. Lambda becomes the body of the function. The value of this expression is returned by the new function.
|
|
|
|
|
|
Example (save as `more_lambda.py`):
|
|
|
|
|
@@ -83,7 +83,7 @@ Output:
|
|
|
|
|
|
**How It Works**
|
|
|
|
|
|
-Notice that the `sort` method of a `list` can take a `key` parameter which determines how the list is sorted (usually we know only about ascending or descending order). In our case, we want to do a custom sort, and for that we need to write a function but instead of writing a separate `def` block for a function that will get used in only this one place, we use a lambda expression to create a new function.
|
|
|
+Notice that the `sort` method of a `list` can take a `key` parameter which determines how the list is sorted (usually we know only about ascending or descending order). In our case, we want to do a custom sort, and for that we need to write a function. Instead of writing a separate `def` block for a function that will get used in only this one place, we use a lambda expression to create a new function.
|
|
|
|
|
|
## List Comprehension
|
|
|
|