如何在代码输出旁边显示我的代码输入,就像我在*答案中看到的那样?

时间:2021-12-27 08:09:48

I am currently learning to program with Python, and I've been making notes in a text editor as I read books, etc. Is there an easy way to create "code snippets" similar to the ones I see in * answers like this:

我目前正在学习用Python编程,当我阅读书籍等时,我一直在文本编辑器中做笔记。是否有一种简单的方法来创建“代码片段”,类似于我在*中看到的答案,如下所示:

http://oi61.tinypic.com/2e0mpsg.jpg

What I like is that I can see the output from the code in a different color, directly beneath the input. I intend to take these code snippets and throw them in a pdf (or online with http://hilite.me/) for future reference.

我喜欢的是,我可以直接在输入下方以不同的颜色看到代码的输出。我打算将这些代码片段放在pdf中(或在http://hilite.me/在线)以供将来参考。

3 个解决方案

#1


Open your interpreter by typing python into your shell:

通过在bash shell中键入python打开python解释器:

bash-3.2$ python
Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = [x*x for x in range(3)]
>>> for i in mylist:
...     print i
... 
0
1
4
>>> exit()
bash-3.2$ 

#2


Python on the command line is great as user1823 pointed out. Since you mentioned saving your snippets to pdf I would suggest you look at IPython Notebook. It is just like running from the command line, but it runs in your web browser.

用户1823指出,命令行上的Python非常棒。既然你提到将你的片段保存为pdf我会建议你看一下IPython Notebook。它就像从命令行运行,但它在您的Web浏览器中运行。

You type some code into a cell, run it, save the code and output. When you exit, the notebook saves the state of your work (as a .ipynb file) so you can pick up where you left off. You can also export it as a pdf.

您将一些代码键入单元格,运行它,保存代码和输出。当您退出时,笔记本会保存您的工作状态(作为.ipynb文件),以便您可以从中断的地方继续。您也可以将其导出为pdf。

You can include LaTeX and other markup in your notebook. You can share it with others and they can pick up where ever you last saved.

您可以在笔记本中包含LaTeX和其他标记。您可以与他人分享,他们可以在您上次保存的地方领取。

On my mac I launch Ipython Notebook from Anaconda, which is a Python distribution that comes bundled with many popular packages (SciPy, Numpy, BeautifulSoup, 300 others).

在我的Mac上,我从Anaconda启动Ipython Notebook,这是一个Python发行版,捆绑了许多流行的软件包(SciPy,Numpy,BeautifulSoup,另外300个)。

#3


One nice tool that you can use is IPython Notebook. With that you can combine code execution, rich text, mathematics, plots and rich media as said in IPython website.

您可以使用的一个很好的工具是IPython Notebook。有了它,您可以将代码执行,富文本,数学,情节和富媒体结合起来,如IPython网站所述。

What it all means? You can have a fancy way to store and share your snippets. You can use rich text to explain your snippets. More than that, you can run your snippet and watch it dynamically, observing what it does in fact.

这一切意味着什么?您可以通过一种奇特的方式来存储和共享您的代码段。您可以使用富文本来解释您的代码段。更重要的是,您可以运行您的代码段并动态观察它,观察它实际上做了什么。

Take a look in the introductory video, also from IPython website.

看看介绍视频,也来自IPython网站。

IPython Notebook is part of IPython package which you can install with pip. For more information or other ways to get it, take a look at IPython documentation.

IPython Notebook是IPython包的一部分,您可以使用pip进行安装。有关更多信息或其他方法,请查看IPython文档。

#1


Open your interpreter by typing python into your shell:

通过在bash shell中键入python打开python解释器:

bash-3.2$ python
Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = [x*x for x in range(3)]
>>> for i in mylist:
...     print i
... 
0
1
4
>>> exit()
bash-3.2$ 

#2


Python on the command line is great as user1823 pointed out. Since you mentioned saving your snippets to pdf I would suggest you look at IPython Notebook. It is just like running from the command line, but it runs in your web browser.

用户1823指出,命令行上的Python非常棒。既然你提到将你的片段保存为pdf我会建议你看一下IPython Notebook。它就像从命令行运行,但它在您的Web浏览器中运行。

You type some code into a cell, run it, save the code and output. When you exit, the notebook saves the state of your work (as a .ipynb file) so you can pick up where you left off. You can also export it as a pdf.

您将一些代码键入单元格,运行它,保存代码和输出。当您退出时,笔记本会保存您的工作状态(作为.ipynb文件),以便您可以从中断的地方继续。您也可以将其导出为pdf。

You can include LaTeX and other markup in your notebook. You can share it with others and they can pick up where ever you last saved.

您可以在笔记本中包含LaTeX和其他标记。您可以与他人分享,他们可以在您上次保存的地方领取。

On my mac I launch Ipython Notebook from Anaconda, which is a Python distribution that comes bundled with many popular packages (SciPy, Numpy, BeautifulSoup, 300 others).

在我的Mac上,我从Anaconda启动Ipython Notebook,这是一个Python发行版,捆绑了许多流行的软件包(SciPy,Numpy,BeautifulSoup,另外300个)。

#3


One nice tool that you can use is IPython Notebook. With that you can combine code execution, rich text, mathematics, plots and rich media as said in IPython website.

您可以使用的一个很好的工具是IPython Notebook。有了它,您可以将代码执行,富文本,数学,情节和富媒体结合起来,如IPython网站所述。

What it all means? You can have a fancy way to store and share your snippets. You can use rich text to explain your snippets. More than that, you can run your snippet and watch it dynamically, observing what it does in fact.

这一切意味着什么?您可以通过一种奇特的方式来存储和共享您的代码段。您可以使用富文本来解释您的代码段。更重要的是,您可以运行您的代码段并动态观察它,观察它实际上做了什么。

Take a look in the introductory video, also from IPython website.

看看介绍视频,也来自IPython网站。

IPython Notebook is part of IPython package which you can install with pip. For more information or other ways to get it, take a look at IPython documentation.

IPython Notebook是IPython包的一部分,您可以使用pip进行安装。有关更多信息或其他方法,请查看IPython文档。