Big O Notation

Big O is the shorthand for describing that growth. It deliberately ignores constants and small details, because at large n only the shape matters.

O(1) constant, indexing a list, checking a dictionary key

O(log n) logarithmic, binary search

O(n) linear, one loop through the data

O(n log n) the speed of good sorting algorithms

O(n²) quadratic, a loop inside a loop

O(2ⁿ) exponential, effectively unusable beyond tiny inputs

Reading it is simple: find the most expensive part of the code, and describe how it grows.

Two loops one after the other is still O(n), because you pass the data twice and 2n is still linear growth. Two loops nested inside each other is O(n²). Sequence adds, nesting multiplies.

EXAMPLE
loading editor…
YOUR TURN
Your turn → Change items to ten numbers and run it, then to twenty. Watch how fast the nested count grows compared with the sequential one.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Making Code Faster →
← Back