I tried writing this code:
def smaller(x, y):
if x > y:
print(y)
else:
print(x)
print(smaller(2, 3))
I got this result:
>>>
2
None
Where did the None
come from? What does it mean?
See also
The accepted answer explains the importance of return
ing a value from the function, rather than print
ing it. For more information, see What is the purpose of the return statement? How is it different from printing?.
To understand the None
result itself, see What is a 'NoneType' object?.
If you are print
ing inside the function in order to see multiple values, it may be better to instead collect those values so that they can be printed by the calling code. For details, see How can I use `return` to get back multiple values from a loop? Can I put them in a list?.