16

It's my day 1 of learning python. so it's a noob question for many of you. See the following code:

#!/usr/bin/env python

import sys

def hello(name):
    name = name + '!!!!'
    print 'hello', name

def main():
    print hello(sys.argv[1])


if __name__ == '__main__':
    main()

when I run it

$ ./Python-1.py alice
hello alice!!!!
None

Now, I have trouble understanding where this "None" came from?

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
CuriousMind
  • 33,537
  • 28
  • 98
  • 137

2 Answers2

26

Count the number of print statements in your code. You'll see that you're printing "hello alice!!!" in the hello function, and printing the result of the hello function. Because the hello function doesn't return a value (which you'd do with the return statement), it ends up returning the object None. Your print inside the main function ends up printing None.

Thomas Wouters
  • 130,178
  • 23
  • 148
  • 122
6

Change your

def main():
    print hello(sys.argv[1])

to

def main():
    hello(sys.argv[1])

You are explicitly printing the return value from your hello method. Since you do not have a return value specified, it returns None which is what you see in the output.

Sujoy
  • 8,041
  • 3
  • 30
  • 36