Difference between Yield and Return in Python Last Updated : 20 Oct, 2020 Comments Improve Suggest changes Like Article Like Report Python Yield It is generally used to convert a regular Python function into a generator. A generator is a special function in Python that returns a generator object to the caller. Since it stores the local variable states, hence overhead of memory allocation is controlled. Example: Python3 1== # Python3 code to demonstrate yield keyword # Use of yield def printresult(String) : for i in String: if i == "e": yield i # initializing string String = "GeeksforGeeks" ans = 0 print ("The number of 'e' in word is : ", end = "" ) String = String.strip() for j in printresult(String): ans = ans + 1 print (ans) Output: The number of 'e' in word is : 4 Python Return It is generally used for the end of the execution and “returns” the result to the caller statement. It can return all type of values and it returns None when there is no expression with the statement "return". Example: Python3 # A Python program to show return statement class Test: def __init__(self): self.str = "GeeksForGeeks" self.x = "Shubham Singh" # This function returns an object of Test def fun(): return Test() # Driver code to test above method t = fun() print(t.str) print(t.x) Output: GeeksForGeeks Shubham Singh Difference between Python yield and Return S.NO. YIELD RETURN 1 Yield is generally used to convert a regular Python function into a generator. Return is generally used for the end of the execution and “returns” the result to the caller statement. 2 It replace the return of a function to suspend its execution without destroying local variables. It exits from a function and handing back a value to its caller. 3 It is used when the generator returns an intermediate result to the caller. It is used when a function is ready to send a value. 4 Code written after yield statement execute in next function call. while, code written after return statement wont execute. 5 It can run multiple times. It only runs single time. 6 Yield statement function is executed from the last state from where the function get paused. Every function calls run the function from the start. Comment More infoAdvertise with us Next Article Difference between Yield and Return in Python S SHUBHAMSINGH10 Follow Improve Article Tags : Python python-basics Practice Tags : python Similar Reads Difference between return and print in Python In Python, we may use the print statements to display the final output of a code on the console, whereas the return statement returns a final value of a function execution which may be used further in the code. In this article, we will learn about Python return and print statements. Return Statement 2 min read Difference Between x = x + y and x += y in Python We often use x += y instead of x = x + y. So, are they same or different? Let's Find it here. Example 1: Python3 x = [1, 2] another_x = x y = [3] x += y print(x) print(another_x) Output: [1, 2, 3] [1, 2, 3] Example 2: Python3 x = [1, 2] another_x = x y = [3] x = x + y print(x) print(another_x) Outpu 2 min read Difference between end and sep in Python In this article we will discuss the difference between The end and sep are two parameters in Python's built-in print() function that will help to control how the output is formatted. end in PythonThe end is a parameter in Python's built-in print() function that controls what character(s) are printed 2 min read Python - Difference between := and == In this article, we will be discussing the major differences between Walrus(:=) and the Comparison operator (==):= in PythonThe := operator in Python, also known as the walrus operator or assignment expression, was introduced in Python 3.8. It enables assignment and evaluation to happen simultaneous 2 min read Difference Between '+' and 'append' in Python + Operator creates a new sequence by concatenating two existing sequences and .append() method (available for lists) modifies the existing list in place by adding one item at the end. In this article we are going to compare '+' operator and append in Python.+ Operator in Python+ operator is typicall 3 min read Like