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 9 of 25

    Loops: for and while

    range, and looping over things.

    8 minutes3 games

    for loops over anything with items. range(5) gives 0 to 4 and range(1, 6) gives 1 to 5. while repeats while its condition is true.

    Python
    for i in range(1, 4):
        print("Round", i)
    
    for letter in "abc":
        print(letter.upper())
    
    count = 3
    while count > 0:
        print(count)
        count -= 1
    print("Go!")

    Important

    Update the variable used by while so the loop can stop. The end of range is excluded and its step can be negative.

    Interview question

    Should you choose for or while?

    Step by step1 / 6
    1. ▶total = 0
    2. 2for n in range(1, 3):
    3. 3 total += n
    4. 4print(total)

    Variables

    total0

    Output

    …

    What's the output?

    for i in range(3):
        print(i)

    What will this code print?

    Complete the code

    Print the numbers 1 to 10.

    for n in range(, ):
        print(n)

    Exercise

    Print the sum of 1 to 100 (the result is 5050).

    Expected output: 5050

    Python
    total = 0
    

    Learn more from

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

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    PreviousConditions: if, elif and elseNextLoop control and nesting
    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.