Yield is for generator functions. Return ends the function and sends back the specified value.
Generators are functions that return iterators, i.e. they 'yield' a value after each iteration of them, before proceeding to the next one. This is generally used for things where you don't need all the results returned at once (which must load them all into memory), but instead only gives back the results one at a time (which can vastly reduce memory usage).
Python documentation on generators,
And on iterators.
Here's their example of a simple generator:
def reverse(data):
# start at the last index and step back one each time.
for index in range(len(data)-1, -1, -1):
yield data[index]
for char in reverse('golf'):
print char
"""result:
f
l
o
g
"""
What this does is call each iteration of the reverse function each time the "for" loop executes in the statement
for char in reverse('golf') . This way, only a single letter is sent back each time, rather than the fully-reversed string.
This isn't a great example of generator usage, but say if you had a dataset with 1000s of numbers and you needed to perform calculations on them, but you'd only be working on one item in the set at a time anyway.
Then a generator would be useful as it would save a TON of memory by only returning each item as it's needed.
Hope that helped!