首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Python•CHAPTER TWO

【导言】《Python Tutorial, Release 3.6.4》是适合Python3.6.4语言版本的教程,该教程英文原版来自Python官网,内容权威,版本最新,注重基础,示例代码准确无误,学习高效快捷,非常适合Python编程初学者入门。

该教程英文原版版权属于Python官网 https://www.python.org/ 所有,从英文原版译出的中文版权归译者华哥所有。

该教程发布形式:前部分为英文原文,后部分为对应的中文译文。

该教程英文原版包括16章和附录共150多页,将分期连续发布,敬请期待!

该教程《第二章 使用Python解释器》的译文分成二部分,将分二次连续发布,这是第一部分(1 / 2)。

CHAPTER TWO USING THE PYTHON INTERPRETER

2.1 Invoking the Interpreter

The Python interpreter is usually installed as /usr/local/bin/python3.6 on those machines where it is available; putting /usr/local/bin in your Unix shell’s search path makes it possible to start it by typing the command:python3.6 to the shell. Since the choice of the directory where the interpreter lives is an installation option, other places are possible; check with your local Python guru or system administrator. (E.g., /usr/local/python is a popular alternative location.)

On Windows machines, the Python installation is usually placed in C:\Python36, though you can change this when you’re running the installer. To add this directory to your path, you can type the following command into the command prompt in a DOS box: set path=%path%;C:\python36 .

Typing an end-of-file character (Control-D on Unix,Control-Z on Windows) at the primary prompt causes the interpreter to exit with a zero exit status. If that doesn’t work, you can exit the interpreter by typing the following command: quit().

The interpreter’s line-editing features include interactive editing, history substitution and code completion on systems that support readline. Perhaps the quickest check to see whether command line editing is supported is typing Control-P to the first Python prompt you get. If it beeps, you have command line editing; see AppendixInteractive Input Editing and History Substitutionfor an introduction to the keys. If nothing appears to happen, or if ^P is echoed, command line editing isn’t available; you’ll only be able to use backspace to remove characters from the current line.

The interpreter operates somewhat like the Unix shell: when called with standard input connected to a tty device, it reads and executes commands interactively; when called with a file name argument or with a file as standard input, it reads and executes a script from that file.

A second way of starting the interpreter is python-c command [arg] ..., which executes the statement(s) incommand, analogous to the shell’s -c option. Since Python statements often contain spaces or other characters that are special to the shell, it is usually advised to quotecommandin its entirety with single quotes.

Some Python modules are also useful as scripts. These can be invoked using python -m module [arg] ..., which executes the source file for module as if you had spelled out its full name on the command line. When a script file is used, it is sometimes useful to be able to run the script and enter interactive mode afterwards. This can be done by passing -i before the script.

All command line options are described in using-on-general.

第二章 使用Python解释器

2.1 调用解释器

Python解释器通常以/usr/local/bin/python3.6这样的形式安装在可用的机器上。把/usr/local/bin放进Unix shell的搜索路径,就可以通过在shell中键入命令:python3.6来启动Python解释器。因为选择解释器所在的目录是一个安装选项,所以还可以选择其他位置;请与本地python专家或系统管理员联系(例如:/usr/local/python是一个通用的备选位置)。

在Windows设备上,Python一般被安装在C:\Python36,尽管你在运行安装时可以改变这个。若要将此目录添加到路径中,可以键入下列命令到DOS框里的命令提示符中:set path=%path%;C:\python36。

在主提示符处输入文件结束字符(在Unix上按Control-D,在Windows 上按Control-Z)会导致解释器发生零退出状态退出。如果没有反应,可以通过输入下列命令:quit()退出解释器。

解释器的行编辑功能包括交互编辑、历史替代和在支持读行系统上的代码完成。大概看看是否支持命令行编辑的最快检查方式是,输入Control-P到你所得到的第一个Python提示符中。如果发出嘟嘟声,说明具有命令行编辑功能:参看关键指引附录——交互输入编辑和历史替代。如果好像没有什么发生,或者如果发出^P 的回声,那么命令行编辑功能不可用:你将只能使用退格键从当前行中移除字符。

解释器操作有点像Unix shell:当调用标准输入链接终端设备时,它以交互方式读取和执行命令;当调用文件名参数或以文件作为标准输入时,它读取和执行来自该文件的脚本。

启动解释器的第二种方法是python-c命令[arg]...,它执行命令中的语句,类似于shell的-c选项。由于Python语句通常包含对于shell是特殊的空格或其他符号,所以一般建议使用单引号完整地引用命令。

一些python模块作为脚本也很有用。这些可以使用python-m模块[arg] ...调用,它执行模块的源文件,就好像你已经在命令行上拼出了它的全名一样。当使用脚本文件时,有时能够运行脚本并随后进入交互模式是有用的。这可以通过在脚本之前传递-i来完成。

所有命令行选项都在“一般使用”中说明。

未完待续

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180201G0ASIB00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券