Report this

What is the reason for this report?

How to Break Out of Multiple Loops in Python

Updated on August 7, 2025
English
How to Break Out of Multiple Loops in Python

Introduction

Using for loops and while loops in Python allows you to automate and efficiently repeat tasks. These loops are fundamental constructs in Python that enable you to iterate over sequences, such as lists, tuples, and strings, or to execute a block of code repeatedly based on a condition.

However, there are scenarios where you need more control over the flow of your loops. For instance, you might encounter a situation where you need to exit a loop prematurely, skip the current iteration, or simply have a placeholder for future code. Python provides three powerful statements to handle these cases: break, continue, and pass.

  • The break statement allows you to exit a loop entirely when a specific condition is met, effectively stopping the loop execution.
  • The continue statement lets you skip the rest of the code inside the loop for the current iteration and move on to the next iteration.
  • The pass statement is a null operation; it is used as a placeholder in loops, functions, classes, or conditionals where code is syntactically required but you have nothing to execute.

Understanding and utilizing these statements can significantly enhance your ability to manage loop control flow, making your code more efficient and easier to read. This article provides a comprehensive guide to using Python’s break, continue, and pass statements within loops to control flow effectively. It explains the purpose and behavior of each statement with practical code examples and output demonstrations. The article also explores advanced loop control techniques, including methods for multi-level loop exits, and introduces the lesser-known else clause with loops for cleaner post-loop logic. Real-world scenarios such as data parsing, file scanning, and matrix traversal are included to show how these concepts apply in practical Python programming.

Need to deploy a Python project and have it live quickly? Check out DigitalOcean App Platform and deploy a Python project directly from GitHub in minutes.

Key Takeaways:

  • The break statement in Python allows you to exit a loop immediately when a specific condition is met, which is especially useful for terminating early during search or validation operations.
  • The continue statement skips the rest of the current iteration and moves to the next cycle of the loop, helping avoid deeply nested conditionals and improve loop clarity.
  • The pass statement is a syntactic placeholder that performs no action, commonly used when a block of code is syntactically required but the logic is yet to be implemented.
  • Since Python lacks native support for breaking out of multiple nested loops, using a flag variable to track the condition and control outer loop behavior is a practical workaround.
  • Encapsulating loop logic in a function and using return allows you to exit multiple levels of iteration cleanly and improves code modularity and maintainability.
  • Although exceptions can be used to exit nested loops, they should be reserved for truly exceptional conditions and not relied on for regular loop control due to performance and readability concerns.
  • The else clause on for and while loops executes only when the loop completes normally without hitting a break, making it ideal for implementing clear and concise “not found” logic.

Prerequisites

You should have Python 3 installed and a programming environment on your computer or server. If you don’t have a programming environment set up, you can refer to the installation and setup guides for setting up a local Python programming environment on your server appropriate for your operating system (Ubuntu, CentOS, Debian, etc.)

Break Statement

In Python, the break statement allows you to exit out of a loop when an external condition is triggered. You’ll put the break statement within the code block under your loop statement, usually after a conditional if statement.

Info: To follow along with the example code in this tutorial, open a Python interactive shell on your local system by running the python3 command. Then you can copy, paste, or edit the examples by adding them after the >>> prompt.

Let’s look at an example that uses the break statement in a for loop:

number = 0

for number in range(10):
    if number == 5:
        break    # break here

    print('Number is ' + str(number))

print('Out of loop')

The variable number is initialized at 0 in this small program. Then, a for loop is constructed to iterate through the numbers 0 through 9, as defined by range(10).

Note: Although number is initially set to 0, this value is immediately overwritten by the for loop, which assigns new values from the range() on each iteration.

Within the for loop, an if statement presents the condition that if the variable number is equivalent to the integer 5, then the loop will break. You can refer to this tutorial on Using for() loop in Python to learn more about using the for loop.

Note: You can also refer to this tutorial on How to construct while loops in Python to learn more about looping in Python.

Within the loop is also a print() statement that will execute with each iteration of the for loop until the loop breaks, since it is after the break statement.

Let’s place a final print() statement outside of the for loop to know when you are out of the loop.

When you run this code, you will get the following output:

Output
Number is 0 Number is 1 Number is 2 Number is 3 Number is 4 Out of loop

This shows that once the integer number is evaluated as equivalent to 5, the loop breaks, as the program is told to do so with the break statement.

The break statement causes a program to break out of a loop.

Continue Statement

