← Python for Beginners
0 of 27 lessons done0%

Loops · Lesson 13 of 27

for loop and range

A for loop goes through items one by one. With range() it runs a fixed number of times.

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

range has three forms:

  • range(5) → 0, 1, 2, 3, 4
  • range(1, 6) → 1, 2, 3, 4, 5
  • range(0, 10, 2) → 0, 2, 4, 6, 8 (the third number is the step)

You can also loop over a string:

for ch in "hey":
    print(ch)

Use for when you know how many times to repeat. Use while when you repeat until something happens.

Your task

Read a number n and print its multiplication table from 1 to 10 in this format:

7 x 1 = 7
7 x 2 = 14
...
7 x 10 = 70