• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Break Out of Function in Python with return Statement

Break Out of Function in Python with return Statement

February 22, 2022 Leave a Comment

To break out of a function in Python, we can use the return statement. The Python return statement can be very useful for controlling the flow of data in our Python code.

def doStuff():
    print("Here before the return")
    return
    print("This won't print")

doStuff()

#Output:
Here before the return

When working with functions in Python, it can be useful to need to break out of a function early based on various conditions.

Functions terminate when we return a value back, and so to break out of a function in Python, we can use the return statement. In this case, we will return nothing.

Below is an example of how to get out of a function early with a return statement.

def doStuff():
    print("Here before the return")
    return
    print("This won't print")

doStuff()

#Output:
Here before the return

As you can see, the second print statement doesn’t print because we broke out of the function early.

How to Use the Python break Statement to Break Out of For Loop

Another useful control flow statement in Python is the break statement. We can use break statements to break out of loops in our Python code.

To break out of a loop based on a condition, you can just put “break” in an if statement.

Below is an example of how to break out of a for loop in Python with the Python break statement.

def printLetters(string):
    for i in range(0,len(string)):
        if i == 2:
            break
        print(string[i])

printLetters("Word")

#Output:
W
o

Hopefully this article has been useful for you to learn how to break out of functions in Python with a return statement.

Other Articles You'll Also Like:

  • 1.  Difference Between // and / When Dividing Numbers in Python
  • 2.  Using Python to Remove Apostrophe From String
  • 3.  Selenium minimize_window() Function to Minimize Window in Python
  • 4.  How to Shutdown Computer with Python
  • 5.  Using Python to Get Domain from URL
  • 6.  Get Username in Python using os module
  • 7.  Length of Tuple Python – Find Tuple Length with Python len() Function
  • 8.  Python isfinite() Function – Check if Number is Finite with math.isfinite()
  • 9.  How to Filter Lists in Python
  • 10.  How to Hide Turtle in Python with hideturtle() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy