Why does Python print "No" in the output?

I defined the function as follows:

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

However, why the output returns None :

 The very first line None 
+5
source share
2 answers

Because there are two print statements . First, it is inside the function, and the second is outside the function. When a function does not return any thing, it returns None.

Use the return at the end of the function to return a value.

eg:.

Do not return a 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 >>> 
+10
source

Due to double print function. I suggest you use return instead of print inside the function definition.

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

OR

 def lyrics(): print "The very first line" lyrics() 
+3
source

Source: https://habr.com/ru/post/1214485/


All Articles