forked from artofproblemsolving/pythonbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathway_of_the_program.html
More file actions
executable file
·562 lines (525 loc) · 32.4 KB
/
way_of_the_program.html
File metadata and controls
executable file
·562 lines (525 loc) · 32.4 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>1. The way of the program — How to Think Like a Computer Scientist: Learning with Python 3 (AoPS Edition)</title>
<link rel="stylesheet" href="_static/style.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/codemirrorEdited.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript" src="_static/pywindowCodemirrorC.js"></script>
<script type="text/javascript" src="_static/skulpt.min.js"></script>
<script type="text/javascript" src="_static/skulpt-stdlib.js"></script>
<script type="text/javascript" src="_static/aopsmods.js"></script>
<link rel="copyright" title="Copyright" href="copyright.html" />
<link rel="top" title="How to Think Like a Computer Scientist: Learning with Python 3 (AoPS Edition)" href="index.html" />
<link rel="next" title="2. Variables, expressions and statements" href="variables_expressions_statements.html" />
<link rel="prev" title="Contributor List" href="contrib.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="variables_expressions_statements.html" title="2. Variables, expressions and statements"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="contrib.html" title="Contributor List"
accesskey="P">previous</a> |</li>
<li><a href="index.html">How to Think Like a Computer Scientist: Learning with Python 3 (AoPS Edition)</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="body">
<div class="line-block">
<div class="line"><br /></div>
</div>
<div class="section" id="the-way-of-the-program">
<h1>1. The way of the program<a class="headerlink" href="#the-way-of-the-program" title="Permalink to this headline">¶</a></h1>
<p>The goal of this book is to teach you to think like a computer scientist. As
computer scientists, we will combine some of the best features of mathematics,
engineering, and natural science. Like mathematicians, we will use formal
languages to communicate our ideas. Like engineers, we will design complex
systems, assembling components into systems and evaluating tradeoffs
among alternatives. Like scientists, we will observe the behavior of complex
systems, form hypotheses, and test predictions.</p>
<p>The single most important skill for a computer scientist is <strong>problem
solving</strong>. Problem solving is a skill that involves formulating problems, thinking
creatively about solutions, and expressing a solution clearly and accurately. As
it turns out, the process of learning to program is an excellent opportunity to
practice problem-solving skills. That’s why this chapter is called <em>The way of
the program</em>.</p>
<div class="section" id="the-python-programming-language">
<span id="index-0"></span><h2>1.1. The Python programming language<a class="headerlink" href="#the-python-programming-language" title="Permalink to this headline">¶</a></h2>
<p>The programming language you will be learning is Python. Python is an example
of a <strong>high-level language</strong>; other high-level languages you might have heard
of are C++, PHP, Pascal, C#, and Java.</p>
<p>As you might infer from the name high-level language, there are also
<strong>low-level languages</strong>, sometimes referred to as machine languages or assembly
languages. Loosely speaking, computers can only execute programs written in
low-level languages. Thus, programs written in a high-level language have to be
translated into something more suitable before they can run.</p>
<p>Almost all programs are written in high-level languages because of their advantages.
It is much easier to program in a
high-level language so programs take less time
to write, they are shorter and easier to read, and they are more likely to be
correct. Second, high-level languages are <strong>portable</strong>, meaning that they can
run on different kinds of computers with few or no modifications.</p>
<p>The engine that translates and runs Python is called the <strong>Python Interpreter</strong>:
There are two ways to use it: <em>immediate mode</em> and <em>script
mode</em>. In immediate mode, which we also call <em>shell mode</em>, you type Python expressions
into the Python interpreter window (which is also called the <strong>shell</strong>),
and the interpreter immediately shows the result.</p>
<p>In AoPS classes, we will use the IDLE application that comes built-in with most Python
installations. In IDLE, the Python interpreter window is labeled <strong>Python Shell</strong>.</p>
<p>Instructions for installing Python on your computer are including in the <em>Course Introduction</em> document
on the AoPS class homepage, and are also posted on the class message board. When you start up
IDLE on your computer, it should look something like this:</p>
<img alt="Screen shot of shell" src="_images/shell_sshot.png" />
<p>The <tt class="docutils literal"><span class="pre">>>></span></tt> is called the <strong>Python prompt</strong>. The interpreter uses the prompt to indicate that it is ready for
instructions. We typed <tt class="docutils literal"><span class="pre">2</span> <span class="pre">+</span> <span class="pre">2</span></tt>, and the interpreter evaluated our expression, and replied <tt class="docutils literal"><span class="pre">4</span></tt>,
and on the next line it gave a new prompt, indicating that it is ready for more.</p>
<p>Alternatively, you can write a program in a file and use the interpreter to
run the lines of code in the file. Such a file is called a <strong>script</strong>. Scripts have the advantage
that they can be saved, printed, and so on. A Python script is often called a <strong>module</strong>.</p>
<p>For example, we can created a file named <tt class="docutils literal"><span class="pre">firstprogram.py</span></tt> using IDLE.
By convention, files that contain Python programs have names that end with
<tt class="docutils literal"><span class="pre">.py</span></tt>.) To do this, select “New File” or “New Window” from the File menu of IDLE, and then type
the following two lines into the new window that appears:</p>
<img alt="first program source screenshot" src="_images/my_first_program_source.png" />
<p>To run the program, we can select the “Run Module” or “Run Program” option from the “Run” menu of IDLE (or
alternatively you can press the F5 key). IDLE will ask you to save your program before it
can run it. Save it using the name <tt class="docutils literal"><span class="pre">firstprogram.py</span></tt>. Then your program will run, and it
should look something like the screenshot below:</p>
<img alt="first program output screenshot" src="_images/my_first_program_output.png" />
<p>Notice that the program always runs in the Shell window.</p>
<p>Don’t worry, most programs are more interesting than this one.</p>
<p>Working directly in the interpreter is convenient for testing short bits of code because you
get immediate feedback. Think of it as scratch paper used to help you work out
problems. Anything longer than a few lines should be put into a script.</p>
<p>There’s a third way that you can run Python using this ebook. In many places in this ebook,
we’ll have a Python interpreter built directly into the text. For example, our program
from above can appear directly in the book as shown below:</p>
<div id="firstprogram" class="pywindow" >
<div id="firstprogram_code_div" style="display: block">
<textarea rows="2" id="firstprogram_code" class="active_code" prefixcode="undefined">
print('My first program adds two numbers.')
print(2+3)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['firstprogram_code'] = true;
pythonTool.readOnlyFlags['firstprogram_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="firstprogram_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="firstprogram_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="firstprogram_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='firstprogram_error'></div>
<div style="text-align: center">
<canvas id="firstprogram_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="firstprogram_suffix" style="display:none">
</pre>
<pre id="firstprogram_pre" class="active_out">
</pre>
<div id="firstprogram_files" class="ac-files ac-files-hidden"></div>
</div>
<p>If you click the “Run” button, the program will run and you’ll see its output above directly in the ebook.
You can also edit the code directly in the window above. If you click the “Reset” button (or refresh
your browser window), the code will return to what it originally was (that is, any edits that you’ve
made will be removed.) The little numbers to the left are <strong>line numbers</strong>, and you don’t
type them in. They appear automatically in our ebook, so that it’s easier for us to discuss our
program. For example, we can say “line 1 of our program prints a sentence”.</p>
</div>
<div class="section" id="what-is-a-program">
<span id="index-1"></span><h2>1.2. What is a program?<a class="headerlink" href="#what-is-a-program" title="Permalink to this headline">¶</a></h2>
<p>A <strong>program</strong> is a sequence of instructions that specifies how to perform a
computation. The computation might be something mathematical, such as solving a
system of equations or finding the roots of a polynomial, but it can also be a
symbolic computation, such as searching and replacing text in a document.</p>
<p>The details look different in different languages, but a few basic instructions
appear in just about every language:</p>
<dl class="docutils">
<dt>input</dt>
<dd>Get data from the keyboard, a file, or some other device.</dd>
<dt>output</dt>
<dd>Display data on the screen or send data to a file or other device.</dd>
<dt>math</dt>
<dd>Perform basic mathematical operations like addition and multiplication.</dd>
<dt>conditional execution</dt>
<dd>Check for certain conditions and execute the appropriate sequence of
statements.</dd>
<dt>repetition</dt>
<dd>Perform some action repeatedly, usually with some variation.</dd>
</dl>
<p>Believe it or not, that’s pretty much all there is to it. Every program you’ve
ever used, no matter how complicated, is made up of instructions that look more
or less like these. Thus, we can describe programming as the process of
breaking a large, complex task into smaller and smaller subtasks until the
subtasks are simple enough to be performed with sequences of these basic
instructions.</p>
<p>That may be a little vague, but we will come back to this topic later when we
talk about <strong>algorithms</strong>.</p>
</div>
<div class="section" id="what-is-debugging">
<span id="index-2"></span><h2>1.3. What is debugging?<a class="headerlink" href="#what-is-debugging" title="Permalink to this headline">¶</a></h2>
<p>Programming is a complex process, and because it is done by human beings, it
often leads to errors. Programming errors are called
<strong>bugs</strong> and the process of tracking them down and correcting them is called
<strong>debugging</strong>. Use of the term <em>bug</em> to describe small engineering difficulties
dates back to at least 1889, when Thomas Edison had a bug with his phonograph.</p>
<p>Three kinds of errors can occur in a program: <strong>syntax errors</strong>, <strong>runtime errors</strong>,
and <strong>semantic errors</strong>. It is useful to
distinguish between them in order to track them down more quickly.</p>
</div>
<div class="section" id="syntax-errors">
<span id="index-3"></span><h2>1.4. Syntax errors<a class="headerlink" href="#syntax-errors" title="Permalink to this headline">¶</a></h2>
<p>Python can only execute a program if the program is syntactically correct;
otherwise, the process fails and returns an error message. <strong>Syntax</strong> refers
to the structure of a program and the rules about that structure. For example,
in English, a sentence must begin with a capital letter and end with a period.
this sentence contains a <strong>syntax error</strong>. So does this one</p>
<p>For most humans, a few syntax errors are not a significant problem, which is
why we can read the above sentences and still figure out what they mean.
Python is not so forgiving. If there is a single syntax error anywhere in your
program, Python will display an error message and quit, and you will not be able
to run your program. During the first few weeks of your programming career, you
will probably spend a lot of time tracking down syntax errors. As you gain
experience, though, you will make fewer errors and find them faster.</p>
</div>
<div class="section" id="runtime-errors">
<span id="index-4"></span><h2>1.5. Runtime errors<a class="headerlink" href="#runtime-errors" title="Permalink to this headline">¶</a></h2>
<p>The second type of error is a runtime error, so called because the error does
not appear until you run the program. These errors are also called
<strong>exceptions</strong> because they usually indicate that something exceptional (and
bad) has happened.</p>
<p>Runtime errors are rare in the simple programs you will see in the first few
chapters, so it might be a while before you encounter one.</p>
</div>
<div class="section" id="semantic-errors">
<span id="index-5"></span><h2>1.6. Semantic errors<a class="headerlink" href="#semantic-errors" title="Permalink to this headline">¶</a></h2>
<p>The third type of error is the <strong>semantic error</strong>. If there is a semantic error
in your program, it will run successfully, in the sense that the computer will
not generate any error messages, but it will not do the right thing. It will do
something else. Specifically, it will do what you told it to do.</p>
<p>The problem is that the program you wrote is not the program you wanted to
write. The meaning of the program (its semantics) is wrong. Identifying
semantic errors can be tricky because it requires you to work backward by
looking at the output of the program and trying to figure out what it is doing.</p>
</div>
<div class="section" id="experimental-debugging">
<span id="index-6"></span><h2>1.7. Experimental debugging<a class="headerlink" href="#experimental-debugging" title="Permalink to this headline">¶</a></h2>
<p>One of the most important skills you will acquire is debugging. Although it
can be frustrating, debugging is one of the most intellectually rich,
challenging, and interesting parts of programming.</p>
<p>In some ways, debugging is like detective work. You are confronted with clues,
and you have to infer the processes and events that led to the results you see.</p>
<p>Debugging is also like an experimental science. Once you have an idea what is
going wrong, you modify your program and try again. If your hypothesis was
correct, then you can predict the result of the modification, and you take a
step closer to a working program. If your hypothesis was wrong, you have to
come up with a new one. As Sherlock Holmes pointed out: When you have
eliminated the impossible, whatever remains, however improbable, must be the
truth. (A. Conan Doyle, <em>The Sign of Four</em>)</p>
<p>For some people, programming and debugging are the same thing. That is,
programming is the process of gradually debugging a program until it does what
you want. The idea is that you should start with a program that does
<em>something</em> and make small modifications, debugging them as you go, so that you
always have a working program.</p>
<p>Later chapters will make more suggestions about debugging and other programming
practices.</p>
</div>
<div class="section" id="formal-and-natural-languages">
<span id="index-7"></span><h2>1.8. Formal and natural languages<a class="headerlink" href="#formal-and-natural-languages" title="Permalink to this headline">¶</a></h2>
<p><strong>Natural languages</strong> are the languages that people speak, such as English,
Spanish, and French. They were not designed by people (although people try to
impose some order on them); they evolved naturally.</p>
<p><strong>Formal languages</strong> are languages that are designed by people for specific
applications. For example, the notation that mathematicians use is a formal
language that is particularly good at denoting relationships among numbers and
symbols. Chemists use a formal language to represent the chemical structure of
molecules. And most importantly:</p>
<blockquote>
<div><em>Programming languages are formal languages that have been designed to
express computations.</em></div></blockquote>
<p>Formal languages tend to have strict rules about syntax. For example, <tt class="docutils literal"><span class="pre">3+3=6</span></tt>
is a syntactically correct mathematical statement, but <tt class="docutils literal"><span class="pre">3=+6$</span></tt> is not.
H<sub>2</sub>O is a syntactically correct chemical name, but <sub>2</sub>Zz is
not.</p>
<p>Syntax rules come in two flavors, pertaining to <strong>tokens</strong> and structure.
Tokens are the basic elements of the language, such as words, numbers, parentheses,
commas, and so on. In Python, a statement like</p>
<p><tt class="docutils literal"><span class="pre">print("Happy</span> <span class="pre">New</span> <span class="pre">Year</span> <span class="pre">for</span> <span class="pre">",2013)</span></tt></p>
<p>has 6 tokens: a function name, an open parenthesis (round bracket), a string, a comma, a number, and a close parenthesis.</p>
<p>It is possible to make errors in the way one constructs tokens.
One of the problems with <tt class="docutils literal"><span class="pre">3=+6$</span></tt> is that <tt class="docutils literal"><span class="pre">$</span></tt> is not a
legal token in mathematics (at least as far as we know). Similarly,
<sub>2</sub>Zz is not a legal token in chemistry notation because there is no element with the abbreviation
<tt class="docutils literal"><span class="pre">Zz</span></tt>.</p>
<p>The second type of syntax rule pertains to the <strong>structure</strong> of a statement— that
is, the way the tokens are arranged. The statement <tt class="docutils literal"><span class="pre">3=+6$</span></tt> is structurally
illegal because you can’t place a plus sign immediately after an equal sign.
Similarly, molecular formulas have to have subscripts after the element name,
not before. And in our Python example, if we omitted the comma, or if we changed the two
parentheses around to say</p>
<p><tt class="docutils literal"><span class="pre">print)"Happy</span> <span class="pre">New</span> <span class="pre">Year</span> <span class="pre">for</span> <span class="pre">",2013(</span></tt></p>
<p>our statement would still
have six legal and valid tokens, but the structure is illegal.</p>
<p>When you read a sentence in English or a statement in a formal language, you
have to figure out what the structure of the sentence is (although in a natural
language you do this subconsciously). This process is called <strong>parsing</strong>.</p>
<p>For example, when you hear the sentence, “The other shoe fell”, you understand
that the other shoe is the subject and fell is the verb. Once you have parsed
a sentence, you can figure out what it means, or the <strong>semantics</strong> of the sentence.
Assuming that you know what a shoe is and what it means to fall, you will
understand the general implication of this sentence.</p>
<p>Although formal and natural languages have many features in common — tokens,
structure, syntax, and semantics — there are many differences:</p>
<dl class="glossary docutils">
<dt id="term-ambiguity">ambiguity</dt>
<dd>Natural languages are full of ambiguity, which people deal with by
using contextual clues and other information. Formal languages are
designed to be nearly or completely unambiguous, which means that any
statement has exactly one meaning, regardless of context.</dd>
<dt id="term-redundancy">redundancy</dt>
<dd>In order to make up for ambiguity and reduce misunderstandings, natural
languages employ lots of redundancy. As a result, they are often
verbose. Formal languages are less redundant and more concise.</dd>
<dt id="term-literalness">literalness</dt>
<dd>Formal languages mean exactly what they say. On the other hand, natural languages
are full of idiom and metaphor. If someone says, “The
other shoe fell”, there is probably no shoe and nothing falling.
You’ll need to find the
original joke to understand the idiomatic meaning of the other shoe falling.</dd>
</dl>
<p>People who grow up speaking a natural language—everyone—often have a hard
time adjusting to formal languages. In some ways, the difference between formal
and natural language is like the difference between poetry and prose, but more
so:</p>
<dl class="glossary docutils">
<dt id="term-poetry">poetry</dt>
<dd>Words are used for their sounds as well as for their meaning, and the
whole poem together creates an effect or emotional response. Ambiguity
is not only common but often deliberate.</dd>
<dt id="term-prose">prose</dt>
<dd>The literal meaning of words is more important, and the structure
contributes more meaning. Prose is more amenable to analysis than
poetry but still often ambiguous.</dd>
<dt id="term-program">program</dt>
<dd>The meaning of a computer program is unambiguous and literal, and can
be understood entirely by analysis of the tokens and structure.</dd>
</dl>
<p>Here are some suggestions for reading programs (and other formal languages).
First, remember that formal languages are much more dense than natural
languages, so it takes longer to read them. Also, the structure is very
important, so it is usually not a good idea to read from top to bottom, left to
right. Instead, learn to parse the program in your head, identifying the tokens
and interpreting the structure. Finally, the details matter. Little things
like spelling errors and bad punctuation, which you can get away with in
natural languages, can make a big difference in a formal language.</p>
</div>
<div class="section" id="the-first-program">
<h2>1.9. The first program<a class="headerlink" href="#the-first-program" title="Permalink to this headline">¶</a></h2>
<p>Traditionally, the first program written in a new language is called <em>Hello,
World!</em> because all it does is display the words, Hello, World! In Python, the script
looks like this:</p>
<div id="helloworld" class="pywindow" >
<div id="helloworld_code_div" style="display: block">
<textarea rows="1" id="helloworld_code" class="active_code" prefixcode="undefined">
print("Hello World!")</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['helloworld_code'] = true;
pythonTool.readOnlyFlags['helloworld_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="helloworld_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="helloworld_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="helloworld_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='helloworld_error'></div>
<div style="text-align: center">
<canvas id="helloworld_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="helloworld_suffix" style="display:none">
</pre>
<pre id="helloworld_pre" class="active_out">
</pre>
<div id="helloworld_files" class="ac-files ac-files-hidden"></div>
</div>
<p>This is an example of using the <strong>print function</strong>, which doesn’t actually print
anything on paper. It displays a value on the screen. In this case, the result shown
is <tt class="docutils literal"><span class="pre">Hello,</span> <span class="pre">World!</span></tt>.</p>
<p>The quotation marks in the program mark the beginning and end of the value;
they don’t appear in the result.</p>
<p>Some people judge the quality of a programming language by the simplicity of
the Hello, World! program. By this standard, Python does about as well as
possible.</p>
</div>
<div class="section" id="comments">
<span id="index-8"></span><h2>1.10. Comments<a class="headerlink" href="#comments" title="Permalink to this headline">¶</a></h2>
<p>As programs get bigger and more complicated, they get more difficult to read.
Formal languages are dense, and it is often difficult to look at a piece of
code and figure out what it is doing, or why.</p>
<p>For this reason, it is a good idea to add notes to your programs to explain in
natural language what the program is doing.</p>
<p>A <strong>comment</strong> in a computer program is text that is intended
only for the human reader — it is completely ignored by the interpreter.</p>
<p>In Python, the <cite>#</cite> token starts a comment. The rest of the line
is ignored. Here is a new version of <em>Hello, World!</em>.</p>
<div id="helloworldwithcomments" class="pywindow" >
<div id="helloworldwithcomments_code_div" style="display: block">
<textarea rows="7" id="helloworldwithcomments_code" class="active_code" prefixcode="undefined">
#---------------------------------------------------
# This demo program shows off how elegant Python is!
# Written by Joe Soap, December 2010.
# Anyone may freely copy or modify this program.
#---------------------------------------------------
print("Hello, World!") # Isn't this easy!</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['helloworldwithcomments_code'] = true;
pythonTool.readOnlyFlags['helloworldwithcomments_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="helloworldwithcomments_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="helloworldwithcomments_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="helloworldwithcomments_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='helloworldwithcomments_error'></div>
<div style="text-align: center">
<canvas id="helloworldwithcomments_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="helloworldwithcomments_suffix" style="display:none">
</pre>
<pre id="helloworldwithcomments_pre" class="active_out">
</pre>
<div id="helloworldwithcomments_files" class="ac-files ac-files-hidden"></div>
</div>
<p>You’ll also notice that we’ve left a blank line in the program. Blank lines
are also ignored by the interpreter, but comments and blank lines can make your
programs much easier for humans to parse. Use them!</p>
</div>
<div class="section" id="glossary">
<h2>1.11. Glossary<a class="headerlink" href="#glossary" title="Permalink to this headline">¶</a></h2>
<dl class="glossary docutils">
<dt id="term-algorithm">algorithm</dt>
<dd>A set of specific steps for solving a category of problems.</dd>
<dt id="term-bug">bug</dt>
<dd>An error in a program.</dd>
<dt id="term-comment">comment</dt>
<dd>Information in a program that is meant for other programmers (or anyone
reading the source code) and has no effect on the execution of the
program.</dd>
<dt id="term-debugging">debugging</dt>
<dd>The process of finding and removing any of the three kinds of
programming errors.</dd>
<dt id="term-exception">exception</dt>
<dd>Another name for a runtime error.</dd>
<dt id="term-formal-language">formal language</dt>
<dd>Any one of the languages that people have designed for specific
purposes, such as representing mathematical ideas or computer programs;
all programming languages are formal languages.</dd>
<dt id="term-high-level-language">high-level language</dt>
<dd>A programming language like Python that is designed to be easy for
humans to read and write.</dd>
<dt id="term-immediate-mode">immediate mode</dt>
<dd>A style of using Python where we type expressions at the command prompt, and
the results are shown immediately. Contrast with <strong>script</strong>, and see the
entry under <strong>Python shell</strong>.</dd>
<dt id="term-interpreter">interpreter</dt>
<dd>The engine that executes your Python scripts or expressions.</dd>
<dt id="term-low-level-language">low-level language</dt>
<dd>A programming language that is designed to be easy for a computer to
execute; also called machine language or assembly language.</dd>
<dt id="term-natural-language">natural language</dt>
<dd>Any one of the languages that people speak that evolved naturally.</dd>
<dt id="term-object-code">object code</dt>
<dd>The output of the compiler after it translates the program.</dd>
<dt id="term-parse">parse</dt>
<dd>To examine a program and analyze the syntactic structure.</dd>
<dt id="term-portability">portability</dt>
<dd>A property of a program that can run on more than one kind of computer.</dd>
<dt id="term-print-function">print function</dt>
<dd>A function used in a program or script that causes the Python interpreter to
display a value on its output device.</dd>
<dt id="term-problem-solving">problem solving</dt>
<dd>The process of formulating a problem, finding a solution, and
expressing the solution.</dd>
<dt id="term-22">program</dt>
<dd>a sequence of instructions that specifies to a computer actions and
computations to be performed.</dd>
<dt id="term-python-shell">Python shell</dt>
<dd>An interactive user interface to the Python interpreter. The user of a
Python shell types commands at the prompt (>>>), and presses the return
key to send these commands immediately to the interpreter for
processing. The word <em>shell</em> comes from Unix.</dd>
<dt id="term-runtime-error">runtime error</dt>
<dd>An error that does not occur until the program has started to execute
but that prevents the program from continuing.</dd>
<dt id="term-script">script</dt>
<dd>A program stored in a file (usually one that will be interpreted).</dd>
<dt id="term-semantic-error">semantic error</dt>
<dd>An error in a program that makes it do something other than what the
programmer intended.</dd>
<dt id="term-semantics">semantics</dt>
<dd>The meaning of a program.</dd>
<dt id="term-source-code">source code</dt>
<dd>A program in a high-level language before being compiled.</dd>
<dt id="term-syntax">syntax</dt>
<dd>The structure of a program.</dd>
<dt id="term-syntax-error">syntax error</dt>
<dd>An error in a program that makes it impossible to parse — and
therefore impossible to interpret.</dd>
<dt id="term-token">token</dt>
<dd>One of the basic elements of the syntactic structure of a program,
analogous to a word in a natural language.</dd>
</dl>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="variables_expressions_statements.html" title="2. Variables, expressions and statements"
>next</a> |</li>
<li class="right" >
<a href="contrib.html" title="Contributor List"
>previous</a> |</li>
<li><a href="index.html">How to Think Like a Computer Scientist: Learning with Python 3 (AoPS Edition)</a> »</li>
</ul>
</div>
<div class="footer">
© <a href="copyright.html">Copyright</a> 2014, AoPS Incorporated, 2012, Peter Wentworth, Jeffrey Elkner, Allen B. Downey and Chris Meyers.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.1.
</div>
</body>
</html>