Monday, 2 November 2026

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

 




Code Explanation:

1. Defining a Generator Function
def process():

- Defines a function named process().
- This function contains yield statements, so Python treats it as a Generator Function.
- Unlike a normal function, a generator produces values one at a time instead of returning all values together.

2. First Yield Statement
    yield 10

- The yield keyword produces the value 10.
- The function pauses its execution at this point.
- Its current execution state is saved.
- When the generator is called again, execution resumes from where it stopped.

3. Second Yield Statement
    yield 20

- Produces the next value, which is 20.
- The generator pauses again.
- It remembers its current position and waits for the next request.

4. Return Statement
    return 30
- Ends the generator function.
- The value 30 is not yielded as a normal generator output.
- Instead, it becomes the value carried by the StopIteration exception.
- Once this statement executes, the generator is exhausted.
Important: yield produces values, whereas return terminates the generator.

5. Creating the Generator Object
g = process()

- Calls the generator function.
- Creates a generator object and stores it in g.
- The function body does not execute immediately.
- Execution starts only when we request a value using next().

6. Getting the First Value
print(next(g))
- next() starts executing the generator.
- Execution reaches yield 10.
- The generator pauses and returns 10.
- print() displays the value.
Output:
10

7. Getting the Second Value
print(next(g))
- Calls next() again on the same generator.
- Execution resumes after the first yield.
- It reaches yield 20.
- The generator pauses again and returns 20.
Output:
20


8. Handling Generator Exhaustion
print(next(g, "Finished"))

- Calls next() for the third time.
- The generator resumes after yield 20.
- It executes return 30 and terminates.
- Python raises StopIteration, carrying 30 as its value.
- However, next(g, "Finished") uses "Finished" as the default value when the generator is exhausted.
- Therefore, it prints "Finished" instead of raising an unhandled exception.
Output:
Finished


Final Output
Correct Answer
10
20
Finished

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 (1403) Python Bug (1) Python Coding Challenge (1298) Python Library (29) Python Mathematics (22) Python Mistakes (51) Python Pattern Challenge (36) python quixz (1) Python Quiz (677) Python Tips (114) 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)