Jupyter 简明教程
IPython - Getting Started
本章将解释如何开始使用 IPython。
This chapter will explain how to get started with working on IPython.
Starting IPython from Command Prompt.
在深入了解 IPython 之前,请注意代替常规 >>> ,您将看到两个主要的 Python 提示,如下所述 −
Before proceeding to understand about IPython in depth, note that instead of the regular >>>, you will notice two major Python prompts as explained below −
-
In[1] appears before any input expression.
-
Out[1] appears before the Output appears.
此外,方括号中的数字自动递增。观察以下屏幕截图以更好地理解 −
Besides, the numbers in the square brackets are incremented automatically. Observe the following screenshot for a better understanding −
现在,如果您安装了 Python 的 Anaconda 分发,请从开始菜单中打开 Anaconda 提示符。
Now, if you have installed Anaconda distribution of Python, open Anaconda prompt from start menu.
Start IPython from conda prompt
与常规 Python 控制台相比,我们会注意到一个区别。IPython 外壳通过对不同的元素(如表达式、函数、变量等)使用不同的颜色方案显示语法高亮。
When compared to regular Python console, we can notice a difference. The IPython shell shows syntax highlighting by using different colour scheme for different elements like expression, function, variable etc.
另一个有用的增强功能是制表符自动补全。我们知道每个对象具有一个或多个方法,如其类中定义的那样。当您在对象前面的点后按 Tab 键时,IPython 会弹出适当的方法列表。
Another useful enhancement is tab completion. We know that each object has one or more methods available as defined in its class. IPython pops up appropriate list of methods as you press tab key after dot in front of object.
在下面的示例中,定义了一个字符串。响应该操作,显示了字符串类的所有方法。
In the following example, a string is defined. As a response, the methods of string class are shown.
通过在它的前面输入“?”,IPython 提供任何对象的详细信息。其中包括文档字符串、函数定义和类的构造函数详细信息。例如,要浏览上面定义的字符串对象 var,请在输入提示符中输入 var? 。结果将显示有关其所有详细信息。观察以下屏幕截图以更好地理解 −
IPython provides information of any object by putting ‘?’ in front of it. It includes docstring, function definitions and constructor details of class. For example to explore the string object var defined above, in the input prompt enter var?. The result will show all information about it. Observe the screenshot given below for a better understanding −
Magic Functions
IPython 的内置魔术函数极其强大。有两种类型的魔术函数。
IPython’s in-built magic functions are extremely powerful. There are two types of magic functions.
-
Line magics, which work very much like DOS commands.
-
Cell magics, which work on multiple lines of code.
我们将在后续章节中详细了解行魔术函数和单元格魔术函数。
We shall learn about line magic functions and cell magic functions in detail in subsequent chapters.