The continue statement allows you to skip over the part of a loop where an external condition is triggered, but to go on to complete the rest of the loop. The current iteration of the loop will be disrupted, but the program will return to the top of the loop.

The continue statement will be within the code block under the loop statement, usually after a conditional if statement.

Using the same for loop program as in the break statement section above, we’ll use a continue statement rather than a break statement:

number = 0

for number in range(10):
    if number == 5:
        continue    # continue here

    print('Number is ' + str(number))

print('Out of loop')

The difference in using the continue statement rather than a break statement is that our code will continue despite the disruption when the variable number is evaluated as equivalent to 5. Let’s review our output:

Output
Number is 0 Number is 1 Number is 2 Number is 3 Number is 4 Number is 6 Number is 7 Number is 8 Number is 9 Out of loop

Here, Number is 5 never occurs in the output, but the loop continues after that point to print lines for the numbers 6–9 before leaving the loop.

You can use the continue statement to avoid deeply nested conditional code or optimize a loop by eliminating frequently occurring cases you would like to reject.

The continue statement causes a program to skip certain factors that come up within a loop but then continue through the rest of the loop.

Pass Statement

When an external condition is triggered, the pass statement allows you to satisfy Python’s syntactical requirement for a code block without performing any operation; all of the code will continue to be read unless a break or other statement occurs.

As with the other statements, the pass statement will be within the code block under the loop statement, typically after a conditional if statement.

Using the same code block as above, let’s replace the break or continue statement with a pass statement:

number = 0

for number in range(10):
    if number == 5:
        pass    # pass here

    print('Number is ' + str(number))

print('Out of loop')

After the if conditional statement, the pass statement tells the program to continue running the loop and ignore that the variable number evaluates as equivalent to 5 during one of its iterations.

You’ll run the program and get the following output:

Output
Number is 0 Number is 1 Number is 2 Number is 3 Number is 4 Number is 5 Number is 6 Number is 7 Number is 8 Number is 9 Out of loop

By using the pass statement in this program, you notice that the program runs exactly as it would if there were no conditional statements in the program. The pass statement tells the program to disregard that condition and continue to run the program as usual.

The pass statement can create minimal classes, or act as a placeholder when working on new code and thinking on an algorithmic level before hammering out details.

Performance Implications: Exceptions vs Flags vs Functions

When controlling the flow of loops in Python, you may find yourself deciding between using exceptions, flags, or functions with return to exit a loop early, especially in the case of nested loops or complex logic conditions. Each method has different performance and readability implications depending on how it’s used.

Let’s explore each approach with examples and discuss when it is best to use one over the other.

Using Exceptions to Exit Loops

In Python, exceptions can be raised intentionally to break out of deeply nested loops. While this method works, it is not the most efficient for regular control flow.

class ExitLoop(Exception):
    pass

try:
    for i in range(5):
        for j in range(5):
            if i == 2 and j == 3:
                raise ExitLoop()
            print(f"i={i}, j={j}")
except ExitLoop:
    print("Exited nested loops using an exception.")
Output
i
=0, j=0 i=0, j=1 i=0, j=2 i=0, j=3 i=0, j=4 i=1, j=0 i=1, j=1 i=1, j=2 i=1, j=3 i=1, j=4 i=2, j=0 i=2, j=1 i=2, j=2 Exited nested loops using an exception.

Note: Raising and catching exceptions is relatively slow in Python because it requires stack unwinding and object creation. Exceptions are ideal for truly exceptional conditions, not for regular control logic.

Using Flags to Exit Nested Loops

Flags are a simple and readable way to exit nested loops. They work by setting a variable (usually a Boolean) when a condition is met, then checking that flag in the outer loop.

found = False

for i in range(5):
    for j in range(5):
        if i == 2 and j == 3:
            found = True
            break
        print(f"i={i}, j={j}")
    if found:
        break

print("Exited nested loops using a flag.")
Output
i
=0, j=0 i=0, j=1 i=0, j=2 i=0, j=3 i=0, j=4 i=1, j=0 i=1, j=1 i=1, j=2 i=1, j=3 i=1, j=4 i=2, j=0 i=2, j=1 i=2, j=2 Exited nested loops using a flag.

Flags are more efficient than exceptions for loop control and are a good choice when:

  • You need to break from multiple loops.
  • You want clean, readable code.
  • You don’t want to introduce additional function or class definitions.

Using Functions with return to Exit Early

Wrapping your loop logic in a function and using return to exit is a Pythonic and efficient method to stop execution when a condition is met.

