# Demo file for Spyder Tutorial
# Hans Fangohr, University of Southampton, UK
def hello():
"""Print "Hello World" and return None."""
print("Hello World")
# Main program starts here
hello()
Please create a new file in the Spyder Editor pane (with File ‣ New file, or Ctrl-N/ Command-N). Then copy and paste the code inside the box above into the file, and save it with the name hello.py.
To execute the program, select Run ‣ Run from the menu (or press F5), and confirm the Run settings if required.
If this is your first time running something, you should see an output like this:
In [1]: runfile('/File/Path/hello.py', wdir=r'/File/Path')
Hello World
In [2]:
注意:在runfile()中显示的特定路径将取决于您保存文件的位置,但这是Spyder自动插入的。
Python会逐行读取文件,而忽略注释(即以#符号开头的行)。
当遇到def关键字时,它知道在此行和下一行(一个或多个)中定义了一个函数。 def hello()之后的所有缩进行均属于函数主体。
请注意,此时已在文件中创建了函数对象,但尚未调用该函数(即未执行)。
当Python解释器遇到写在最左侧列中的命令(除def …和一些其他关键字之外)时,它将立即执行这些命令。
在hello.py文件中,只有读取hello()的行会实际调用(即执行)名称为hello的函数。
如果从程序中注释或删除行hello()并再次运行整个文件(通过按F5或选择Run‣Run),将不会打印任何内容(因为已定义但未调用hello函数,即未执行&#x