Before this lesson

Understand Python variables, integers, strings and print.

What you will be able to do

Translate rules into branches and handle empty data and boundaries deliberately.

Understand the mechanism

An if/elif chain takes the first matching branch. Order grade thresholds from highest to lowest. Comparisons produce booleans, while short-circuit operators can return operands rather than only bool values.

A for loop consumes iterable values and range excludes its endpoint. enumerate supplies positions with values. continue skips an iteration remainder and break exits the current loop, not every enclosing operation.

Give accumulators explicit invariants: total is the accepted sum and count the accepted count. Only divide when nonempty. None represents missing data without confusing it with a legitimate zero average.

While loops need state that moves toward termination. Diagnose their condition and update instead of adding arbitrary breaks that conceal incorrect logic.

Runnable example

Use Python 3.11 or later. Save lesson.py in a separate practice directory and run python3 lesson.py, or py lesson.py on Windows. Examples use only the standard library.

scores = [0, 60, 90, 100, -1, 101]
total = 0
count = 0
for score in scores:
    if not 0 <= score <= 100:
        continue
    total += score
    count += 1
average = total / count if count else None
print(total, count, average)
for position, score in enumerate(scores[:3], start=1):
    print(position, score)

Trace and verify

Expect 250, four and 62.5, followed by positions starting at one. Empty input yields None while all-zero valid scores yield 0.0. These are different states.

Common mistakes and boundaries

The example assumes numeric entries; validate external data first. Deleting while iterating can skip elements, so build a filtered collection instead.

Practice challenge

Add grading for missing, A, B and C states and test values around both thresholds.

Reference solution and reasoning

Handle None before descending numeric thresholds. Boundary tests catch strict-versus-inclusive comparison mistakes.

Further reading