def search():
    for i in range(5):
        for j in range(5):
            if i == 2 and j == 3:
                print("Condition met. Exiting function.")
                return
            print(f"i={i}, j={j}")

search()
Output
i
=0, j=0 i=0, j=1 i=0, j=2 i=0, j=3 i=0, j=4 i=1, j=0 i=1, j=1 i=1, j=2 i=1, j=3 i=1, j=4 i=2, j=0 i=2, j=1 i=2, j=2 Condition met. Exiting function.

Using a function makes the logic modular and avoids cluttering outer scopes with flags or exception classes. It’s a great approach when:

  • You can encapsulate the loop inside a function.
  • You want clean exits with fewer side effects.
  • You plan to reuse the logic or test it independently.

The following table compares the readability, performance, and ideal use cases for each method of exiting loops in Python, helping you choose the most effective approach for your scenario.

Method Readability Performance Use Case Recommendation
Exception Low Slow Avoid for loop control; use for true exceptions
Flag Medium Fast Good for nested loops; easy to follow
Function High Fast Best for modular and testable loop logic

For most situations where you need to exit nested loops or complex conditions, functions or flags are the preferred methods due to their clarity and performance. Avoid using exceptions unless handling actual errors or unexpected conditions.

Using the else Clause with Loops for Cleaner Exits

Python allows an else clause to be used with both for and while loops. While this feature may seem unusual at first, it can lead to cleaner and more readable code, especially when combined with control flow statements like break.

Understanding Loop else Behavior

The else block associated with a loop is executed only if the loop completes without encountering a break statement. This is particularly useful for searching operations, where you’re iterating over a sequence and performing an action if a condition is met or taking alternative action if it’s not.

This feature is often underused, but when applied correctly, it helps avoid unnecessary state variables (like flags) and keeps related logic within the scope of the loop construct.

Example: Searching for a Value in a List

Let’s consider a simple use case: searching for an item in a list.

items = ["apple", "banana", "cherry", "date"]

for item in items:
    if item == "cherry":
        print("Found cherry!")
        break
else:
    print("Cherry not found.")

How it works:

  • The for loop iterates over the list.
  • If "cherry" is found, the break statement stops the loop immediately.
  • If the loop completes without encountering a break, the else block runs.

Output:

Found cherry!

If we change the search term to something that’s not in the list:

for item in items:
    if item == "fig":
        print("Found fig!")
        break
else:
    print("Fig not found.")

Output:

Fig not found.

This approach eliminates the need for an external flag variable to determine whether the item was found.

Why Use else with Loops?

Without the else clause, you would typically manage the same logic using a flag which may unnecessarily complicate the control flow:

found = False

for item in items:
    if item == "cherry":
        print("Found cherry!")
        found = True
        break

if not found:
    print("Cherry not found.")

While this works, it requires introducing and managing an additional variable (found). Using the else clause helps encapsulate the logic entirely within the loop construct, leading to simpler and more focused code, particularly in short searches or conditional iteration.

Practical Use Case: Scanning a File for a Keyword

Suppose you are reading a file line by line and want to check whether it contains a specific keyword. Using a for-else structure allows you to handle both outcomes cleanly:

with open("example.txt") as f:
    for line in f:
        if "error" in line:
            print("Error found in file.")
            break
    else:
        print("No errors detected.")

Here:

  • The loop scans each line.
  • If the keyword "error" is found, it prints a message and exits the loop early.
  • If no such line exists, the else clause executes after the loop finishes.

This pattern can be particularly useful in file scanning, log analysis, or any search-through-sequence scenario.

Using else with while Loops

The else clause works with while loops in the same way. It runs only if the loop condition becomes false without encountering a break.

count = 0

while count < 5:
    if count == 3:
        print("Condition met. Exiting early.")
        break
    count += 1
else:
    print("Loop completed without meeting condition.")

In this example:

  • If count reaches 3, the loop exits via break, and the else clause is skipped.
  • If the loop runs fully without triggering break, the else block executes.

This can be helpful when you want to detect whether the loop completed its full range of iterations or ended prematurely due to a specific condition.

The following table summarizes when the else clause attached to a loop will execute, depending on how the loop is exited.

Scenario Is else executed?
Loop completes all iterations Yes
Loop exits with a break No
Loop exits due to an exception No (unless handled)
Loop runs zero times Yes

The else clause with loops is not a replacement for conditionals or exceptions, but it is a clean and Pythonic way to handle “not found” cases or post-iteration logic without external state management.

Real-World Scenarios for Multi-Level Loop Breaks

