Why is this printing 'None' in the output?

def_0101 picture def_0101 · Mar 2, 2015 · Viewed 119.1k times · Source

I have defined a function as follows:

def lyrics():
    print "The very first line"
print lyrics()

However why does the output return None:

The very first line
None

Answer

Vivek Sable picture Vivek Sable · Mar 2, 2015

Because there are two print statements. First is inside function and second is outside function. When function not return any thing that time it return None value.

Use return statement at end of function to return value.

e.g.:

Return None value.

>>> def test1():
...    print "In function."
... 
>>> a = test1()
In function.
>>> print a
None
>>> 
>>> print test1()
In function.
None
>>>
>>> test1()
In function.
>>> 

Use return statement

>>> def test():
...   return "ACV"
... 
>>> print test()
ACV
>>> 
>>> a = test()
>>> print a
ACV
>>>