Wednesday, 4 November 2026

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

 


Code Explanation:

1. Creating an Empty List
funcs = []

- Creates an empty list named funcs.
- This list will store multiple lambda functions.
- Each function will be added to this list using the append() method.

2. Starting the For Loop
for i in range(3):

- The range(3) function generates three values: 0, 1, and 2.
- The loop executes three times.
- During each iteration, the variable i receives the next value.
- These values will be accessed by the lambda functions.

3. Creating Lambda Functions
    funcs.append(lambda: i)

- lambda creates an anonymous function without using the def keyword.
- This lambda function does not accept any arguments.
- It simply returns the current value of i when called.
- append() stores the newly created function inside the funcs list.
Important: The lambda functions do not immediately store the values 0, 1, and 2. They refer to the same variable i, which is looked up when the function is executed.

4. Calling the First Lambda Function
print(funcs[0]())

- funcs[0] accesses the first lambda function in the list.
- The additional () executes that function.
- By this time, the loop has completed.
- The final value of i is 2.
- Therefore, the function returns 2.

5. Calling the Second Lambda Function
print(funcs[1]())

- funcs[1] accesses the second lambda function.
- It also refers to the same variable i.
- Since the loop has already finished, i is still 2.
- Therefore, this function also returns 2.

6. Calling the Third Lambda Function
print(funcs[2]())

- funcs[2] accesses the third lambda function.
- It executes and reads the current value of i.
- The value of i remains 2.
- Therefore, it also returns 2.


Final Output

Correct Output
2
2
2

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (355) Android (25) AngularJS (1) API (2) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (359) Bootcamp (20) C (78) C# (12) C++ (83) cloud (1) Course (96) Coursera (306) Cybersecurity (37) data (10) Data Analysis (48) Data Analytics (31) data management (16) Data Science (442) Data Strucures (21) Deep Learning (227) Django (17) 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 (416) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (36) Python (1404) Python Bug (1) Python Coding Challenge (1301) Python Library (29) Python Mathematics (22) Python Mistakes (51) Python Pattern Challenge (36) python quixz (1) Python Quiz (678) Python Tips (115) Questions (3) R (72) React (7) Scripting (3) security (5) Selenium Webdriver (4) Software (21) SQL (56) Udemy (24) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)