In many real-world programming situations, you may work with nested loops, especially when handling structured data, processing files, or searching through grids. Sometimes, you need to exit multiple levels of loops at once when a certain condition is met, such as finding a match or encountering invalid data.

Python doesn’t have a built-in break outer like some languages, so you’ll often use techniques like flags, functions with return, or exceptions to exit nested loops cleanly.

Let’s walk through several practical examples to understand when and how to use multi-level loop breaks effectively.

Parsing Structured Data (e.g., CSV or JSON)

Suppose you’re parsing tabular data (such as a list of rows from a CSV file), and you want to find a specific value. Once you find it, there’s no need to continue scanning.

Here’s a 2D list that mimics rows and columns of a simple dataset:

data = [
    ["id", "name", "status"],
    ["001", "Alice", "active"],
    ["002", "Bob", "inactive"],
    ["003", "Carol", "active"]
]

Let’s say you want to find whether "Bob" exists in the dataset:

target = "Bob"
found = False

for row in data:
    for item in row:
        if item == target:
            print(f"Found target '{target}' in row: {row}")
            found = True
            break  # Exit inner loop
    if found:
        break  # Exit outer loop
Output
Found target 'Bob' in row: ['002', 'Bob', 'inactive']

Why it works:

  • The inner loop checks each item.
  • When it finds the target, it sets the found flag to True and breaks out of the inner loop.
  • The outer loop checks the flag and breaks as well.

This technique avoids scanning more data than necessary, improving performance for large datasets.

Searching Files in Nested Directories

When working on real-world applications like log analysis, content search, or security audits, you might need to search through hundreds of text files spread across multiple nested directories. The goal is often to find a specific keyword, such as an API key, a password, or a user ID. Once the keyword is found in any file, the search should terminate immediately to save time and resources.

Python provides a built-in function called os.walk() that allows you to iterate over all files and folders within a root directory recursively, meaning it will go into subdirectories as well.

Let’s see an example. You need to recursively scan all .txt files in a directory, search each line for a keyword, and immediately stop and report the file name when the keyword is found.

import os

def search_file(root_dir, keyword):
    for folder, subfolders, files in os.walk(root_dir):
        for file in files:
            if file.endswith(".txt"):
                with open(os.path.join(folder, file)) as f:
                    for line in f:
                        if keyword in line:
                            print(f"Found '{keyword}' in {file}")
                            return  # Exit all loops by returning from the function
    print("Keyword not found.")

You can call the function like this:

search_file("./documents", "password123")

Why use a function here:

  • return exits the function immediately, which is useful when nested loops are involved.
  • Cleaner than using flags across multiple levels of code.
  • Ideal when the logic can be modularized.

Grid or Matrix Search (e.g., in Games or Pathfinding)

Imagine working with a 2D grid (list of lists), such as a game map or seating chart, and needing to find the first obstacle or occupied seat. You want to stop scanning the grid as soon as the first obstacle is found, instead of checking every remaining cell unnecessarily.

Let’s define the grid where 0 represents an empty space, 1 represents an obstacle:

grid = [
    [0, 0, 0, 1],
    [0, 1, 0, 0],
    [0, 0, 0, 0],
]

Let’s say your task is to find the first obstacle (1) and then stop.

found = False

for row_index, row in enumerate(grid):
    for col_index, cell in enumerate(row):
        if cell == 1:
            print(f"Obstacle found at ({row_index}, {col_index})")
            found = True
            break  # Exit inner loop
    if found:
        break  # Exit outer loop
Output
Obstacle found at (0, 3)

What’s happening:

  • Each cell is checked using nested for loops.
  • The first 1 found triggers a break from both loops using the found flag.

This type of search is common in maze-solving, matrix scanning, or games like Minesweeper.

Validating Nested Data Fields

In real-world applications, especially in form handling, API processing, or user management systems, you often deal with lists of dictionaries, each dictionary representing a record with multiple fields.

You may want to scan these records to verify that all required fields are filled out. If any field is empty, your application should immediately stop processing and alert the user or log the error.

Let’s look at an example. We have a list of users where each user must have a name and an email; our goal is to scan all users, check each of their fields, and immediately stop processing as soon as any field is missing, reporting exactly which field is missing and for which user.

Consider the following sample input:

users = [
    {"name": "Alice", "email": "alice@example.com"},
    {"name": "Bob", "email": ""},
    {"name": "Carol", "email": "carol@example.com"},
]

Here, Bob’s email field is empty. We want to catch that.

