Wednesday, 21 October 2026

Python Coding challenge - Day 1286| What is the output of the following Python Code?

 


Code Explanation:

1. def outer(x): — Outer Function
def outer(x):
- Defines a function named outer().
- It accepts one parameter, x.
- When we call outer(2), the variable x starts with the value 2.
- The variable x belongs to the local scope of outer().

2. def inner(y): — Nested Function
def inner(y):
- Defines a function named inner() inside outer().
- This is called a nested function.
- It accepts one parameter, y.
- The inner function can access variables from its enclosing function.

3. nonlocal x — Modify the Enclosing Variable
nonlocal x
- The nonlocal keyword refers to the variable x from the enclosing function.
- It allows inner() to modify the existing x instead of creating a new local variable.
- The value of x will persist between calls to inner().

4. x += y — Update the Value
x += y

- Adds y to the current value of x.
- Updates the enclosing variable x.
For the first call:
f(3)
Calculation:
x = 2 + 3
x = 5


For the second call:
f(4)

Calculation:
x = 5 + 4
x = 9


5. return lambda z: x * z — Return a Lambda Function
return lambda z: x * z

- lambda creates an anonymous function.
- It accepts one parameter, z.
- It returns the product of x and z.
- The returned lambda remembers the enclosing variable x. This behavior is related to a closure.
After the first call, a stores a lambda function. After the second call, b stores another lambda function. Both reference the same enclosing variable x.

6. return inner — Return the Inner Function
return inner

- Returns the function inner itself, rather than calling it.
- This allows us to store that function in another variable.
- The enclosing scope remains available even after outer() has finished executing.

7. f = outer(2) — Create the Function
f = outer(2)
- Calls outer() with x = 2.
- The function returns inner.
- The variable f now refers to the inner function.

8. a = f(3) — First Function Call
a = f(3)
- Calls inner(y) with y = 3.
- Updates x from 2 to 5.
- Returns a lambda function, which is stored in a.
At this point, a refers to a lambda that uses the shared variable x.

9. b = f(4) — Second Function Call
b = f(4)
- Calls inner(y) with y = 4.
- The current value of x is 5.
- Updates x to 9.
- Returns another lambda function, which is stored in b.
Important: Both a and b refer to lambdas that access the same variable x. Its current value is now 9.

10. print(a(2), b(2)) — Final Output
print(a(2), b(2))
- a(2) calculates 9 * 2 = 18.
- b(2) also calculates 9 * 2 = 18.
- Both lambdas use the latest value of the shared variable x.

Output:
18 18

Books: 400 Days Python Coding Challenges with Explanation

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (353) Android (25) AngularJS (1) API (2) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (359) Bootcamp (16) C (78) C# (12) C++ (83) cloud (1) Course (96) Coursera (306) Cybersecurity (36) data (10) Data Analysis (47) Data Analytics (31) data management (16) Data Science (438) Data Strucures (20) Deep Learning (223) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (9) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Gadgets (1) Generative AI (79) Git (14) Google (55) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (411) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (35) Python (1393) Python Coding Challenge (1282) Python Library (26) Python Mathematics (22) Python Mistakes (51) Python Pattern Challenge (28) python quixz (1) Python Quiz (664) Python Tips (114) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (24) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)