I was watching python lecture by Raymond Hettinger on youtube. And he showed the right way to exit for loop:
def find(seq, target):
for i, value in enumerate(seq):
if value == target:
break
else:
return -1
return i
I dont get why bother with else statement and not just do:
def find(seq, target):
for i, value in enumerate(seq):
if value == target:
return i
return -1
Am I missing something or it is just good idea to sometimes add this else/break statement for whatever reason?