invalid = False

for user in users:
    for key, value in user.items():
        if not value:
            print(f"Invalid entry: Missing {key} for user {user['name']}")
            invalid = True
            break
    if invalid:
        break
Output
Invalid entry: Missing email for user Bob

The program correctly identifies that Bob’s email is missing and stops further checking; Carol is never evaluated, because we exited the loop early.

Why this pattern is useful:

  • You prevent unnecessary checks once an error is detected.
  • Helps enforce fail-fast behavior, where the system stops early if something is wrong.

When Should You Use Multi-Level Breaks?

Use multi-level breaks when:

  • Continuing to loop would waste time or resources.
  • You’ve already found what you’re looking for.
  • Early exits make the logic easier to understand and maintain.

Choose your method wisely:

Method Best When…
Flag You’re inside a loop but don’t want to exit the function.
Function Your logic is modular and can return early from a routine.
Exception You have deeply nested logic and a true “exceptional” case.

FAQs

1. How to use pass, continue, and break in Python?

  • pass: The pass statement does nothing; it is used as a placeholder when a statement is syntactically required but no action is needed. For example:

    for i in range(5):
    if i == 3:
        pass  # Placeholder for future code
    print(i)
    
  • continue: The continue statement skips the rest of the code in the current iteration and moves to the next iteration of the loop. Example:

    for i in range(5):
        if i == 3:
            continue  # Skip the current iteration when i == 3
        print(i)
    
  • break: The break statement exits the loop immediately, regardless of the iteration condition. Example:

    for i in range(5):
        if i == 3:
            break  # Exit the loop when i == 3
        print(i)
    

2. How can you use break and continue statements in a for loop?

The break statement can be used inside a for loop to terminate it early when a specific condition is met. Example:

for i in range(10):
    if i == 5:
        break  # Exit the loop when i == 5
    print(i)

The continue statement skips the rest of the current iteration and moves to the next iteration. Example:

for i in range(10):
    if i % 2 == 0:
        continue  # Skip even numbers
    print(i)  # Only prints odd numbers

3. Can you resume a loop after a break ?

Once a break statement is executed, the loop terminates, and the code after the loop is executed. If you want to “continue” a loop after breaking, you need to reinitialize the loop.

For Example:

for i in range(3):  # First loop
    if i == 1:
        break  # Break out of the loop when i == 1
    print(f"First loop iteration: {i}")

# Restarting the loop
for i in range(3, 6):  # Second loop
    print(f"Second loop iteration: {i}")

4. How can I use a break statement in my Python for loops?

The break statement is straightforward to use in a for loop to terminate it when a specific condition is met:

for i in range(5):
    print(f"Checking value: {i}")
    if i == 2:
        print("Condition met. Breaking out of the loop.")
        break  # Exit the loop immediately
print("Loop ended.")

5. How to code a loop in Python ?

In Python, loops can be written using for or while. Examples:

Using a for loop:

for i in range(5):
    print(i)  # Prints numbers from 0 to 4

Using a while loop:

count = 0
while count < 5:
    print(count)  # Prints numbers from 0 to 4
    count += 1

To learn more about using for loop and while loop in python, you can refer to the tutorials below:

6. What does pass do in a Python for loop?

The pass statement acts as a placeholder and performs no action. It’s often used when a block of code is syntactically required but hasn’t been implemented yet:

for i in range(5):
    if i == 3:
        pass  # Placeholder
    print(i)
Output
0 1 2 3 4

7. Does Python break exit all loops ?

No, the break statement only exits the innermost loop where it is executed. To exit nested loops, you can use additional control mechanisms, such as flags or functions. Example:

for i in range(3):
    for j in range(3):
        if i == 1 and j == 1:
            break  # Exits the inner loop
        print(f"i={i}, j={j}")
Output
i
=0, j=0 i=0, j=1 i=0, j=2 i=1, j=0 i=2, j=0 i=2, j=1 i=2, j=2

To exit all loops, you can use a flag or wrap the loops in a function and use return:

# Using a flag
flag = False
for i in range(3):
    for j in range(3):
        if i == 1 and j == 1:
            flag = True
            break
        print(f"i={i}, j={j}")
    if flag:
        break

# Using a function
def nested_loops():
    for i in range(3):
        for j in range(3):
            if i == 1 and j == 1:
                return  # Exit all loops
            print(f"i={i}, j={j}")

nested_loops()

8. How to break from multiple nested loops in Python?

