Beginner Python Exercises

Multiplication Table in Python

Print 7 x 1 = 7, 7 x 2 = 14 and onward with a for loop and an f-string.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • Looping with range()
  • Building each row with an f-string
  • Aligning columns
n = 7
  1. 17 x 1 = 7
  2. 27 x 2 = 14
  3. 37 x 3 = 21
  4. 47 x 5 = 35
↺ i takes the next value from range(1, 6)

One row per multiplier

The problem

Printing a multiplication table is one of the first programs that produces many lines of output from one rule. It shows how a loop plus a format string replaces dozens of nearly identical print statements.

Think of it like this

A loop that prints a table is like a stamp with a blank space. The stamp always prints 7 x _ = _, and for every row you fill the blanks with the next multiplier and its product.

From a rubber stamp to code
number = 7🔢 The table you want
for multiplier in range(1, 6):🔁 One row for each multiplier 1 to 5
f"{number} x {multiplier} = {number * multiplier}"🖨️ Stamp one row with the blanks filled
print(...)📃 Each row on its own line
One rule, many rows.
In one sentence

A multiplication table lists the products of one number with a series of multipliers, one row per multiplier.

Key points

  1. range(1, 6) gives the multipliers 1 to 5.
  2. number * multiplier is computed inside the braces of the f-string.
  3. Each loop round prints one row, so five rows appear.
  4. Changing number or the range gives a different table without changing anything else.

When should you use it?

  • Generating repeated, patterned output from one rule.
  • Practicing for loops with range and f-strings.
  • Printing simple reports and tables.

When not to use it

  • Building a table of data to use later: store the values in a list instead of printing them.
  • Wide tables: align columns with format specifiers such as {product:>3}.

Example code

Run code →
main.py
number = 7
# Generate one formatted row for each multiplier.
for multiplier in range(1, 6):
    print(f"{number} x {multiplier} = {number * multiplier}")
Get a Run button for this example

Result of running the code

7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35

Code explanation

range(1, 6) produces the multipliers 1, 2, 3, 4 and 5, one for each loop round. In every round the f-string inserts number, the current multiplier and their product, computed directly inside the braces. Because print() is inside the loop, each product appears on its own line.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Missing the last row

range(1, 5) stops at 4.

Problem
for multiplier in range(1, 5):
Fix
for multiplier in range(1, 6):

Finished this lesson?

Mark it as learned to track your progress.

Next: Strings in Python →

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges