10-23-2022, 01:46 AM
What’s the Difference Between return and break in Python?
5/5 – (1 vote)
Question: What is the difference between and ? When to use which?
Let’s first look at a short answer before we dive into a simple example to understand the differences and similarities between and .
Comparison
![[Image: image-190-1024x682.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-190-1024x682.png)
Both and are keywords in Python.
|
Used to end a function |
Used to end a or loop
Passes an optional value to the caller of the function (e.g.,) |
Doesn’t pass anything to the “outside”
While they serve a different purpose, i.e., ending a function vs ending a loop, there are some cases where they can be used interchangeably.
Similar Use Cases
The following use case shows why you may have confused both keywords and . In both cases, you can use them to end a loop inside a function and return to the outside.
Here’s the variant using:
And here’s the variant using:
Both code snippets do exactly the same—printing out the first 5 values 0, 1, 2, 3, and 4.
Output:
However, this is where the similarity between those two keywords ends. Let’s dive into a more common use case where they both perform different tasks in the code.
Different Use Cases
The following example uses both keywords and . It uses the keyword to end the loop as soon as the loop variable is greater than 3.
So the line is never executed after variable reaches the value 4—the loop ends.
But the function doesn’t end because only ends the loop and not the function. That’s why the statement is still executed, and the return value of the function is (which we also print in the final line).
Output:
Summary
The keyword is different and more powerful than the keyword because it allows you to specify an optional return value. But it can only be used in a function context and not outside a function.
Rule: Only if you want to exit a loop inside a function and this would also exit the whole function, you can use both keywords. In that case, I’d recommend using the keyword instead of because it gives you more degrees of freedom, i.e., specifying the return value. Plus, it is more explicit which improves the readability of the code.
Thanks for reading over the whole tutorial—if you want to keep learning, feel free to join my email academy. It’s fun!![[Image: 1f642.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png)
Recommended Video
5/5 – (1 vote)
Question: What is the difference between Code:
returnCode:
breakLet’s first look at a short answer before we dive into a simple example to understand the differences and similarities between
Code:
returnCode:
breakComparison
![[Image: image-190-1024x682.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-190-1024x682.png)
Both
Code:
returnCode:
break- The keyword ends a function and passes a value to the caller.Code:
return
- The keyword ends a loop immediately without doing anything else. It can be used within or outside a function.Code:
break
Code:
returnCode:
breakUsed to end a function |
Used to end a
Code:
forCode:
whilePasses an optional value to the caller of the function (e.g.,
Code:
return 'hello'Doesn’t pass anything to the “outside”
While they serve a different purpose, i.e., ending a function vs ending a loop, there are some cases where they can be used interchangeably.
Similar Use Cases
The following use case shows why you may have confused both keywords
Code:
returnCode:
breakHere’s the variant using
Code:
returnCode:
def f(): for i in range(10): print(i) if i>3: return f()And here’s the variant using
Code:
breakCode:
def f(): for i in range(10): print(i) if i>3: break f()Both code snippets do exactly the same—printing out the first 5 values 0, 1, 2, 3, and 4.
Output:
Code:
0
1
2
3
4However, this is where the similarity between those two keywords ends. Let’s dive into a more common use case where they both perform different tasks in the code.
Different Use Cases
The following example uses both keywords
Code:
breakCode:
returnCode:
breakCode:
iSo the line
Code:
print(i)Code:
iBut the function doesn’t end because
Code:
breakCode:
print('hi')Code:
42Code:
def f(): for i in range(10): if i>3: break print(i) print('hi') return 42 print(f())Output:
Code:
0
1
2
3
hi
42The keyword
Code:
returnCode:
break- You use the keyword to give back a value to the caller of the function or terminate the whole function.Code:
return
- You use the keyword to immediately stop aCode:
breakorCode:forloop.Code:while
Rule: Only if you want to exit a loop inside a function and this would also exit the whole function, you can use both keywords. In that case, I’d recommend using the keyword Code:
returnCode:
breakThanks for reading over the whole tutorial—if you want to keep learning, feel free to join my email academy. It’s fun!
![[Image: 1f642.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png)
Recommended Video

