Vidyalelo
Python · Q22

Generators and Iterators

Programming · Python · question 22

Q22

What is a coroutine in Python?

A.
A specialized class for iteration
B.
A generator with two yield statements
C.
A function that can yield control and later resume
Answer
D.
A function that only returns values

Answer: Option C

Solution

Answer: Option C
Solution:
The correct answer is Option C: A function that can yield control and later resume.
A coroutine in Python is a function that can yield control back to the caller and later resume execution from the point it left off. Unlike regular functions, which execute to completion and return a single value, coroutines can yield values using the yield statement, allowing for cooperative multitasking and asynchronous programming. Coroutines are commonly used in asynchronous frameworks like asyncio to handle concurrent execution and manage asynchronous tasks efficiently.