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.
Run the Python examples, compare expected outputs, modify the code, and complete guided exercises.
View on GitHubfor 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.
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
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
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
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]
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
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
input() returns a string. Convert and validate the value before doing arithmetic.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.
The outer loop handles rows and the inner loop handles columns.
It moves to the next line after each row.
Set both loop end values to 12.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.