Python: Linear Search
Finding data in a list by checking one-by-one.
The Hook
Finding a song in a playlist.
You scroll down, checking each title one-by-one until you find it.
This is Linear Search. Start at index 0, check, move, check, move...
Worked Example
Finding Nemo in a fish tank.
Predict Output
What happens to the loop when it finds a match?
for x in nums:
if x > 25:
print(x)
break
Golden Ticket
"Search for 'Gold' in the list. If found, print 'Winner!'."
Waldo Filter
"Stop the noise! Remove the else block so it only prints when Waldo is found."
Admin Counter
Independent Search Pattern
- Target List:
["Admin", "Guest", "Root", "Admin"]. - Initialize
count = 0. - Loop and increment
countwhenever "Admin" is found. - Print results (Expected: 2).