for and range

A for loop takes the values in a sequence one at a time. It runs its block once for each value. A sequence is a group of values in a fixed order. A string is a sequence of characters, so a loop over a string gives you one letter at a time.

for letter in "abc":
    print(letter)

Output:

a
b
c

Each time through the loop, the variable letter holds the next character: first a, then b, then c.

When you want to loop over numbers, use range. It gives you a sequence of whole numbers.

for number in range(1, 4):
    print(number)

Output:

1
2
3

The end number is not included. range(1, 4) starts at 1 and stops before 4, so you get 1, 2 and 3.

You can also give range a third number, called the step. The step is the amount added each time:

for number in range(2, 10, 3):
    print(number)

Output:

2
5
8

The loop starts at 2 and adds 3 each time, so you get 2, 5 and 8. The next value would be 11, which is past the end, so the loop stops after 8.

In the exercise, print a multiplication table for a number.