Tutorials

Python 3 Tutorial

All Tutorials Learning Tools Login

Python 3 Tutorial · Chapter 16 of 22

Python Nested While

Nest while loops for grids and counters.

Each while needs its own counter and update.

Reset the inner counter before every outer iteration.

Nested while sample Try it Yourself »
row = 1
while row <= 5:
    col = 1
    while col <= row:
        print(col, end=" ")
        col += 1
    print()
    row += 1

Classroom Tips

  • Watch for infinite loops when an update is missing.