-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGettingStartedWithPython.html
More file actions
367 lines (248 loc) · 23 KB
/
GettingStartedWithPython.html
File metadata and controls
367 lines (248 loc) · 23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh-CN" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh-CN" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta content="Topic: Basic description of the Python programming language, Difficulty: Easy, Category: Background" name="description" />
<meta content="python, install, basics, scripts, interpreter, foundations" name="keywords" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>介绍Python编程语言 — Python Like You Mean It</title>
<script type="text/javascript" src="../_static/js/modernizr.min.js"></script>
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="https://www.googletagmanager.com/gtag/js?id=UA-115029372-1"></script>
<script src="../_static/gtag.js"></script>
<script src="../_static/translations.js"></script>
<script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"></script>
<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["$", "$"], ["\\(", "\\)"]], "processEscapes": true, "ignoreClass": "document", "processClass": "math|output_area"}})</script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/my_theme.css" type="text/css" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="next" title="安装Python" href="Installing_Python.html" />
<link rel="prev" title="代码格式的简单指南" href="SiteFormatting.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../index.html" class="icon icon-home"> Python Like You Mean It
</a>
<div class="version">
1.4
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption"><span class="caption-text">目录:</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../intro.html">Python Like You Mean It</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../module_1.html">模组1:Python入门</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="SiteFormatting.html">代码格式的简单指南</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">介绍Python编程语言</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#Python是什么?">Python是什么?</a></li>
<li class="toctree-l3"><a class="reference internal" href="#Python脚本">Python脚本</a></li>
<li class="toctree-l3"><a class="reference internal" href="#Python直译器是什么?“安装Python”意味着什么?">Python直译器是什么?“安装Python”意味着什么?</a></li>
<li class="toctree-l3"><a class="reference internal" href="#为什么选择Python?">为什么选择Python?</a></li>
<li class="toctree-l3"><a class="reference internal" href="#总结">总结</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="Installing_Python.html">安装Python</a></li>
<li class="toctree-l2"><a class="reference internal" href="Informal_Intro_Python.html">非正式的Python介绍</a></li>
<li class="toctree-l2"><a class="reference internal" href="Jupyter_Notebooks.html">Jupyter记事本</a></li>
<li class="toctree-l2"><a class="reference internal" href="Getting_Started_With_IDEs_and_Notebooks.html">设置开发环境</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../module_2.html">模组2:Python基础</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_2_problems.html">模组2:题目</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_3.html">模组3:NumPy基础</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_3_problems.html">模组3:题目</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_4.html">模组4:面向对象编程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_5.html">模组5:琐碎话题</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Python Like You Mean It</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html">Docs</a> »</li>
<li><a href="../module_1.html">模组1:Python入门</a> »</li>
<li>介绍Python编程语言</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/Module1_GettingStartedWithPython/GettingStartedWithPython.md.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<style>
/* CSS overrides for sphinx_rtd_theme */
/* 24px margin */
.nbinput.nblast.container,
.nboutput.nblast.container {
margin-bottom: 19px; /* padding has already 5px */
}
/* ... except between code cells! */
.nblast.container + .nbinput.container {
margin-top: -19px;
}
.admonition > p:before {
margin-right: 4px; /* make room for the exclamation icon */
}
/* Fix math alignment, see https://github.com/rtfd/sphinx_rtd_theme/pull/686 */
.math {
text-align: unset;
}
</style>
<div class="section" id="介绍Python编程语言">
<h1>介绍Python编程语言<a class="headerlink" href="#介绍Python编程语言" title="永久链接至标题">¶</a></h1>
<p>在本节中我们将学习</p>
<ul class="simple">
<li><p>Python是什么。</p></li>
<li><p>“安装Python”具体意味着什么。</p></li>
<li><p>如何写和执行Python代码。</p></li>
</ul>
<div class="section" id="Python是什么?">
<h2>Python是什么?<a class="headerlink" href="#Python是什么?" title="永久链接至标题">¶</a></h2>
<p><strong>Python是一门编程语言</strong>。具体来讲,Python为我们提供一组严谨的语法规则;每条规则都对应一组计算机会执行的明确命令。这将允许我们编写一些即易读,又能提供具体命令给计算机的文本。所谓的Python<strong>代码</strong>其实就是符合Python语法的文本。</p>
<p>比如说,以下的文本就符合Python的语法规则:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">3</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'2 + 3 = </span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
</pre></div>
</div>
<p>根据Python的定义,它将指令计算机去:</p>
<ul class="simple">
<li><p>计算 2 + 3</p></li>
<li><p>在内存中设定一个函数 <code class="docutils literal notranslate"><span class="pre">x</span></code> 为计算的结果(5)</p></li>
<li><p>访问 <code class="docutils literal notranslate"><span class="pre">x</span></code> 的值,并在计算机屏幕上显示:“2 + 3 = 5”</p></li>
</ul>
<p>一个<strong>不符合</strong>Python规则的例子:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">~</span> <span class="mi">3</span>
</pre></div>
</div>
<p>原因是,根据Python定义,夹在两个整数中间的符号 <code class="docutils literal notranslate"><span class="pre">~</span></code> 没有任何意义。</p>
<p>因此,“学习Python”意味着学习Python语言的语法规则以及这些规则对应的计算机指令。同时,你也需要了解已经“提前写好”的,极其方便的Python自带的工具;这些工具是所谓Python标准库的一部分。</p>
<p>有了对Python语言的基本理解后,我们现在要学习如何使用我们的Python代码去给计算机发送指令。最基本的方法是:</p>
<ul class="simple">
<li><p>写Python“脚本”:一个包含Python代码的文本。</p></li>
<li><p>将这个文本送到<strong>Python直译器</strong>(Python Interpreter)中。直译器将告诉计算机去执行代码描述的工作。</p></li>
</ul>
</div>
<div class="section" id="Python脚本">
<h2>Python脚本<a class="headerlink" href="#Python脚本" title="永久链接至标题">¶</a></h2>
<p>你可以用任意一个文本编辑器将前文的能用的代码存到一个文本文件中。就像这样,你写好了一个<strong>Python脚本</strong>:一个包含Python代码的文本文件。标准情况下,Python代码的后缀都是 <code class="docutils literal notranslate"><span class="pre">.py</span></code>(比如说 <code class="docutils literal notranslate"><span class="pre">my_code.py</span></code>),而不是常见的 <code class="docutils literal notranslate"><span class="pre">.txt</span></code>(例如 <code class="docutils literal notranslate"><span class="pre">my_text.txt</span></code>)。这个 <code class="docutils literal notranslate"><span class="pre">.py</span></code> 后缀没有任何特殊的;这仅仅是为了帮助用户区分包含Python代码的文本文件和其他正常的包含普通文章的文本文件。</p>
<p>虽然你可以使用基本的文本编辑器去写Python脚本(如notepad(Windows),TextEdit(Mac),nano(Linux)),但是你也可以使用更加复杂的提供“集成开发环境”(Integrated Development Environment,也就是IDE)的编辑器来写代码。一个为Python配置的IDE会警告不符合Python语法的代码,就像Word这类文字处理软件会提醒你的错误拼写一样。我们会在之后的一节里详细讨论IDE。</p>
<div class="admonition warning">
<p class="admonition-title fa fa-exclamation-circle"><strong>警告</strong>:</p>
<p>不要使用像Microsfot Word这样的传统文字处理程序来编写代码。这些程序会“静默”地对你打出的字符进行修改,如双引号的种类。这会导致你的代码出现错误。</p>
<p>译者注:在写Python代码时一定要记得使用英文输入法。Python直译器并不会理解中文的双引号 或单引号 ‘’</p>
</div>
<p>在有一个Python脚本后,该如何让电脑读取并执行脚本的指示呢?你需要在你的计算机上安装一个<strong>Python直译器</strong>。当人们说你需要“安装Python”的时候,这就是他们具体的意思。</p>
</div>
<div class="section" id="Python直译器是什么?“安装Python”意味着什么?">
<h2>Python直译器是什么?“安装Python”意味着什么?<a class="headerlink" href="#Python直译器是什么?“安装Python”意味着什么?" title="永久链接至标题">¶</a></h2>
<p>Python直译器泛指任何可以执行以下的电脑程序:</p>
<ul class="simple">
<li><p>读取文本文件(如 <code class="docutils literal notranslate"><span class="pre">my_code.py</span></code>)。</p></li>
<li><p>解析其文本并反馈文本是否符合Python语言的语法规则(如果不符合,直译器会报错)。</p></li>
<li><p>指示计算机去执行文本描述的任务。</p></li>
<li><p>总体而言,Python直译器将从上到下执行脚本。</p></li>
</ul>
<p>世界上第一个Python直译器是用C编程语言写的,因此其被命名为CPython。所以<strong>CPython直译器是最官方的Python语言直译器</strong>。任何新的Python特征或规则一定会在<a class="reference external" href="https://github.com/python/cpython">CPython代码库</a>中实现。</p>
<div class="admonition warning">
<p class="admonition-title fa fa-exclamation-circle"><strong>注</strong>:</p>
<p>当然,Python直译器也存在于其他不是C的编程语言中,如Java(Jython)和Go(Grumpy)。这些直译器并不能保证实现最新的Python规范,因此很少有人会将它们和CPython直译器对比。</p>
</div>
<div class="admonition warning">
<p class="admonition-title fa fa-exclamation-circle"><strong>关于安装Python</strong>:</p>
<p>不要从python.org下载和安装Python。这样做没有任何错,但是本文后面的一节将提供详细的Python安装指示。</p>
</div>
<p>如果你从<a class="reference external" href="https://www.python.org/downloads/release/python-363/">python.org</a>“在你的计算机上安装了Python”,你在做的基本就是下载一个满足Python直译器规范的可执行文件。比如在Windows上,这个程序叫做 <code class="docutils literal notranslate"><span class="pre">python.exe</span></code>。这个程序就是之前提到的CPython代码的产物,其可以执行任何Python直译器的工作。同时,你也在下载一大组可以用于你自己代码里的工具和函数。这些工具和函数叫做Python标准库;花一些时间去了解<a class="reference external" href="https://docs.python.org/3/library/index.html#the-python-standard-library">标准库</a>的内容。</p>
<p>当你在计算机上安装了一个Python直译器后,使用它去执行Python脚本并不困难。为简化我们的讨论,假设叫做 <code class="docutils literal notranslate"><span class="pre">python</span></code> 的直译器程序和 <code class="docutils literal notranslate"><span class="pre">my_script.py</span></code> 都在你的计算机中同一个文件夹内(叫做 <code class="docutils literal notranslate"><span class="pre">folder</span></code>)。那么,在命令行(对Windows而言就是 <code class="docutils literal notranslate"><span class="pre">cmd.exe</span></code>)中,你可以执行以下指令:</p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>python my_script.py
</pre></div>
</div>
<p>这将指示Python直译器程序 <code class="docutils literal notranslate"><span class="pre">python</span></code> 去读取你的文本文件 <code class="docutils literal notranslate"><span class="pre">my_script.py</span></code>,确保里面的代码符合所有Python语言的语法规则,并指示你的计算机去执行和文本相应的指令。如果你的脚本仅仅包含代码 <code class="docutils literal notranslate"><span class="pre">print("hello</span> <span class="pre">world")</span></code>,那么你的命令行自然会显示字符串 <code class="docutils literal notranslate"><span class="pre">hello</span> <span class="pre">world</span></code>。</p>
<p>在实际使用中,不管你从任何文件夹中执行 <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">my_script.py</span></code>,你的计算机也应该会知道如何去找到名为 <code class="docutils literal notranslate"><span class="pre">python</span></code> 程序。相应的设置会在安装过程中完成。</p>
<p>理解Python语言是被一个用另外语言编写的程序来直译的事实可能比较绕脑。如果是这样,那另外那个语言是怎么被直译的呢?在CPython的情况下,答案就是C代码并不会被直译;有程序存在于Windows,Mac,和Linux上可以直接将C代码翻译成电脑直接执行的机器指令。</p>
</div>
<div class="section" id="为什么选择Python?">
<h2>为什么选择Python?<a class="headerlink" href="#为什么选择Python?" title="永久链接至标题">¶</a></h2>
<p>Python已经成为了一个极其流行的编程语言。事实上,它可能是<a class="reference external" href="https://cacm.acm.org/blogs/blog-cacm/176450-python-is-now-the-most-popular-introductory-teaching-language-at-top-u-s-universities/fulltext">最流行的大学编程入门语言</a>。首先,Python的句法非常易懂易读。比如说,接下来的代码会求数字 0-9 的和,并打印结果:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>
</pre></div>
</div>
<p>同样的工作可以用以下的C++代码达成,但是这难懂很多:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf"><iostream></span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf"><vector></span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf"><numeric></span><span class="cp"></span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
<span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o"><</span><span class="kt">int</span><span class="o">></span> <span class="n">a</span> <span class="o">=</span> <span class="p">{</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">};</span>
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">accumulate</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span> <span class="n">a</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span> <span class="mi">0</span><span class="p">)</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>因此,Python作为一门语言很方便快速编写和测试新代码。同时,Python是开源的;你可以免费使用它,任何人都可以参与到维持或改善这门语言中,且大家创造了很多给Python提供了大量功能的第三方模组。因此,Python在科学家,工程师,和其他研究者中极其流行。</p>
<p>在本资源中我们会使用Python和高度优化的数字工作模组NumPy。</p>
</div>
<div class="section" id="总结">
<h2>总结<a class="headerlink" href="#总结" title="永久链接至标题">¶</a></h2>
<ul class="simple">
<li><p>Python是一门编程语言——它为我们提供一组简单的语法规则来编写既可以让人看懂也可以准确翻译到计算机指令的文本。</p></li>
<li><p>Python代码是任何符合Python语言标准的文本。</p></li>
<li><p>Python脚本是一个包含Python代码的文本文件。这种文件根据命名标准都使用后缀 <code class="docutils literal notranslate"><span class="pre">.py</span></code>。</p></li>
<li><p>Python直译器是一个能够读取和分析文本文件并根据Python语言的规则将其中的代码翻译成计算机指示的程序。</p></li>
<li><p>“安装Python”归根结底就是将Python直译器下载到你的计算机上。</p></li>
<li><p>官方的Python直译器是一个用C语言编写的程序,叫做CPython直译器。</p></li>
<li><p>Python“标准库“是和CPython直译器一起包装的一组庞大的工具和函数。</p></li>
</ul>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="Installing_Python.html" class="btn btn-neutral float-right" title="安装Python" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="SiteFormatting.html" class="btn btn-neutral float-left" title="代码格式的简单指南" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2019, Ryan Soklaski
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>