-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f18eee
commit 19b11c9
Showing
1 changed file
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# Day 20: Exploring Nested Functions in Python | ||
|
||
## **Task**: Understand and implement nested functions to perform structured tasks. | ||
|
||
**Description**: | ||
Nested functions are functions defined within other functions. They allow encapsulation, improving code readability and providing closure properties. Today, you will learn: | ||
|
||
1. How to define nested functions. | ||
2. Using nested functions to structure complex logic. | ||
3. Benefits of closures in nested functions. | ||
|
||
--- | ||
|
||
## 1. Defining a Nested Function | ||
|
||
```python | ||
# Outer function | ||
def greet(name): | ||
# Inner function | ||
def message(): | ||
return "Welcome!" | ||
|
||
return f"Hello, {name}. {message()}" | ||
|
||
print(greet("Alice")) # Output: Hello, Alice. Welcome! | ||
``` | ||
|
||
## 2. Practical Example: Calculator with Nested Functions | ||
|
||
Create a calculator function with nested functions for basic arithmetic operations. | ||
|
||
```python | ||
def calculator(a, b): | ||
def add(): | ||
return a + b | ||
|
||
def subtract(): | ||
return a - b | ||
|
||
def multiply(): | ||
return a * b | ||
|
||
def divide(): | ||
return a / b if b != 0 else "Division by zero!" | ||
|
||
return { | ||
"add": add(), | ||
"subtract": subtract(), | ||
"multiply": multiply(), | ||
"divide": divide() | ||
} | ||
|
||
results = calculator(10, 5) | ||
print("Addition:", results["add"]) # Output: Addition: 15 | ||
print("Subtraction:", results["subtract"]) # Output: Subtraction: 5 | ||
print("Multiplication:", results["multiply"]) # Output: Multiplication: 50 | ||
print("Division:", results["divide"]) # Output: Division: 2.0 | ||
``` | ||
|
||
## 3. Using Closures for Data Encapsulation | ||
|
||
Closures in Python allow inner functions to retain the state of variables from their enclosing scope. | ||
|
||
```python | ||
def power(exponent): | ||
def calculate(base): | ||
return base ** exponent | ||
|
||
return calculate | ||
|
||
square = power(2) # Closure with exponent set to 2 | ||
cube = power(3) # Closure with exponent set to 3 | ||
|
||
print(square(4)) # Output: 16 | ||
print(cube(2)) # Output: 8 | ||
``` | ||
|
||
## 4. Nested Functions with Loops | ||
|
||
Combine nested functions and loops to process lists dynamically. | ||
|
||
```python | ||
def process_numbers(numbers): | ||
def square(n): | ||
return n ** 2 | ||
|
||
def cube(n): | ||
return n ** 3 | ||
|
||
results = {"squares": [], "cubes": []} | ||
for num in numbers: | ||
results["squares"].append(square(num)) | ||
results["cubes"].append(cube(num)) | ||
|
||
return results | ||
|
||
numbers = [1, 2, 3, 4] | ||
processed = process_numbers(numbers) | ||
print("Squares:", processed["squares"]) # Output: Squares: [1, 4, 9, 16] | ||
print("Cubes:", processed["cubes"]) # Output: Cubes: [1, 8, 27, 64] | ||
``` | ||
|
||
--- | ||
|
||
### Summary | ||
Today’s task introduces the concept of nested functions, demonstrating their utility for encapsulation, closures, and structured logic. Nested functions are a powerful tool for creating modular and efficient Python programs. |