-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment_24.py
More file actions
43 lines (29 loc) · 3.99 KB
/
Copy pathAssignment_24.py
File metadata and controls
43 lines (29 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
1. **What is the relationship between `def` statements and lambda expressions?**
`def` statements are used to define named functions, while lambda expressions are used to define anonymous functions (functions without a name). The main difference is that `def` statements can have multiple statements and can include a `return` statement, while lambda expressions are limited to a single expression and do not have a `return` statement (the expression itself is the return value).
2. **What is the benefit of lambda?**
The main benefit of lambda expressions is their conciseness. They allow you to define simple, one-line functions without the need for a full `def` statement. This can be particularly useful when you need a function as an argument to another function, such as with `map()`, `filter()`, or `reduce()`.
3. **Compare and contrast `map()`, `filter()`, and `reduce()`:**
- `map(function, iterable)`: Applies the given function to each element of the iterable and returns an iterator with the transformed elements.
- `filter(function, iterable)`: Applies the given function to each element of the iterable and returns an iterator with only the elements for which the function returned `True`.
- `reduce(function, iterable, [initial_value])`: Applies the given function of two arguments cumulatively to the elements of the iterable, from left to right, to reduce the iterable to a single value. The optional `initial_value` is used as the initial value for the reduction.
The main differences are that `map()` transforms each element, `filter()` selects elements based on a condition, and `reduce()` combines the elements into a single value.
4. **What are function annotations, and how are they used?**
Function annotations are a way to associate arbitrary metadata with the parameters and return value of a function. They are defined using the `:` syntax, like this: `def my_function(x: int, y: str) -> float:`. The annotations can be accessed at runtime using the `__annotations__` attribute of the function. They are primarily used for type hints, but can also be used for other purposes, such as documenting the function's purpose or providing additional context.
5. **What are recursive functions, and how are they used?**
Recursive functions are functions that call themselves to solve a problem. They are useful for solving problems that can be broken down into smaller, similar subproblems. A classic example is the factorial function, which can be defined recursively as `factorial(n) = n * factorial(n-1)`. Recursive functions continue to call themselves until they reach a base case, at which point they start returning the results back up the call stack.
6. **What are some general design guidelines for coding functions?**
Some general design guidelines for coding functions include:
- Keep functions small and focused on a single task.
- Use descriptive and meaningful function names.
- Use default parameter values to make functions more flexible.
- Avoid side effects and mutating global state.
- Handle errors and edge cases gracefully.
- Write docstrings to document the function's purpose and behavior.
- Follow the "Don't Repeat Yourself" (DRY) principle.
7. **Name three or more ways that functions can communicate results to a caller.**
Functions can communicate results to a caller in the following ways:
- **Return values**: The most common way is to use the `return` statement to return one or more values from the function.
- **Output parameters**: Functions can also modify mutable arguments passed to them, effectively using the arguments as "output parameters" to communicate results.
- **Exceptions**: Functions can raise exceptions to signal that an error or unexpected condition has occurred.
- **Side effects**: Functions can modify global state or external resources, which can be observed by the caller.
- **Logging or printing**: Functions can communicate results by writing to log files or printing to the console.