BeginnerCoder

Lesson 3 — Linear search

Linear search: check every item

Linear search looks at items one by one until it finds the target (the value you are looking for). If it reaches the end, the target is not there, and by convention we return −1. enumerate(nums) gives you both the index (position, starting at 0) and the value.

for i, n in enumerate([5, 8]):
    print(i, n)   # 0 5, then 1 8

It is simple, works on any list (sorted or not), and takes up to n steps for n items — we say it is O(n), "grows in step with the list size".

Why it matters: it is the baseline every faster search is compared against.

Want it another way?
Task

Inside the loop, return i when n equals target. Expected: 2, then -1.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.