Skip to content

ይህ ትምህርት ገና ወደ አማርኛ አልተተረጎመም፤ ስለዚህ በእንግሊዝኛ ቀርቧል። የእንግሊዝኛውን ገጽ ክፈቱ

for loops and range()

4 min read

A while loop keeps going until something happens. Often you already know what to go through: every letter of a name, the numbers 1 to 100, ten rows of a table. For that, Python has for. By the end of this lesson you'll loop over text and numbers, and you'll know the two loop bugs that catch nearly everyone: the one that misses the last number, and the one that forgets the total.

One value at a time

Predict what this prints.

ውጤቱን ገምቱ

Decide before you look. Guessing wrong is how this sticks.

for letter in "Selam":
    print(letter)

Python prints

S
e
l
a
m

for goes through the text one character at a time. Each round, letter refers to the next character, and the body prints it.

for letter in "Selam": means "for each character in the text, call it letter and run the block". The name after for is the : each round it refers to the next value, and you don't update it yourself. Ge'ez works the same way, one character per round:

for letter in "ሰላም":
    print(letter)

In the editor, press Escape then Tab to move on.

Change the text to your own name.

Counting with range()

range() gives a series of whole numbers to loop over. Predict this one carefully.

ውጤቱን ገምቱ

Decide before you look. Guessing wrong is how this sticks.

for i in range(5):
    print(i)
Pick the output

Python prints

0
1
2
3
4

range(5) gives five numbers, starting from 0: 0, 1, 2, 3, 4. It starts at 0 and stops before 5, never on it.

range(5) starts at 0 and stops before 5. Give it two numbers, a start and a stop, and it starts where you say: range(1, 6) gives 1 to 5. A third number is the step, and a negative step counts down:

for n in range(10, 0, -2):
    print(n)

In the editor, press Escape then Tab to move on.

The stop is never included, even counting down: 0 isn't printed. Try range(1, 6), then range(0, 20, 5).

range() only takes whole numbers, so a number that came from input() needs converting first:

Back to the stop. "Stops before" causes the most common loop bug of all, the error:

ጥያቄ

Which loop prints the numbers 1 to 10?

A running total

To add up 1 to 100, keep a running total: start it at 0 before the loop, and add each number to it inside the loop.

total = 0
for n in range(1, 101):
    total = total + n
print(total)

In the editor, press Escape then Tab to move on.

n is the loop's counter, and total is the running total. Where total = 0 goes matters. Here it's inside the loop by mistake; predict what this prints.

ውጤቱን ገምቱ

Decide before you look. Guessing wrong is how this sticks.

for n in range(1, 4):
    total = 0
    total = total + n
print(total)
Pick the output

Python prints

3

total = 0 runs at the start of every round, so each round throws the total away and adds only the current number. After the last round, total is just 3.

Step through it and watch total drop back to 0 each round:

The reset bug

Press Next to run one line at a time and watch each name change.

for n in range(1, 4):
total = 0
total = total + n
print(total)
Names and their values after line 1
NameValue after line 1
ncounter1new
totalnot made yet

Output so far

Step 1 of 11

Step 1 of 11. Line 1: for n in range(1, 4):. n is now 1, the counter.

The last visit to line 1 finds no numbers left, so the loop ends. The fix is to move total = 0 above the loop, so it runs once: the code then prints 6, the same shape as the 1-to-100 program. A running total always starts before the loop.

The loop variable is not in charge

In a while loop, you change the counter yourself. In a for loop, you don't, and changing it has no effect on the next round. Predict:

ውጤቱን ገምቱ

Decide before you look. Guessing wrong is how this sticks.

for i in range(3):
    print(i)
    i = 10
Pick the output

Python prints

0
1
2

Each round, for gives i the next value from range(3), whatever you did to i in the round before. Setting i = 10 changes it only until the end of that round.

So which loop should you use? If you know what you're going through, or how many rounds (every letter, 1 to 100, 10 rows), use for. If you're repeating until something happens (a valid answer, a savings goal), use while.

መልመጃ

A multiplication table

Print the table for 7, from 7 x 1 = 7 to 7 x 10 = 70. Then change the number to 12. Check the first line and the last line: that's where off-by-one bugs show.

Output
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

Your code

number = 7
# Loop from 1 to 10 and print one line each round

In the editor, press Escape then Tab to move on.

መፍትሄውን አሳይ

This is one way to solve it, not the only one. If yours prints the same thing, it works.

number = 7
for i in range(1, 11):
    print(f"{number} x {i} = {number * i}")

ዋና ዋና ነጥቦች

  • for goes through values one at a time: the characters of text, or the numbers from range().
  • range(stop) starts at 0; range(start, stop, step) sets all three. The stop is never included.
  • Off-by-one: to include 10, stop at 11. Always check the first and last values.
  • A running total starts before the loop; inside the loop, it resets every round.
  • Know how many rounds? Use for. Repeating until something happens? Use while.