Editor

Binary Search

Intermediate · Write the code · from 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

From the lesson: 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

Fifty problems, each with a code editor and real tests. Write your solution, press Check, and see exactly which cases pass. Stuck? Hints reveal one at a time, and the solution unlocks once you have checked at least once. Your code and progress are saved in this browser.

Read the lesson

Write binary_search(items, target) for a sorted list. Return the index of target, or -1 if it is not there. Do not use .index() or the bisect module.

def binary_search(items, target):
pass
Solution unlocks after your first Check

What Check runs

Your code runs first, then each of these lines. The output must match exactly.

RunsExpected output
binary_search([1, 3, 5, 7, 9, 11], 7)3
binary_search([1, 3, 5, 7, 9, 11], 4)-1

More practice problems

All Python exercises · 50 practice problems