Python does not support a built-in syntax like break 2 to exit multiple nested loops at once. Instead, you can break out of nested loops using one of the following methods:

  1. Use a flag variable:

    found = False
    
    for i in range(3):
        for j in range(3):
            if some_condition(i, j):
                found = True
                break  # Break inner loop
        if found:
            break  # Break outer loop
    
  2. Wrap the loops in a function and use return:

    def search():
        for i in range(3):
            for j in range(3):
                if some_condition(i, j):
                    print("Condition met.")
                    return  # Exits both loops
    
    search()
    

This function-based approach is often preferred for readability and modularity.

9. Is there a break level parameter in Python loops?

No, Python does not have a break level or label-based loop control like some other languages (such as Java or PHP). The break statement in Python always exits only the innermost loop in which it is used.

To exit from multiple levels of nesting, you need to use:

  • A flag to signal the outer loop.
  • A function with return.
  • An exception, though that should be used carefully and only when appropriate.

10. How to exit an outer loop from inside an inner loop?

To exit an outer loop from inside an inner loop, you can either:

  1. Use a flag variable that gets set inside the inner loop and checked in the outer loop:

    exit_outer = False
    
    for i in range(5):
        for j in range(5):
            if i == 2 and j == 3:
                exit_outer = True
                break
        if exit_outer:
            break
    
  2. Encapsulate the loop in a function and use return for a cleaner and more Pythonic approach:

    def process():
        for i in range(5):
            for j in range(5):
                if i == 2 and j == 3:
                    print("Exiting from nested loop.")
                    return
    
    process()
    

11. Is using exceptions for loop breaking a good practice?

Using exceptions to break out of loops is technically possible but not considered good practice for normal control flow.

class BreakOut(Exception):
    pass

try:
    for i in range(3):
        for j in range(3):
            if i == 1 and j == 1:
                raise BreakOut()
except BreakOut:
    print("Exited nested loops using exception.")

While this works, exceptions are meant for unexpected or exceptional conditions, not standard loop control. Using exceptions this way can make your code harder to read, maintain, and debug. Prefer flags or function-based exits unless you’re dealing with a truly exceptional case.

Conclusion

You explored the core loop control statements in Python: break, continue, and pass, explaining their behavior with clear examples and practical use cases. We also explored more advanced control flow techniques, such as breaking out of nested loops using flags and function returns, and discussed the use of exceptions for loop termination along with their trade-offs.

Additionally, we introduced the else clause on loops, highlighting how it can simplify certain search patterns by eliminating the need for extra flags. Real-world examples such as data parsing, file scanning, grid searches, and input validation were used to show how these techniques apply in practice. Together, these concepts help you write more structured, readable, and efficient loop logic in Python.

To further build your understanding of Python loops and core programming concepts, check out these helpful tutorials:

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

Tutorial Series: How To Code in Python

Python is a flexible and versatile programming language that can be leveraged for many use cases, with strengths in scripting, automation, data analysis, machine learning, and back-end development. It is a great tool for both new learners and experienced developers alike.

About the author(s)

Lisa Tagliaferri
Lisa Tagliaferri
Author
See author profile

Community and Developer Education expert. Former Senior Manager, Community at DigitalOcean. Focused on topics including Ubuntu 22.04, Ubuntu 20.04, Python, Django, and more.

Anish Singh Walia
Anish Singh Walia
Editor
Sr Technical Writer
See author profile

I help Businesses scale with AI x SEO x (authentic) Content that revives traffic and keeps leads flowing | 3,000,000+ Average monthly readers on Medium | Sr Technical Writer @ DigitalOcean | Ex-Cloud Consultant @ AMEX | Ex-Site Reliability Engineer(DevOps)@Nutanix

Manikandan Kurup
Manikandan Kurup
Editor
Senior Technical Content Engineer I
See author profile

With over 6 years of experience in tech publishing, Mani has edited and published more than 75 books covering a wide range of data science topics. Known for his strong attention to detail and technical knowledge, Mani specializes in creating clear, concise, and easy-to-understand content tailored for developers.

Still looking for an answer?

Was this helpful?


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Well done, Fully understood. Thanks for clarification. Have a nice day

exaplined quite simple, that’s nice; but you have a major error in your code: you modify the counter within the loop. Thr for with range automatically visits all values in the range. If you then increase the counter you will end up having only even numbers printed ;-)

is writing pass necessary? and what if we don’t write it?

Typo in example - blank line between break and print should not be there.

for number in range(10):
    if number == 5:
        break    # break here
    print('Number is ' + str(number))
Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.