In Python, the yield keyword is used to create a generator function. When a function includes a yield statement, it becomes a generator function, which returns an iterator object that can be iterated over with a loop.
The yield statement suspends the function's execution and sends a value back to the caller, but unlike return, the function state is saved, and the function can be resumed later from where it left off.
Here's an example to demonstrate the use of yield:
def countdown(num):
while num > 0:
yield num
num -= 1
0 Comments:
Post a Comment