I am trying to understand why the second function with the return inside the conditionals, will only loop once. l = [1,2,3,45,6,7] First working example with print:
def xyz(l):
for i in l:
if i==7:
print('7 found')
else:
print('7 not found')
xyz(l)
Output:
7 not found
7 not found
7 not found
7 not found
7 not found
7 found
Now the function with return statement:
def xyz(l):
for i in l:
if i==7:
return '7 found'
else:
return '7 not found'
Result:
'7 not found'
The loop runs only once for the first element and returns the else value. Please don`t downvote this, this is a thing I need to understand before continuing to learn python. Does the return statement exit the loop?
Thanks in advance