Skip to main content

Your first Python: for loops and if/else

The PEEMS team · Published November 10, 2026 · 4 min read

Pemi CS level 'Red and green signs': a Python snippet with for and if/else on the left, and a robot walking past red and green signs on the right

A first Python snippet with for and if/else is really the text blocks your child already knows, written out as code. This post reads a Pemi CS level that counts red and green signs, line by line.

The first time they see a Python snippet, many students and parents think, "That is a lot of text." The good news: if your child has played with blocks, most of what is in that snippet is already familiar: a loop, a condition, a counting variable. They are just written out as text now.

The level: counting red and green signs

In the Pemi CS level "Red and green signs", a robot moves along a channel lined with signs. The harbor master wants a count of every colour. The level's Python looks like this:

Pemi CS level with a Python snippet using for and if/else, and a robot walking along red and green signs

from peems import robot

red = 0
green = 0
for _ in range(6):
    robot.forward(1)
    if robot.read_sign() == "red":
        red += 1
    else:
        green += 1
robot.forward(1)
robot.say("Red: " + str(red))
robot.say("Green: " + str(green))

Reading it line by line, next to the text blocks

The PythonThe text block students already know
from peems import robotLoads the commands for controlling the robot (no matching block)
red = 0, green = 0"Set variable to 0": two labelled boxes for counting
for _ in range(6):"Repeat 6 times"
robot.forward(1)"Move forward 1 step"
if ... == "red":"If the sign is red"
else:"Otherwise"
red += 1"Add 1 to red"
robot.say("Red: " + str(red))"Say" block: the robot reads the count out

Two things to notice:

  • Indentation shows which commands sit inside the loop or the condition, just as blocks sit "inside" a repeat block.
  • The colon : at the end of for, if and else says, "the indented lines below belong to me."

What does the program do?

The robot repeats six times: each time it moves forward one step and reads the sign. If the sign is red, it adds to the red count; otherwise it adds to the green count. After the loop the robot steps forward once more to the flag and "says" both numbers with two robot.say commands: the red count and the green count. The loop saves writing the same steps six times, and the condition lets the program behave differently for each sign.

Tips for parents

  1. Don't be put off by the new look. Ask your child to "read the code aloud in everyday words", as in the table.
  2. Step through it. Use the step button to watch the two counts grow.
  3. Ask "what if we change it?" What happens if range(6) becomes range(3)?
  4. Mistakes are normal. A missing colon or wrong indentation is a classic beginner bug.

Questions to talk about with your child

  • Which line in the code is the "repeat"? Which one "chooses a path"?
  • If a yellow sign appeared too, how would you change the code?

Frequently asked questions

When should a child move from text blocks to Python? There is no fixed point. Common signs are being comfortable with loops, conditions and variables, and reading and typing with ease. Every child moves at their own pace.

Is Python harder than blocks? The ideas are the same; the difference is typing things correctly and indenting. Syntax slips are common at first and completely normal.

What does for _ in range(6) mean? "Repeat 6 times." The underscore _ means we do not need the number of each repeat.

Do I need a powerful computer? No. Pemi CS needs no special hardware: an ordinary computer that browses the web is enough. We recommend a computer or tablet; the phone interface is not optimised yet.

Want to try it together?

Pemi CS has more than 3,000 levels that lead from blocks to Python. It is open now and international families are welcome to register their interest by email.

Keep reading: From block coding to Python: when is the right time to switch? · What is a function? Pemi defines its first one

The PEEMS team

Nov 10, 2026

Switching to Python too early frustrates children; too late and they get bored. This article lists signs of readiness, a path from picture blocks to text blocks to Python, and why ideas should come before syntax.

Nov 10, 2026

A function is a named group of commands you can call again and again. This post uses a Pemi CS level and a lemonade recipe to explain defining and calling a function.

Nov 10, 2026

To check whether a shelf is in order, compare each pair of neighbours. This post reads a Python is_sorted function through a Pemi CS level.