The text and code are released under the CC0 license; see also the companion project, the Python Data Science Handbook.
文本和代码在 CC0 许可证下发布;另请参阅配套项目 Python 数据科学手册 。
Python is a flexible language, and there are several ways to use it depending on your particular task.
One thing that distinguishes Python from other programming languages is that it is interpreted rather than compiled.
This means that it is executed line by line, which allows programming to be interactive in a way that is not directly possible with compiled languages like Fortran, C, or Java. This section will describe four primary ways you can run Python code: the Python interpreter, the IPython interpreter, via Self-contained Scripts, or in the Jupyter notebook.
Python 是一种灵活的语言,根据您的特定任务,有多种方法可以使用它。Python 与其他编程语言的区别在于它是解释性的,而不是编译的。这意味着它是逐行执行的,这使得编程能够以一种使用 Fortran、C 或 Java 等编译语言无法直接实现的方式进行交互。本节将介绍运行 Python 代码的四种主要方式:Python 解释器 、IPython 解释器 、通过自包含脚本或在 Jupyter 笔记本中运行。
The most basic way to execute Python code is line by line within the Python interpreter.
The Python interpreter can be started by installing the Python language (see the previous section) and typing python
at the command prompt (look for the Terminal on Mac OS X and Unix/Linux systems, or the Command Prompt application in Windows):
执行 Python 代码的最基本方法是在 Python 解释器中逐行执行。可以通过安装 Python 语言(请参阅上一节)并在命令提示符处键入 python
(在 Mac OS X 和 Unix/Linux 系统上查找终端,或在 Windows 中查找命令提示符应用程序)来启动 Python 解释器:
$ python
Python 3.5.1 |Continuum Analytics, Inc.| (default, Dec 7 2015, 11:24:55)
Type "help", "copyright", "credits" or "license" for more information.
>>>
With the interpreter running, you can begin to type and execute code snippets.
Here we'll use the interpreter as a simple calculator, performing calculations and assigning values to variables:
在解释器运行的情况下,您可以开始键入和执行代码片段。在这里,我们将解释器用作一个简单的计算器,执行计算并为变量赋值:
>>> 1 + 1
2
>>> x = 5
>>> x * 3
15
The interpreter makes it very convenient to try out small snippets of Python code and to experiment with short sequences of operations.
解释器可以非常方便地尝试小段 Python 代码和试验短序列作。
If you spend much time with the basic Python interpreter, you'll find that it lacks many of the features of a full-fledged interactive development environment.
An alternative interpreter called IPython (for Interactive Python) is bundled with the Anaconda distribution, and includes a host of convenient enhancements to the basic Python interpreter.
It can be started by typing ipython
at the command prompt:
如果您花大量时间使用基本的 Python 解释器,您会发现它缺少成熟的交互式开发环境的许多功能。一个名为 IPython(用于交互式 Python)的替代解释器与 Anaconda 发行版捆绑在一起,其中包括对基本 Python 解释器的许多便捷增强功能。可以通过在命令提示符下键入 ipython
来启动它:
$ ipython
Python 3.5.1 |Continuum Analytics, Inc.| (default, Dec 7 2015, 11:24:55)
Type "copyright", "credits" or "license" for more information.
IPython 4.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:
The main aesthetic difference between the Python interpreter and the enhanced IPython interpreter lies in the command prompt: Python uses >>>
by default, while IPython uses numbered commands (e.g. In [1]:
).
Regardless, we can execute code line by line just as we did before:
Python 解释器和增强的 IPython 解释器之间的主要美学区别在于命令提示符:Python 默认使用 >>>
,而 IPython 使用编号命令(例如在 [1]:中
)。无论如何,我们可以像以前一样逐行执行代码:
In [1]: 1 + 1
Out[1]: 2
In [2]: x = 5
In [3]: x * 3
Out[3]: 15
Note that just as the input is numbered, the output of each command is numbered as well.
IPython makes available a wide array of useful features; for some suggestions on where to read more, see Resources for Further Learning.
请注意,正如 input 被编号一样,每个命令的输出也被编号。IPython 提供了一系列有用的功能;有关在何处阅读更多内容的一些建议,请参阅进一步学习的资源 。
Running Python snippets line by line is useful in some cases, but for more complicated programs it is more convenient to save code to file, and execute it all at once.
By convention, Python scripts are saved in files with a .py extension.
For example, let's create a script called test.py which contains the following:
在某些情况下,逐行运行 Python 代码段很有用,但对于更复杂的程序,将代码保存到文件中并一次性执行会更方便。按照惯例,Python 脚本保存在扩展名为 .py 的文件中。例如,让我们创建一个名为 test.py 的脚本,其中包含以下内容:
# file: test.py
print("Running test.py")
x = 5
print("Result is", 3 * x)
To run this file, we make sure it is in the current directory and type python
filename
at the command prompt:
要运行此文件,请确保它位于当前目录中,并在命令提示符处键入 python
filename
:
$ python test.py
Running test.py
Result is 15
For more complicated programs, creating self-contained scripts like this one is a must.
对于更复杂的程序,必须创建像这样的自包含脚本。
A useful hybrid of the interactive terminal and the self-contained script is the Jupyter notebook, a document format that allows executable code, formatted text, graphics, and even interactive features to be combined into a single document.
Though the notebook began as a Python-only format, it has since been made compatible with a large number of programming languages, and is now an essential part of the Jupyter Project.
The notebook is useful both as a development environment, and as a means of sharing work via rich computational and data-driven narratives that mix together code, figures, data, and text.
交互式终端和独立脚本的有用混合体是 Jupyter 笔记本 ,这是一种文档格式,允许将可执行代码、格式化文本、图形甚至交互式功能合并到一个文档中。尽管 Notebook 最初是纯 Python 格式,但此后它已与大量编程语言兼容,现在是 Jupyter 项目的重要组成部分。该笔记本既可用作开发环境,也可用作通过丰富的计算和数据驱动叙述(将代码、图形、数据和文本混合在一起)共享工作的一种方式。