Multiplication Table
Learn how to generate a multiplication table in Python using loops.
Multiplication Table
Problem
Given a number, print its multiplication table. Then extend it to print a full N×N multiplication table for any size.
Part 1 — single number table:
Input: 5
Output:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50Part 2 — full N×N table:
Input: 5
Output:
1 2 3 4 5
1 1 2 3 4 5
2 2 4 6 8 10
3 3 6 9 12 15
4 4 8 12 16 20
5 5 10 15 20 25Logic
Part 1:
- Loop from 1 to 10
- Multiply the given number by the loop counter
- Print the result
Part 2:
- Loop through rows 1 to N
- For each row, loop through columns 1 to N
- Multiply row number by column number
- Print the result formatted in a grid
Flow
Part 2 flow:
Part 1 — Solution 1: basic loop
Loop from 1 to 10 and multiply.
def multiplication_table(n):
print(f"Multiplication table of {n}:")
print("-" * 20)
for i in range(1, 11):
# :2d means right-align the multiplier in 2 spaces
# :3d means right-align the result in 3 spaces
print(f"{n} x {i:2} = {n * i:3}")
multiplication_table(5)
multiplication_table(12)Code Execution — Part 1
Trace through multiplication_table(5):
| Loop | i | n * i | Printed |
|---|---|---|---|
| 1st | 1 | 5 | 5 x 1 = 5 |
| 2nd | 2 | 10 | 5 x 2 = 10 |
| 3rd | 3 | 15 | 5 x 3 = 15 |
| ... | ... | ... | ... |
| 10th | 10 | 50 | 5 x 10 = 50 |
range(1, 11) gives 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 — starts at 1, stops before 11.
Part 1 — Solution 2: custom range
Let the user choose the range — not just 1 to 10.
def multiplication_table(n, start=1, end=10):
print(f"Multiplication table of {n} ({start} to {end}):")
print("-" * 25)
for i in range(start, end + 1):
print(f"{n} x {i:2} = {n * i:4}")
# default range — 1 to 10
multiplication_table(7)
# custom range — 5 to 15
multiplication_table(7, start=5, end=15)
# first 5 only
multiplication_table(3, end=5)Code Execution — Part 1 Solution 2
Trace through multiplication_table(7, start=5, end=15):
range(5, 16) gives 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
i | 7 * i |
|---|---|
5 | 35 |
6 | 42 |
7 | 49 |
| ... | ... |
15 | 105 |
Part 2 — Solution 1: full N×N table with nested loops
Print a complete grid — every number multiplied by every other number.
def full_multiplication_table(n):
# print header row — column labels
print(f"{'':4}", end="") # empty corner cell
for col in range(1, n + 1):
print(f"{col:4}", end="")
print() # new line after header
# print separator
print("-" * (4 * (n + 1)))
# print each row
for row in range(1, n + 1):
# print row label
print(f"{row:<4}", end="")
# print each column value in this row
for col in range(1, n + 1):
print(f"{row * col:4}", end="")
print() # new line after each row
full_multiplication_table(5)
print()
full_multiplication_table(10)Code Execution — Part 2 Solution 1
Trace through full_multiplication_table(5):
Header row:
range(1, 6) → 1, 2, 3, 4, 5
Prints: 1 2 3 4 5
Row 1:
row = 1, range(1, 6) → col = 1, 2, 3, 4, 5
col | row * col | Printed |
|---|---|---|
1 | 1 × 1 = 1 | 1 |
2 | 1 × 2 = 2 | 2 |
3 | 1 × 3 = 3 | 3 |
4 | 1 × 4 = 4 | 4 |
5 | 1 × 5 = 5 | 5 |
Full row 1: 1 1 2 3 4 5
Row 3:
col | row * col | Printed |
|---|---|---|
1 | 3 × 1 = 3 | 3 |
2 | 3 × 2 = 6 | 6 |
3 | 3 × 3 = 9 | 9 |
4 | 3 × 4 = 12 | 12 |
5 | 3 × 5 = 15 | 15 |
Full row 3: 3 3 6 9 12 15
end="" in print() stops Python from adding a newline after each value — so all values in a row print on the same line. Then a plain print() at the end of each row moves to the next line.
Part 2 — Solution 2: using list comprehension and join
Build each row as a list and join it into a string.
def full_multiplication_table(n):
# header row
header = " " + "".join(f"{col:4}" for col in range(1, n + 1))
print(header)
print("-" * len(header))
# each data row
for row in range(1, n + 1):
# build the row as a string
row_str = f"{row:<4}" + "".join(f"{row * col:4}" for col in range(1, n + 1))
print(row_str)
full_multiplication_table(5)Code Execution — Part 2 Solution 2
Trace through row 2 of full_multiplication_table(5):
row = 2
Generator: f"{2 * col:4}" for col in range(1, 6)
col | 2 * col | Formatted |
|---|---|---|
1 | 2 | " 2" |
2 | 4 | " 4" |
3 | 6 | " 6" |
4 | 8 | " 8" |
5 | 10 | " 10" |
"".join(...) = " 2 4 6 8 10"
Full row: "2 2 4 6 8 10"
Part 2 — Solution 3: storing in a 2D list
Build the table as a 2D list first, then print it. Useful when you want to process the data before displaying.
def build_multiplication_table(n):
# build the table as a 2D list
# table[i][j] = (i+1) * (j+1)
table = [
[(row + 1) * (col + 1) for col in range(n)]
for row in range(n)
]
return table
def print_table(table):
n = len(table)
# header
print(f"{'':4}", end="")
for col in range(1, n + 1):
print(f"{col:4}", end="")
print()
print("-" * (4 * (n + 1)))
# rows
for i, row in enumerate(table):
print(f"{i + 1:<4}", end="")
for value in row:
print(f"{value:4}", end="")
print()
table = build_multiplication_table(5)
print_table(table)
# you can also access individual values
print(f"\n3 x 4 = {table[2][3]}") # row 3, col 4 → index [2][3]
print(f"5 x 5 = {table[4][4]}") # row 5, col 5 → index [4][4]Code Execution — Part 2 Solution 3
Trace through build_multiplication_table(3):
The comprehension [(row+1) * (col+1) for col in range(3)] for row in range(3):
row | col=0 | col=1 | col=2 | Row result |
|---|---|---|---|---|
0 | 1×1=1 | 1×2=2 | 1×3=3 | [1, 2, 3] |
1 | 2×1=2 | 2×2=4 | 2×3=6 | [2, 4, 6] |
2 | 3×1=3 | 3×2=6 | 3×3=9 | [3, 6, 9] |
Result: [[1,2,3], [2,4,6], [3,6,9]]
Accessing table[2][3] means row index 2 (= row 3), col index 3 (= col 4) → 3 × 4 = 12
Which solution to use?
| Solution | How | Best when |
|---|---|---|
| Part 1 — Solution 1 | Basic loop | Learning loops |
| Part 1 — Solution 2 | Custom range | Flexible, real use |
| Part 2 — Solution 1 | Nested loops | Understanding nested iteration |
| Part 2 — Solution 2 | List comprehension + join | Cleaner code |
| Part 2 — Solution 3 | 2D list | When you need to store and process the data |
Output
Part 1 — multiplication_table(5):
Multiplication table of 5:
--------------------
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50Part 2 — full_multiplication_table(5):
1 2 3 4 5
--------------------
1 1 2 3 4 5
2 2 4 6 8 10
3 3 6 9 12 15
4 4 8 12 16 20
5 5 10 15 20 25