On Sat, Mar 25, 2017 at 3:31 PM, Peter O'Doherty wrote:

>
> def myFunc(num):
>     for i in range(num):
>         print(i)
>
> print(myFunc(4))
> 0
> 1
> 2
> 3
> None #why None here?
>
> Because there are two print() functions, one inside the function and
another outside.  When a function does not return anything, an implicit
“return None” statement gets added to the end of the function.


The inner myFunc(4) is printing out 0, 1, 2, 3 and it also returns None
which gets printed by the outer print() function.

>
> def myFunc(num):
>     for i in range(num):
>         return i
>
> print(myFunc(4))
> 0 #why just 0?


The return statement causes the loop to stop and the function to exit and
returns 0 to its caller which is the outer print() function.

Hope that helps.

Sri


>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to