Skip to content

Multiplication table in Python using nested loops | Code

  • by

Use range function in for loop and if else condition for Multiplication table in Python.

Example Multiplication table in Python using nested loops

Simple example code nested loop to print Multiplication table in Python

for row in range(0, 10):
    for col in range(0, 10):
        num = row * col
        if num < 10:
            empty = "  "
        else:
            if num < 100:
                empty = " "
        if col == 0:
            if row == 0:
                print("    ", end='')
            else:
                print("  ", row, end='')
        elif row == 0:
            print("  ", col, end='')
        else:
            print(empty, num, end='')
    print()

Output:

Multiplication table in Python using nested loops

Another example using a join with map:

mult_table = [
    [1, 2, 3],
    [2, 4, 6],
    [3, 6, 9]
]

for row in mult_table:
    print(" | ".join(map(str, row)))

Output:

1 | 2 | 3
2 | 4 | 6
3 | 6 | 9

Multiplication table for double-digit numbers using nested loops in Python

for i in range(1, 10):
    print("i =", i, ":", end=" ")
    for j in range(1, 10):
        print("{:2d}".format(i * j), end=" ")
    print()

Output:

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

Do comment if you have any doubts or suggestions on this Python Multiplication table.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading