NOETRION

Choose a topic

LESSON 5 OF 9 · Python & data foundations
PYTHON BASICS · 4 MIN READ

Conditions and loops: turn a data rule into a repeatable check

Trace an if statement, filter a list, and catch boundary errors before they change your result.

Learning lesson · Reviewed October 2, 2026. Allow additional time for the practice. Prerequisites: variables, lists, and the earlier lesson on summary statistics. Python 3; no extra packages.

What you will learn
  • Translate a written rule into a Boolean condition.
  • Trace a loop one observation at a time.
  • Test the exact boundary and the empty-input case.

A list becomes useful when you can apply the same decision to every item. Imagine a fictional class with scores [48, 60, 75, 59, 90]. The teaching rule is simple: a score of at least 60 passes. Before writing code, settle the boundary: 60 passes, 59 does not. A program can implement the wrong rule perfectly, so this sentence is part of the specification.

Start with one decision

A condition produces a true or false result. An if block runs when its condition is true. In an if/elif/else chain, Python selects the first true branch; the order matters when conditions overlap. Indentation shows which statements belong to a branch. Use consistent four-space indentation so the decision is easy to inspect.

score = 60
if score >= 60:
    label = "pass"
else:
    label = "review"
print(label)  # pass

The operator >= includes equality. Changing it to > would incorrectly send a score of 60 to review. Keep assignment = separate from comparison ==: one stores a value, the other asks whether two values are equal.

Apply the decision to every observation

scores = [48, 60, 75, 59, 90]
passed = []
for score in scores:
    if score >= 60:
        passed.append(score)
print(passed)  # [60, 75, 90]
print(len(passed))  # 3

Read the loop as a sequence of small states. The first observation is 48, so the output list stays empty. The next is 60, so it becomes [60]. By the final observation, it contains three items. The new list preserves the original data; changing the source list during iteration would make this simple trace harder to trust.

ObservationAt least 60?Output after this step
48False[]
60True[60]
75True[60, 75]
59False[60, 75]
90True[60, 75, 90]
Observe
One score
Compare
At least 60?
Collect
Keep matches
Repeat
Next score
Apply the same stated boundary to each observation, preserving the original list.

Count, filter, and calculate are different tasks

Here we filter observations, then count the selected values. If you also want a pass rate, identify the denominator: all five observations, rather than only the three selected ones. This fictional rate is 3 / 5 = 0.6, or 60%. It describes this list only. It does not establish a population-wide success rate or explain why anyone passed.

total = len(scores)
if total == 0:
    print("No scores to summarize")
else:
    print(f"Pass rate: {len(passed) / total:.0%}")  # Pass rate: 60%

An empty list should produce an explicit no-data result. Treating it as 0% would confuse “nobody passed” with “we observed nobody.” A for loop is a good fit when you already have the observations. A while loop fits repetition controlled by a condition, but needs a clear way to stop. Start with for for this bounded dataset.

Practice: introduce a second rule

Create three bands: below 60 is review, 60–79 is pass, and 80–100 is distinction. Assume scores have already been validated as integers from 0 to 100. Predict the labels for 59, 60, 79, and 80 before running your code. Then explain why testing only 75 would miss a boundary mistake.

Show the worked solution
for score in [59, 60, 79, 80]:
    if score >= 80:
        label = "distinction"
    elif score >= 60:
        label = "pass"
    else:
        label = "review"
    print(score, label)
# 59 review
# 60 pass
# 79 pass
# 80 distinction

The highest band is checked first. If the at-least-60 branch came first, 80 would match it before reaching distinction. The boundary examples distinguish inclusive from exclusive comparisons.

Before moving on

Be able to explain the rule, the source list, the selected list, and the denominator without running the program. Keep two checks in your notes: an exact-boundary example and a no-data example. In the next lesson, you will package a calculation into a function so that these checks can be reused.

Sources and further reading

Official sources checked October 2, 2026. Examples, exercises, and rubrics are original Noetrion teaching material; fictional datasets are identified below.

Check your understanding

The rule is “at least 60 passes.” Which condition includes the boundary correctly?

Choose one answer