Yield does not work, but return does in Python why?
Yield does not work, but return does in Python why?
685
11-Apr-2023
Aryan Kumar
17-Apr-2023The yield keyword and the return keyword serve different purposes in Python, and they are used in different contexts.
if you want to create a generator that produces a sequence of values, you need to use the yield keyword. If you use the return keyword instead, the function will return the value and terminate, and you will not be able to resume its execution to produce the remaining values.
Krishnapriya Rajeev
12-Apr-2023The yield keyword is similar to the return statement in Python, where both return some sort of object or value to where the function was called.
The key differences between the both of them are: