Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    PyPython

    Getting started

    • Your first Python program
    • Variables and types
    • Numbers and operators
    • Type conversion and input

    Strings

    • Strings and f-strings
    • String indexing and slicing
    • Cleaning and splitting strings

    Control flow

    • Conditions: if, elif and else
    • Loops: for and while
    • Loop control and nesting

    Functions

    • Functions with def
    • Parameters and scope

    Collections

    • Lists
    • Dictionaries
    • Tuples and sets
    • Comprehensions, enumerate and zip

    Errors, files and libraries

    • Handling exceptions
    • Modules and the standard library
    • Reading and writing files
    • Exchanging data with JSON

    Objects and lazy iteration

    • Classes and objects
    • Inheritance and composition
    • Iterators and generators

    Testing and project

    • Testing and project setup
    • Project: a grade report
    Python

    Control flow · Lesson 10 of 25

    Loop control and nesting

    Stop a search, skip an item and nest loops.

    8 minutes2 games

    break exits the nearest loop; continue skips the rest of the current iteration. A nested loop runs for each outer iteration, so track which variable changes and when.

    Python
    for n in range(1, 8):
        if n == 3:
            continue
        if n == 5:
            break
        print(n)
    
    for row in range(2):
        for col in range(2):
            print(row, col)

    Start iteration

    continue: next iteration

    break: leave loop

    Important

    A break in the inner loop does not stop the outer loop.

    Interview question

    How can continue create an infinite while loop?

    What's the output?

    for n in range(4):
        if n == 2:
            break
        print(n)

    What will this code print?

    Exercise

    Print the first number above 10 and stop searching.

    Expected output: 12

    Python
    numbers = [4, 12, 20]
    

    Learn more from

    • Python documentationdocs.python.org
    • Real Python: further readingrealpython.com

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousLoops: for and whileNextFunctions with def
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.