📄 node5.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head> <title>3. 非正式的Python介绍</title> <meta name="description" content="3. 非正式的Python介绍 "> <meta name="keywords" content="tut"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <link rel="STYLESHEET" href="tut.css"> <link rel="next" href="node6.html"> <link rel="previous" href="node4.html"> <link rel="up" href="tut.html"> <link rel="next" href="node6.html"></head> <body> <div class="navigation"><table align="Center" width="100%" cellpadding="0" cellspacing="2"> <tbody> <tr> <td><a href="node4.html"><img src="../icons/previous.gif" border="0" height="32" alt="Previous Page" width="32"></a></td> <td><a href="tut.html"><img src="../icons/up.gif" border="0" height="32" alt="Up One Level" width="32"></a></td> <td><a href="node6.html"><img src="../icons/next.gif" border="0" height="32" alt="Next Page" width="32"></a></td> <td align="Center" width="100%">Python 教学文件</td> <td><a href="node2.html"><img src="../icons/contents.gif" border="0" height="32" alt="Contents" width="32"></a></td> <td><img src="../icons/blank.gif" border="0" height="32" alt="" width="32"></td> <td><img src="../icons/blank.gif" border="0" height="32" alt="" width="32"></td> </tr> </tbody></table> <b class="navlabel">Previous:</b> <a class="sectref" href="node4.html">2.使用Python的直译器 </a> <b class="navlabel">Up:</b> <a class="sectref" href="tut.html">Python 教学文件</a> <b class="navlabel">Next:</b> <a class="sectref" href="node6.html">4. 更多流程控制的工具 </a> <br><hr></div> <!--End of Navigation Panel--> <!--Table of Child-Links--> <a name="CHILD_LINKS"><strong>小段落</strong></a> <ul> <li><a name="tex2html244" href="node5.html#SECTION005100000000000000000">3.1 把Python当作计算机来用 </a> <ul> <li><a name="tex2html245" href="node5.html#SECTION005110000000000000000">3.1.1 数字 </a> </li> <li><a name="tex2html246" href="node5.html#SECTION005120000000000000000">3.1.2 字串 </a> </li> <li><a name="tex2html247" href="node5.html#SECTION005130000000000000000">3.1.3 Unicode 字串 </a> </li> <li><a name="tex2html248" href="node5.html#SECTION005140000000000000000">3.1.4 列(List) </a> </li> </ul> </li> <li><a name="tex2html249" href="node5.html#SECTION005200000000000000000">3.2 迈向程式设计的第一步 </a> </li></ul> <!--End of Table of Child-Links--> <hr> <h1> <br> 3. An Informal Introduction to Python </h1> <p> 在底下的例子里,你可以很容易区别凡是需要输入的地方都会出现prompts ("<tt class="samp">><code>></code>> </tt>" 或 "<tt class="samp">... </tt>"),凡是输出的结果则没有。如果你想要跟着这个教学文件一起做的话,你就得打入所有在prompts之后的指令,凡是没有prompts出现的行就是直译器输出的结果。值得注意的是,secondarypromt 之后如果什么东西都没有,表示这是一个空行(直接按ENTER的结果),也表示这是一个多行指令的结束。 </p><p> 在本文件中的大部分例子,都有加上注释,甚至是那些互动模式下的例子。注释(comment)在Python中是以 "<tt class="character">#</tt>" 之后的东西都是注释(译:跟Perl一样)。注释可以自成一行,也可以跟在空白字元或是程式码的后面。但是,如果 "<tt class="character">#</tt>" 是在字串常数(string literal)之中的话,就不代表注释的意义,而只是一个普通字元罢了。 </p><p> 底下是一些例子: </p><p> </p><dl><dd><pre class="verbatim"># this is the first comment<br>SPAM = 1 # and this is the second comment<br> # ... and now a third!<br>STRING = "# This is not a comment."<br></pre> </dd> </dl> <p> </p> <h1> <br> 3.1 把Python当作计算机来用 </h1> <p> 现在我们来试一试一些简单的Python指令吧。请先启动Python的直译器并且等待primary prompt( "<tt class="samp">><code>></code>> </tt>" )的出现。(应该不会很久的) </p> <p> </p> <h2> <br> 3.1.1 数字 </h2> <p> 直译器就好像一个计算机一样:你可以打入一个表示式(expression),然后直译器会把这个expression的执行结果秀出来。Expression的语法都很简单直接,一般的运算符号 <code>+</code>, <code>-</code>, <code>*</code> 以及 <code>/</code> 的用法就跟其他的程式语言(像是Pascal或C)一样。你也可以用括号"( )" 来表示运算执行的先后次序。例子如下: </p> <p> </p> <dl> <dd><pre class="verbatim">>>> 2+2<br>4<br>>>> # This is a comment<br>... 2+2<br>4<br>>>> 2+2 # and a comment on the same line as code<br>4<br>>>> (50-5*6)/4<br>5<br>>>> # Integer division returns the floor:<br>... 7/3<br>2<br>>>> 7/-3<br>-3<br></pre> </dd> </dl> <p> 跟C语言一样,等于符号 ("<tt class="character">=</tt>") 其实是表示设定某个值给一个变数的意思。虽然设定("<tt class="character">=</tt>") 运算本身是有结果值的,但是直译器并不会输出其结果来。 </p> <p> </p> <dl> <dd><pre class="verbatim">>>> width = 20<br>>>> height = 5*9<br>>>> width * height<br>900<br></pre> </dd> </dl> <p> 一个值是可以同时设给许多变数的: </p> <p> </p> <dl> <dd><pre class="verbatim">>>> x = y = z = 0 # Zero x, y and z<br>>>> x<br>0<br>>>> y<br>0<br>>>> z<br>0<br></pre> </dd> </dl> <p> 浮点数的运算在Python里面也是支援的,如果整数与浮点数(带小数点或e的数)进行运算的话,整数部分会先转换(convert)成浮点数再进行运算。 </p> <p> </p> <dl> <dd><pre class="verbatim">>>> 4 * 2.5 / 3.3<br>3.0303030303<br>>>> 7.0 / 2<br>3.5<br></pre> </dd> </dl> <p> 甚至连复数的运算也支援喔,只需要把虚数部分加上 "<tt class="samp">j</tt>" 或是 "<tt class="samp">J</tt>"在其后就可以了。如果实部不为零的话,复数的写法就写成 "<tt class="samp">(<var>real</var>+<var>imag</var>j)</tt>" 。或者,我们也可以用函数的方式来表示复数为 "<tt class="samp">complex(<var>real</var>, <var>imag</var>)</tt>" 的形式。 </p> <p> </p> <dl> <dd><pre class="verbatim">>>> 1j * 1J<br>(-1+0j)<br>>>> 1j * complex(0,1)<br>(-1+0j)<br>>>> 3+1j*3<br>(3+3j)<br>>>> (3+1j)*3<br>(9+3j)<br>>>> (1+2j)/(1+1j)<br>(1.5+0.5j)<br></pre> </dd> </dl> <p> 复数的虚数部分及实数部分的值都是以浮点数(float point numbers)来表示的,如果 <var>z</var> 代表一个复数的话,你可以很轻易的用 <code><var>z</var>.real</code> 以及 <code><var>z</var>.imag</code> 得到一个复数的实数部分及虚数部分。 </p> <p> </p> <dl> <dd><pre class="verbatim">>>> a=1.5+0.5j<br>>>> a.real<br>1.5<br>>>> a.imag<br>0.5<br></pre> </dd> </dl> <p> 复数没有办法直接用 (<tt class="function">float()</tt>, <tt class="function">int()</tt> 或是 <tt class="function">long()</tt>) 转换成浮点数或是整数。事实上,复数没有直接对应的实数,你必须用
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -