Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] What’s the Difference Between return and break in Python?

#1
What’s the Difference Between return and break in Python?

5/5 – (1 vote)

? Question: What is the difference between return and break? 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 return and break.

Comparison



Both return and break are keywords in Python.

  • The keyword return ends a function and passes a value to the caller.
  • The keyword break ends a loop immediately without doing anything else. It can be used within or outside a function.

return break
Used to end a function Used to end a for or while loop
Passes an optional value to the caller of the function (e.g., 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 return and break. In both cases, you can use them to end a loop inside a function and return to the outside.

Here’s the variant using return:

def f(): for i in range(10): print(i) if i>3: return f()

And here’s the variant using break:

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:

0
1
2
3
4

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 break and return. It uses the keyword break to end the loop as soon as the loop variable i is greater than 3.

So the line print(i) is never executed after variable i reaches the value 4—the loop ends.

But the function doesn’t end because break only ends the loop and not the function. That’s why the statement print('hi') is still executed, and the return value of the function is 42 (which we also print in the final line).

def f(): for i in range(10): if i>3: break print(i) print('hi') return 42 print(f())

Output:

0
1
2
3
hi
42

Summary


The keyword return is different and more powerful than the keyword break 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.

  • You use the keyword return to give back a value to the caller of the function or terminate the whole function.
  • You use the keyword break to immediately stop a for or while loop.

? 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 return instead of break 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! ?

Recommended Video


YouTube Video



https://www.sickgaming.net/blog/2022/10/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Break Things and Be on the Right Side of Change in the Exponential World xSicKxBot 0 1,239 03-22-2023, 12:21 PM
Last Post: xSicKxBot
  [Tut] What ChatGPT Thinks About The Matrix – Do This to Break Free! xSicKxBot 0 1,320 02-05-2023, 03:30 PM
Last Post: xSicKxBot
  [Tut] How to Return a File From a Function in Python? xSicKxBot 0 1,333 10-21-2022, 09:47 AM
Last Post: xSicKxBot
  [Tut] Python – Return NumPy Array From Function xSicKxBot 0 1,191 10-16-2022, 03:49 AM
Last Post: xSicKxBot
  [Tut] Python Return String From Function xSicKxBot 0 1,204 10-14-2022, 01:13 PM
Last Post: xSicKxBot
  [Tut] Return Keyword in Python – A Simple Illustrated Guide xSicKxBot 0 1,262 03-21-2022, 11:16 AM
Last Post: xSicKxBot
  [Tut] How to Sort and Return a Python List in One Line? xSicKxBot 0 1,245 09-09-2020, 04:08 AM
Last Post: xSicKxBot
  [Tut] How to Sort and Return a Python List in One Line? xSicKxBot 0 1,395 09-02-2020, 03:16 PM
Last Post: xSicKxBot
  [Tut] Python One Line Return if xSicKxBot 0 1,317 08-01-2020, 04:08 PM
Last Post: xSicKxBot
  [Tut] List Difference | The Most Pythonic Way xSicKxBot 0 1,398 06-27-2020, 11:51 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016