60

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
martineau
  • 119,623
  • 25
  • 170
  • 301
def_0101
  • 783
  • 1
  • 7
  • 9

2 Answers2

95

Because there are two print statements. First is inside function and second is outside function. When a function doesn't return anything, it implicitly returns None.

Use return statement at end of function to return value.

e.g.:

Return None.

>>> 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
>>> 
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56
  • Hey can you tell me what is wrong the code? it's only returning last digit def another(n): rev = 0 while(n>=0): rem=n%10 rev = (10*rev)+ n%10 n //= 10 return rev print another(2154) output:4 – def_0101 Mar 05 '15 at 08:13
  • @def_0101: ok, What you are doing? reverse number?? – Vivek Sable Mar 05 '15 at 08:30
  • yes reverse integers – def_0101 Mar 05 '15 at 08:31
  • 1
    @def_0101: `def another(n): rev = 0 while(n!=0): rev = rev*10 + n%10 n //= 10 return rev ` check .. adding explanation in next comment. – Vivek Sable Mar 05 '15 at 08:40
  • 1
    @def_0101: For Reverse, we need to get digit from number by last to first and crate new number by adding these digit with multiple of 0, 10, 100, 1000... 1. How to get last digit: Use `%` operation to get last digit that you done. 2. Decrease input number by `/`dividing 10, this also you done. 3. Create reverse number by add result of `%` operation to reverse number which is multiple of 10. Let me know – Vivek Sable Mar 05 '15 at 08:52
  • still it's not working Thanks for the explanation i understood the logic behind reversing integer actually there might be a fault of while loop – def_0101 Mar 05 '15 at 08:53
  • @def_0101: email me your code vivekbsable@gmail.com.. Check where you write `return` statement? it should be outside of `while` loop. – Vivek Sable Mar 05 '15 at 08:56
  • [Here is the pastebin link](http://pastebin.com/M8ZgqQAs) well it is pretty inside the loop – def_0101 Mar 05 '15 at 09:03
  • @def_0101: `return rev` must be outside of while loop. Code is returning from the while because return is inside while loop.. – Vivek Sable Mar 05 '15 at 09:06
  • SyntaxError: return outside function Lol – def_0101 Mar 05 '15 at 09:08
  • @def_0101 :) http://pastebin.com/VrLjuDuq Check this – Vivek Sable Mar 05 '15 at 09:14
  • worked flawlessly ! actually i wrote it outside the function well I gotta practice hard to get over with indentation errors Thanks again – def_0101 Mar 05 '15 at 09:27
19

Because of double print function. I suggest you to 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()
D Malan
  • 10,272
  • 3
  • 25
  • 50
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274