About 14,100,000 results
Open links in new tab
  1. python - Break or exit out of "with" statement? - Stack Overflow

    Just wrap the expression you're going to with with fragile, and raise fragile.Break to break out at any point! print 'before condition' if condition: raise fragile.Break. print 'after condition'

  2. python - How to exit an if clause - Stack Overflow

    There are times when I'm writing code and want to put a break statement inside of an if clause, only to remember that those can only be used for loops. Lets take the following code as an example: ... if condition_a: # do something. # and then exit the outer if block. ... if condition_b: # do something. # and then exit the outer if block.

  3. How to Exit an If Statement in Python [7 different ways] - Python

    Feb 26, 2024 · Learn how to exit an if statement in Python using seven methods in detail with examples like exit() function, quit(), return and break statements, etc.

  4. Python break statement - GeeksforGeeks

    Dec 27, 2024 · The break statement in Python is used to exit or “break” out of a loop (either a for or while loop) prematurely, before the loop has iterated through all its items or reached its condition. When the break statement is executed, the program immediately exits the loop, and the control moves to the next line of code after the loop.

  5. Exiting a ‘with’ Statement in Python 3 - dnmtechs.com

    Exiting a ‘with’ statement can be achieved by using a context manager with ‘__enter__’ and ‘__exit__’ methods or by using the ‘break’ statement to prematurely exit the block. Understanding how to properly exit a ‘with’ statement ensures proper resource management and can help in writing cleaner and more efficient code.

  6. python - How to stop one or multiple for loop (s) - Stack Overflow

    In order to jump out of a loop, you need to use the break statement. n=L[0][0] m=len(A) for i in range(m): for j in range(m): if L[i][j]!=n: break; Here you have the official Python manual with the explanation about break and continue, and other flow control statements:

  7. How to Exit Loops Early With the Python Break Keyword

    Apr 16, 2025 · In Python, the break statement lets you exit a loop prematurely, transferring control to the code that follows the loop. This tutorial guides you through using break in both for and while loops. You’ll also briefly explore the continue keyword, which complements break by skipping the current loop iteration.. By the end of this tutorial, you’ll understand that:

  8. Python break - Python Tutorial

    Typically, you use the break statement with the if statement to terminate a loop when a condition is True. The following shows how to use the break statement inside a for loop: # more code here if condition: break Code language: Python (python)

  9. How To Exit A Function In Python? (7 Ways With Examples) - Python

    In this blog post, we will explore various techniques and best practices to exit functions in Python effectively. We will cover different exit strategies, such as return statements, exceptions, and other control flow mechanisms, providing comprehensive explanations and examples along the way.

  10. Break in Python – Nested For Loop Break if Condition Met Example

    May 17, 2022 · In situations where we want to stop the iteration before getting to the last item or before a given condition is met, we can use the break statement. The break statement will have its own condition – this tells it when to "break" the loop. In this article, we'll first see how to use the break statement in for and while loops.

  11. Some results have been removed