Difference between Yield and Return in Python
When it comes to writing efficient Python code, you may often come across two common keywords – ‘yield’ and ‘return’. Although they may seem similar, they serve their own unique purpose in programming. In this article, we will discuss the difference between yield and return in Python.
What is ‘return’ in Python?
In Python, ‘return’ is a keyword that is used to end a function and return a value. It basically stops the execution of the function and returns the value to the calling function. Here is a simple example to understand ‘return’ better:
def square(num):
return num*num
result = square(5)
print(result)
In the above code, we define a function named ‘square’ that takes a number as an argument and returns the square of that number. We then call the function with the argument ‘5’ and store the result in the variable ‘result’. Finally, we print the value of ‘result’ which is 25. The ‘return’ statement is responsible for returning the value ’25’ to the calling function.
What is ‘yield’ in Python?
In Python, ‘yield’ is also a keyword that is used to define a generator function. Generator functions are used to produce a series of values over time. The ‘yield’ statement is used inside a generator function to produce the next value in the sequence. Here is a simple example to understand ‘yield’ better:
def countdown(n):
while n > 0:
yield n
n -= 1
for num in countdown(5):
print(num)
In the above code, we define a generator function named ‘countdown’ that takes a number as an argument and produces a sequence of numbers from that number down to 1. We then use a ‘for’ loop to iterate over the sequence and print each value. The ‘yield’ statement is responsible for producing each value of the sequence.
Difference between ‘yield’ and ‘return’
Now that we understand what ‘yield’ and ‘return’ are, let’s take a look at the differences between them:
1. Function Type
The main difference between ‘yield’ and ‘return’ is that ‘yield’ is used to define a generator function while ‘return’ is used to define a normal function. A generator function is a special type of function that produces a series of values over time. It uses the ‘yield’ statement to generate the next value in the sequence.
2. Control Flow
Another key difference between ‘yield’ and ‘return’ is in the control flow of the program. When a ‘return’ statement is encountered, the function ends immediately and returns the specified value to the calling function. On the other hand, when a ‘yield’ statement is encountered, the function is paused and the current value is returned. The function then continues from where it left off the next time it is called.
3. Values Returned
When a ‘return’ statement is used, the function returns a single value and ends. On the other hand, when a ‘yield’ statement is used, the function can produce an indefinite number of values over time. These values can be produced one at a time or in batches, depending on the implementation.
Example Program: Yield vs Return
Let’s take a look at an example program to see how ‘yield’ and ‘return’ work:
# Using return statement
def square_numbers(n):
result = []
for i in range(n):
result.append(i*i)
return result
print(square_numbers(5)) # Output: [0, 1, 4, 9, 16]
# Using yield statement
def square_numbers(n):
for i in range(n):
yield i*i
numbers = square_numbers(5)
print(next(numbers)) # Output: 0
print(next(numbers)) # Output: 1
print(next(numbers)) # Output: 4
print(next(numbers)) # Output: 9
print(next(numbers)) # Output: 16
In the above code, we define a function named ‘square_numbers’ that takes a number ‘n’ and generates a sequence of squares of numbers from 0 to ‘n-1’. We have implemented the function using both ‘return’ and ‘yield’ statements.
In the first implementation, we use a list to store the squares and return the list at the end of the function. We call the function with argument 5 and print the resulting list.
In the second implementation, we use ‘yield’ instead of ‘return’ to generate the sequence. We then create a generator object with the function call and use ‘next()’ to print each value in the sequence.
Conclusion
In conclusion, the main difference between ‘yield’ and ‘return’in Python is that ‘return’ is used to end the function and return a single value to the caller, while ‘yield’ is used to define a generator function that produces a sequence of values over time. ‘return’ is used in normal functions, while ‘yield’ is used in generator functions.
When a ‘return’ statement is encountered, the function exits immediately, whereas when a ‘yield’ statement is encountered, the function is paused, and the current value is returned. The function then resumes from where it was paused the next time it is called.
In summary, it is important to understand the difference between ‘yield’ and ‘return’ in Python to write efficient and correct code. Both keywords have their own unique use cases, and understanding them allows you to write code that is optimized for your specific needs.