Sometimes you want to stop the moment you find what you are after. return works in the middle of a loop too: it ends the function there and then. Most tasks with the word "first" are solved this way.
for i in range(len(values)):if values[i] == key:return ireturn -1
len(values) is the number of elements; range(len(values)) counts from 0 to the last index, so you have both the position (i) and the value (values[i]). If the loop ends and you have not returned, what you wanted is not there.out = []for v in values:if v % 2 == 0:out.append(v)return out
[]) and add the elements that qualify to its end with append. The order keeps itself.