-3

When I run this following code:

def foo():
  for n in range(0, 10):
      return('bar', n)


print(foo())

it prints:

('bar', 0)

When I run the exact same code but replace the 'return' with 'print'

def foo():
    for n in range(0, 10):
        print('bar', n)


print(foo())

it prints the following:

bar 0
bar 1
bar 2
bar 3
bar 4
bar 5
bar 6
bar 7
bar 8
bar 9
None

in the code where I use return, shouldn't it give the results same as above because I'm returning every n?

1 Answers1

0

Because when it reaches the first return, the function ends.

For this reason, inside the loop and in the first iteration, it run the Return command and the function ends.