Always free. For everyone.
Course outline
Module 03 of 20 · Foundations

Make decisions and repeat work

Turn rules into conditions and process a whole dataset with loops. This module has 3 lessons and takes about 1h. Each lesson has a practice exercise that runs real Python in your browser and a short quiz.

Start module 03 in the Learning Lab
LESSON 1 OF 3

Turn a rule into a condition

20 minEngineering
WHAT YOU WILL DO

Classify a fictional tank reading with explicit boundaries.

A comparison such as level < 20 produces True or False. Use == to compare equality, >= for at least, and <= for at most. A single = still means assignment.

if runs a block when a condition is true. elif checks another condition, and else handles the remaining cases. A colon starts each block, and four spaces show which lines belong to it.

The made-up tank rules are: below 20% is low, above 90% is high, and everything else is normal. These are exercise rules, not real operating limits.

See it in action

example.pyPython 3
# Synthetic tank level and teaching thresholds
level_pct = 18
if level_pct < 20:
    status = "low"
elif level_pct > 90:
    status = "high"
else:
    status = "normal"
print(status)
Keep this in mind

Write the boundary rules before the code, then test values at the boundaries.

LESSON 2 OF 3

Process every value with a for loop

20 minFinance
WHAT YOU WILL DO

Add selected expenses without repeating the calculation by hand.

for value in values: visits each item in a sequence. The indented block runs once per item. The name value refers to the current item.

Start a running total at zero before the loop. total += amount is short for total = total + amount. Put an if inside the loop to include only matching items.

These synthetic transport costs describe student fieldwork. Counting and adding selected costs prepares you for filtering much larger datasets later.

See it in action

example.pyPython 3
# Synthetic fieldwork transport costs in NGN
costs_ngn = [1200, 800, 1500, 600]
large_cost_total = 0
large_cost_count = 0
for cost in costs_ngn:
    if cost >= 1000:
        large_cost_total += cost
        large_cost_count += 1
print("Large costs:", large_cost_count)
print("Total:", large_cost_total)
Keep this in mind

A loop applies the same rule consistently to every item.

LESSON 3 OF 3

Repeat until a target is reached

20 minEnergy
WHAT YOU WILL DO

Model a small charging process with a loop that stops.

while condition: repeats while its condition stays true. Unlike a for loop, it does not need a known list of items.

Update the value used in the condition inside the loop. Otherwise the condition may stay true forever. min(a, b) returns the smaller value and can keep a simulated charge at its target.

This fictional battery gains a fixed amount each hour. Real charging is more complex. Our small model helps you practise state changes and stopping rules.

See it in action

example.pyPython 3
# Synthetic constant-rate charging model
charge_pct = 30
hours = 0
while charge_pct < 80:
    charge_pct = min(charge_pct + 15, 80)
    hours += 1
print("Hours:", hours)
print("Final charge:", charge_pct)
Keep this in mind

Every while loop needs a clear stopping condition and a path toward it.

ABOUT THE LEARNING LAB

Free, open and run by a nonprofit.

This page is part of Python to AI, a free course of 20 modules and 60 lessons in the engineering.fit( ) Learning Lab. The Learning Lab is built and run by engineering.fit( ), a nonprofit founded by Abdullateef Opeyemi Bakare that helps African engineering students gain research experience. You do not need an account or pay a fee to learn.

Course outline · Project lab · Visit engineering-fit.org