Binary search in python don't and the use of -1 or +1


#1

While learning binary search, I found this code. Still in the absence of iteration, I don’t understand the use of -1 in last = len(myList)-1 (no next value anyways) and also the use of -1 & +1 downthere The code work just fine without all of em …
Am I missing something ?


search = 2
myList = [1, 2, 3, 4, 5, 6]

first = 0
last = len(myList)-1
found = False

while first <= last and not found:

middle = (first + last) / 2
if myList[middle] == search:
	found = True
else:
	if search < myList[middle]:
		last = middle -1
	else:
		first = middle +1

if found:
print(“Found the search number.”)
else:
print(“Did not find the search number.”)


#2

last is the last physical index of the list.

myList[last]  =>  6