Python:打印一个简单的功能

see*_*ker -1 python function python-2.7

我是Python和编程的绝对初学者,我刚刚接触过函数.

我在下面定义了两个简单的函数:

def output1():
   print "Hello, world!"

def output2():
   print "Hello, there!"

output1()
output2()
Run Code Online (Sandbox Code Playgroud)

将上面的内容保存到一个名为function.py的脚本后,我使用windows power shell运行脚本,并按照您的预期打印以下内容:

Hello, world!
Hello, there!
Run Code Online (Sandbox Code Playgroud)

但是当我将脚本修改为:

def output1():
   print "Hello, world!"

def output2():
   print "Hello, there!"

print output1()
print output2()
Run Code Online (Sandbox Code Playgroud)

它打印:

Hello, world!
None
Hello, there!
None
Run Code Online (Sandbox Code Playgroud)

出于好奇,当我将output1和output2作为print前缀时,为什么会这样做呢?

Sim*_*ser 5

一个函数可以有一个返回值:你调用该函数并将它返回到调用它的位置.

None默认情况下,函数在Python中返回.

现在你还打印的返回值output1output2.

您可以在Python语言教程的这一部分中阅读更多相关内容.