⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node4.html

📁 python的入门教程,讲得挺不错的,有一些实例
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//CN"><html><head><link rel="STYLESHEET" href="style.css" type='text/css' /><link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /><link rel='start' href='../index.html' title='Python Documentation Index' /><link rel="first" href="tut.html" title='Python Tutorial' /><link rel='contents' href='node2.html' title="Contents" /><link rel='last' href='about.html' title='About this document...' /><link rel='help' href='about.html' title='About this document...' /><link rel="next" href="node5.html" /><link rel="prev" href="node3.html" /><link rel="parent" href="tut.html" /><link rel="next" href="node5.html" /><meta name='aesop' content='information' /><title>2. 使用Python解释器 Using the Python Interpreter </title></head><body><DIV CLASS="navigation"><div id='top-navigation-panel' xml:id='top-navigation-panel'><table align="center" width="100%" cellpadding="0" cellspacing="2"><tr><td class='online-navigation'><a rel="prev" title="1. 开胃菜 Whetting Your"  href="node3.html"><img src='../icons/previous.png'  border='0' height='32'  alt='Previous Page' width='32' /></A></td><td class='online-navigation'><a rel="parent" title="Python Tutorial"  href="tut.html"><img src='../icons/up.png'  border='0' height='32'  alt='Up One Level' width='32' /></A></td><td class='online-navigation'><a rel="next" title="3. Python简介 An Informal"  href="node5.html"><img src='../icons/next.png'  border='0' height='32'  alt='Next Page' width='32' /></A></td><td align="center" width="100%" class='online-navigation'>Python Tutorial</td><td class='online-navigation'><a rel="contents" title="Table of Contents"  href="node2.html"><img src='../icons/contents.png'  border='0' height='32'  alt='Contents' width='32' /></A></td><td class='online-navigation'><img src='../icons/blank.png'  border='0' height='32'  alt='' width='32' /></td><td class='online-navigation'><img src='../icons/blank.png'  border='0' height='32'  alt='' width='32' /></td></tr></table><div class='online-navigation'><b class="navlabel">Previous:</b><a class="sectref" rel="prev" href="node3.html">1. 开胃菜 Whetting Your</A><b class="navlabel">Up:</b><a class="sectref" rel="parent" href="tut.html">Python Tutorial</A><b class="navlabel">Next:</b><a class="sectref" rel="next" href="node5.html">3. Python简介 An Informal</A></div><hr /></div></DIV><!--End of Navigation Panel--><div class='online-navigation'><!--Table of Child-Links--><A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a><UL CLASS="ChildLinks"><LI><A href="node4.html#SECTION004100000000000000000">2.1 调用解释器 Invoking the Interpreter</a><UL><LI><A href="node4.html#SECTION004110000000000000000">2.1.1 参数传递 Argument Passing</a><LI><A href="node4.html#SECTION004120000000000000000">2.1.2 交互模式 Interactive Mode</a></ul><LI><A href="node4.html#SECTION004200000000000000000">2.2 解释器及其环境 The Interpreter and Its Environment</a><UL><LI><A href="node4.html#SECTION004210000000000000000">2.2.1 错误处理 Error Handling</a><LI><A href="node4.html#SECTION004220000000000000000">2.2.2 执行Python脚本 Executable Python Scripts</a><LI><A href="node4.html#SECTION004230000000000000000">2.2.3 源程序编码 Source Code Encoding</a><LI><A href="node4.html#SECTION004240000000000000000">2.2.4 交互式环境的启动文件 The Interactive Startup File</a></ul></ul><!--End of Table of Child-Links--></div><HR><H1><A NAME="SECTION004000000000000000000"></A><A NAME="using"></A><BR>2. 使用Python解释器 Using the Python Interpreter </H1><P><H1><A NAME="SECTION004100000000000000000"></A><A NAME="invoking"></A><BR>2.1 调用解释器 Invoking the Interpreter </H1><P>The Python interpreter is usually installed as
<span class="file">/usr/local/bin/python</span> on those machines where it is
available; putting <span class="file">/usr/local/bin</span> in your <span class="Unix">Unix</span> shell's
search path makes it possible to start it by typing the command<P>通常 Python 的解释器被安装在目标机器的
<span class="file">/usr/local/bin/python</span> 目录下;把 <span class="file">/usr/local/bin</span>
目录放进你的<span class="Unix">Unix</span> Shell 的搜索路径里,确保它可以通过输入<P><div class="verbatim"><pre>
python</pre></div><P>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., <span class="file">/usr/local/python</span> is a popular
alternative location.)<P>来启动。因为安装路径是可选的,所以也有可能安装在其它位置,你可以与安装
Python
的用户或系统管理员联系。(例如,<span class="file">/usr/local/python</span>就是一个很常见的选择)<P>Typing an end-of-file character (<kbd>Control-D</kbd> on <span class="Unix">Unix</span>,
<kbd>Control-Z</kbd> 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
commands: "<tt class="samp">import sys; sys.exit()</tt>".<P>输入一个文件结束符(<span class="Unix">Unix</span>
上是<kbd>Ctrl+D</kbd>,Windows上是<kbd>Ctrl+Z</kbd>)解释器会以0值退出(就是说,没有什么错误,正常退出--译者)。如果这没有起作用,你可以输入以下命令退出:"<tt class="samp">import
sys; sys.exit()</tt>"。<P>The interpreter's line-editing features usually aren't very
sophisticated.  On <span class="Unix">Unix</span>, whoever installed the interpreter may
have enabled support for the GNU readline library, which adds more
elaborate interactive editing and history features. 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 Appendix <A HREF="node14.html#interacting"><tex2html_cross_ref_visible_mark></A> for
an introduction to the keys.  If nothing appears to happen, or if
<code>P</code> is echoed, command line editing isn't available; you'll
only be able to use backspace to remove characters from the
current line.<P>解释器的行编辑功能并不很复杂。装在<span class="Unix">Unix</span>上的解释器可能会有GNU
readline
库支持,这样就可以额外得到精巧的交互编辑和历史记录功能。可能检查命令行编辑器支持能力最方便的方式是在主提示符下输入Ctrl-P。如果有嘟嘟声(计算机扬声器),说明你可以使用命令行编辑功能,从附录
A <A HREF="node14.html#interacting"><tex2html_cross_ref_visible_mark></A>
可以查到快捷键的介绍。如果什么也没有发声,或者<code>P</code>显示了出来,说明命令行编辑功能不可用,你只有用退格键删掉输入的命令了。<P>The interpreter operates somewhat like the <span class="Unix">Unix</span> 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 <em>script</em> from
that file.<P>解释器的操作有些像 <span class="Unix">Unix</span>
Shell:使用终端设备做为标准输入来调用它时,解释器交互的解读和执行命令,通过文件名参数或以文件做为标准输入设备时,它从文件中解读并执行<em>脚本</em>。<P>A second way of starting the interpreter is
"<tt class="samp"><b class="program">python</b> <b class="programopt">-c</b> <var>command</var> [arg] ...</tt>", which
executes the statement(s) in <var>command</var>, analogous to the shell's
<b class="programopt">-c</b> option.  Since Python statements often contain spaces
or other characters that are special to the shell, it is best to quote
<var>command</var> in its entirety with double quotes.<P>启动解释器的第二个方法是"<tt class="samp"><b class="program">python</b> <b class="programopt">-c</b>
<var>command</var> [arg]
...</tt>",这种方法可以在命令行中直接执行语句,等同于Shell的
<b class="programopt">-c</b>选项。因为Python语句通常会包括空格之类的特殊字符,所以最好把整个<var>语句</var>用双引号包起来。<P>Note that there is a difference between "<tt class="samp">python file</tt>" and
"<tt class="samp">python &lt;file</tt>".  In the latter case, input requests from the
program, such as calls to <tt class="function">input()</tt> and <tt class="function">raw_input()</tt>, are
satisfied from <em>file</em>.  Since this file has already been read
until the end by the parser before the program starts executing, the
program will encounter end-of-file immediately.  In the former case
(which is usually what you want) they are satisfied from whatever file
or device is connected to standard input of the Python interpreter.<P>注意"<tt class="samp">python file</tt>"和"<tt class="samp">python
&lt;file</tt>"是有区别的。对于后一种情况,程序中类似于调用
<tt class="function">input()</tt><tt class="function">raw_input()</tt>
这样的输入请求,来自于确定的<em>文件</em>。因为在解析器开始执行之前,文件已经完全读入,所以程序指向文件尾。在前一种情况(这通常是你需要的)它们从来自于任何联接到
Python 解释器的标准输入,无论它们是文件还是其它设备。<P>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 <b class="programopt">-i</b> before the script.  (This does not
work if the script is read from standard input, for the same
reason as explained in the previous paragraph.)<P>使用脚本文件时,经常会运行脚本然后进入交互模式。这也可以通过在脚本之前加上
<b class="programopt">-i</b>
参数来实现。(如果脚本来自标准输入,就不能这样运行,与前一段提到的原因一样。)<P><H2><A NAME="SECTION004110000000000000000"></A><A NAME="argPassing"></A><BR>2.1.1 参数传递 Argument Passing </H2><P>When known to the interpreter, the script name and additional
arguments thereafter are passed to the script in the variable
<code>sys.argv</code>, which is a list of strings.  Its length is at least
one; when no script and no arguments are given, <code>sys.argv[0]</code> is
an empty string.  When the script name is given as <code>'-'</code> (meaning
standard input), <code>sys.argv[0]</code> is set to <code>'-'</code>.  When
<b class="programopt">-c</b> <var>command</var> is used, <code>sys.argv[0]</code> is set to
<code>'-c'</code>.  Options found after <b class="programopt">-c</b> <var>command</var> are
not consumed by the Python interpreter's option processing but left in
<code>sys.argv</code> for the command to handle.<P>调用解释器时,脚本名和附加参数之传入一个名为 <code>sys.argv</code>
的字符串列表。没有脚本和参数时,它至少也有一个元素:
<code>sys.argv[0]</code> 此时为空字符串。脚本名指定为 <code>'-'</code>
(表示标准输入)时, <code>sys.argv[0]</code>被设置为<code>'-'</code>,使用
<b class="programopt">-c</b> <var>指令</var> 时, <code>sys.argv[0]</code>
被设定为<code>'-c'</code>。 <b class="programopt">-c</b> <var>命令</var>之后的参数不会被
Python 解释器的选项处理机制所截获,而是留在<code>sys.argv</code>
中,供脚本命令操作。<P><H2><A NAME="SECTION004120000000000000000"></A><A NAME="interactive"></A><BR>2.1.2 交互模式 Interactive Mode </H2><P>When commands are read from a tty, the interpreter is said to be
in <em>interactive mode</em>.  In this mode it prompts for the next
command with the <em>primary prompt</em>, usually three greater-than
signs ("<tt class="samp">&gt;<code>&gt;</code>&gt;&nbsp;</tt>"); for continuation lines it prompts with
the <em>secondary prompt</em>, by default three dots ("<tt class="samp">...&nbsp;</tt>").
The interpreter prints a welcome message stating its version
number and a copyright notice before printing the first prompt:<P>从 tty 读取命令时,我们称解释器工作于 <em>交互模式</em>
。这种模式下它根据 <em>主提示符</em>
来执行,主提示符通常标识为三个大于号( "<tt class="samp">&gt;<code>&gt;</code>&gt;&nbsp;</tt>"
);继续的部分被称为 <em>从属提示符</em> ,由三个点标识(
"<tt class="samp">...&nbsp;</tt>"
)。在第一行之前,解释器打印欢迎信息、版本号和授权提示:<P><div class="verbatim"><pre>
python
Python 1.5.2b2 (#1, Feb 28 1999, 00:02:06)  [GCC 2.8.1] on sunos5
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
&gt;&gt;&gt;</pre></div><P>Continuation lines are needed when entering a multi-line construct.
As an example, take a look at this <tt class="keyword">if</tt> statement:<P>输入多行结构时需要从属提示符了,例如,下面这个  <tt class="keyword">if</tt>
语句:<P><div class="verbatim"><pre>
&gt;&gt;&gt; the_world_is_flat = 1
&gt;&gt;&gt; if the_world_is_flat:
...     print "Be careful not to fall off!"
...
Be careful not to fall off!</pre></div><P><H1><A NAME="SECTION004200000000000000000"></A><A NAME="interp"></A><BR>2.2 解释器及其环境 The Interpreter and Its Environment </H1>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -