Making decisions in Python

Module 3 · Making decisions in Python · Lesson 6 of 7

Pseudocode and a peek at loops

Before writing real code, it helps to plan in plain language — a sketch called pseudocode. It's just the steps, written for a human, with no fussing over exact syntax:

ask for the temperature
if it is above 30:
    say "Hot!"
otherwise:
    say "Nice."

Notice it reads almost like the Python you just wrote — that's the point. Sketch the logic first, then translate each step into code. For a decision, you already can. But pseudocode often contains a step like “do this for every item” or “keep going until done” — and that's something new: repetition.

A loop runs the same block of code over and over. You've seen one on the home page, in fact — here it is again: a counter that walks through range(5), adding each number to a running total.

loop.pyrunning
total = 0
for i in range(5):
total += i
range(5)
0
1
2
3
4
i
total
0

That endless little cycle is exactly what the name loop is about — and it's where this module heads next. Loops let you process every item in a list, repeat until a condition changes, and do in three lines what would otherwise take three hundred. First, a quick quiz on decisions.