← Python from Scratch Lesson 31 of 78 · 10 XP

The range function

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

range() creates a sequence of numbers — the perfect partner for for:

for i in range(5):
    print(i)            # 0 1 2 3 4 — the end is not included

range(1, 6)             # 1 2 3 4 5
range(0, 10, 2)         # 0 2 4 6 8 — the third argument is the step
range(5, 0, -1)         # 5 4 3 2 1 — a countdown

Remember: range(a, b) goes from a up to b, not including b. That's why, to get the numbers from 1 to n, you write range(1, n + 1).

range doesn't build a list in memory; it hands out numbers one by one, so range(1_000_000) works instantly. You can see all the numbers like this: list(range(5)) → [0, 1, 2, 3, 4].

Task. Read a number n and print its multiplication table from 1 to 10 in the form 3 x 1 = 3 (a lowercase letter x, not ×).

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.