Content · Programming · Python

Python Basic Algorithm Analysis: Count Operations, Not Seconds

Estimate how running time grows as the input grows instead of relying on one stopwatch result.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Estimate how running time grows as the input grows instead of relying on one stopwatch result.

What You Will Learn


Big O describes the dominant growth of work for a family of input sizes. Count operations in loops and repeated scans, and ignore constant factors only when comparing growth rates. Small inputs may still favor a simpler implementation. Distinguish average cases and worst cases where appropriate.

Time complexity assumes a specific input structure. Include setup work such as sorting or building a lookup table when comparing complete solutions.

Step-by-Step Example


The example below makes the behavior visible with small input values that you can change safely.

def contains_linear(values, target):
    for item in values:
        if item == target:
            return True
    return False

print(contains_linear([3, 5, 8, 10], 8))
print(contains_linear([3, 5, 8, 10], 9))

How the Example Works


  • The function contains_linear defines reusable behavior; its body executes when it is called, not when it is declared.
  • The print() calls make the return values or state visible so you can compare them with the explanation and check your prediction.

Expected output from the example:

True
False

When You Would Use This


This technique matters when a program handles larger queues, sorted records, or repeated lookups. Choose the data structure based on actual operations and the preconditions the algorithm requires. Verify boundary inputs before optimizing for speed.

Practice: Modify the Code


Count comparisons when searching for a missing item in lists of length 5 and 50.

Verify the changed output against the behavior described above, including the relevant invalid or boundary case.

For complete parameters and version details, consult the official Python documentation.

Common Beginner Mistakes


  • Calling an algorithm O(1) because one very small test finished quickly.
  • Confusing the cost of a single dictionary lookup with repeatedly rebuilding a dictionary from scratch.

Key Takeaways


  • Estimate how running time grows as the input grows instead of relying on one stopwatch result.
  • Big O describes the dominant growth of work for a family of input sizes.
  • Apply this operation only after checking the assumptions and input conditions shown in the example.
Share this resourceSend this page to someone who may find it useful.