IB Computer Science HL

B.2 Programming

Write and test Python programs

Use functions, conditions, iteration, and zero-based sequence indexing.

Examples use Python. HL includes additional lessons beyond the SL path.

Make the algorithm explicit

A function groups a calculation under a name and receives input through parameters. The example counts values at or above a threshold. The local count starts at zero on each call, and return passes the result back without printing it.

Python sequences use zero-based indexing. A for-each traversal is useful when only the value matters; use an index when the position is needed. Indentation defines the body of a condition, loop, or function.

def count_at_least(values, threshold):
    count = 0
    for value in values:
        if value >= threshold:
            count += 1
    return count

print(count_at_least([40, 50, 80], 50))

Choose tests that expose mistakes

For this function, test an empty list, no qualifying values, all qualifying values, and a value equal to the threshold. The equality case distinguishes >= from >. State the expected result before running the program.

Separate input and output from the calculation so the same algorithm is easy to test with different values. If reading a file, define how malformed records and missing data will be handled before including them in the analysis.

Check yourself

What should count_at_least([], 50) return?Show answer

0. There are no values to count, and the initialized total is returned.

References used to build these lessons

We used these official documents as references when building this course's lessons. They are the sources for the syllabus structure and course requirements.

International Baccalaureate · First assessment 2027

This PDF is the public IB subject brief. The complete subject guide and assessment requirements are available through your school or the IB Programme Resource Centre.