From ff7e46573ea605013d258d4b2bd1421a14f4af85 Mon Sep 17 00:00:00 2001 From: Joe <127773439+joegeorge022@users.noreply.github.com> Date: Fri, 20 Dec 2024 16:57:36 +0530 Subject: [PATCH] Update readme.md --- Day-20/readme.md | 52 +----------------------------------------------- 1 file changed, 1 insertion(+), 51 deletions(-) diff --git a/Day-20/readme.md b/Day-20/readme.md index 67204ba..942c134 100644 --- a/Day-20/readme.md +++ b/Day-20/readme.md @@ -25,57 +25,7 @@ def greet(name): 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 +## 2. Nested Functions with Loops Combine nested functions and loops to process lists dynamically.