Multiplication Tables in Python: Nested for and while Loops

A multiplication table uses repeated products such as 3 × 4 = 12. Nested loops are useful for a grid of tables: the outer loop chooses the row and the inner loop chooses the column.

Interactive Companion Jupyter Notebook

Run the Python examples, compare expected outputs, modify the code, and complete guided exercises.

View on GitHub Open in Google Colab
On this page — Show page sections— Hide page sections

First working example

for row in range(1, 6):
    for col in range(1, 6):
        print(f"{row * col:3}", end=" ")
    print()

Output:

  1   2   3   4   5 
  2   4   6   8  10 
  3   6   9  12  15 
  4   8  12  16  20 
  5  10  15  20  25 

This produces a compact 5×5 multiplication grid. Increase both limits for 10×10.

Original 10 × 10 nested for-loop table

The original code uses the {:2d} format field. The version below prints one row per line without extra blank lines.

rows = 10
columns = 10
for i in range(1, rows + 1):
    for j in range(1, columns + 1):
        c = i * j
        print("{:3d}".format(c), end=" ")
    print()

Output:

  1   2   3   4   5   6   7   8   9  10 
  2   4   6   8  10  12  14  16  18  20 
  3   6   9  12  15  18  21  24  27  30 
  4   8  12  16  20  24  28  32  36  40 
  5  10  15  20  25  30  35  40  45  50 
  6  12  18  24  30  36  42  48  54  60 
  7  14  21  28  35  42  49  56  63  70 
  8  16  24  32  40  48  56  64  72  80 
  9  18  27  36  45  54  63  72  81  90 
 10  20  30  40  50  60  70  80  90 100 

Create a table using while loops

A while-loop version needs to reset the inner counter at the start of every new row.

rows = 3
columns = 5
i = 1
while i <= rows:
    j = 1
    while j <= columns:
        print(f"{i*j:3}", end=" ")
        j += 1
    print()
    i += 1

Output:

  1   2   3   4   5 
  2   4   6   8  10 
  3   6   9  12  15 

User-selected multiplication table

The source page asks the user for one number and prints its first twelve multiples.

number = int(input("Enter a number: "))
for multiple in range(1, 13):
    print(f"{number} x {multiple} = {number*multiple}")

Output:

7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
7 x 11 = 77
7 x 12 = 84

Reusable table function

Move display logic into a function, so you can choose a table number and end value.

def multiplication_table(number, up_to=5):
    return [number * i for i in range(1, up_to + 1)]
print(multiplication_table(9, 6))

Output:

[9, 18, 27, 36, 45, 54]

Format a grid with aligned columns

Fixed-width formatting keeps two- and three-digit results aligned.

for i in range(1, 6):
    print(" ".join(f"{i*j:3d}" for j in range(1, 6)))

Output:

  1   2   3   4   5
  2   4   6   8  10
  3   6   9  12  15
  4   8  12  16  20
  5  10  15  20  25

Validate row and column values

Reject non-positive dimensions instead of silently producing an empty table.

rows, columns = 4, 0
if rows <= 0 or columns <= 0:
    print("Both dimensions must be positive")
else:
    print([[i * j for j in range(1, columns+1)] for i in range(1, rows+1)])

Output:

Both dimensions must be positive

Common mistakes and troubleshooting

  • Run each example in Python 3.9 or newer. Indentation is significant in loops and functions.
  • Remember that input() returns a string. Convert and validate the value before doing arithmetic.
  • Check boundary values such as zero, negative numbers, small inputs and empty ranges.
  • When comparing outputs, note whether a code sample uses fixed values or user input.

Practice exercises

Print the multiplication tables of 7 and 9, then produce a 12×12 grid. Use the companion practice notebook for runnable examples and self-checks.

Frequently asked questions

Why are there two for loops?

The outer loop handles rows and the inner loop handles columns.

Why is print() after the inner loop?

It moves to the next line after each row.

How do I change to a 12×12 table?

Set both loop end values to 12.

Plus2Net Python Basics podcast playlist

Other related resources

Back to top ↑




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer