forked from ninas/umonya_notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_concepts.html
289 lines (223 loc) · 11.1 KB
/
basic_concepts.html
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
<!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>
<title>Introductory Programming in Python: Basic Concepts</title>
<link rel='stylesheet' type='text/css' href='style.css' />
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<script src="animation.js" type="text/javascript">
</script>
</head>
<body onload="animate_loop()">
<div class="page">
<h1>Introduction to Programming<br />
Basic Concepts</h1>
<div class="centered">
[<a href="index.html">Course Outline</a>] [<a href="invocation.html">Next: Running Python and Python Code</a>]
</div>
<h2>What is a Program?</h2>
<p>Simply put, a program is a set of instructions on <strong>how</strong> to
take some <strong>input</strong> data and produce
<strong>output</strong> data. Programs can be written in many
languages, such as PERL, Python, C, Ruby, Pascal, etc... Even a
stylised form of natural languages such as English, known as pseudo
code, can be used to describe a program effectively.</p>
<div class="centered">
<strong>Input -> Program -> Output</strong>
</div>
<p>It is important to realise that the <strong>program</strong>
determines what input is satisfactory, and what input will cause error.
Input cannot be used to change how a program works. Similarly, the program
defines what output will be produced. If you need a program to handle
input differently, you need to write a new program. Likewise, if the
output isn't what you need, you will need to change the program too.</p>
<p>Ultimately a program, no matter what language it is written in,
consists of a some atomic actions/instructions, each one an instruction
that cannot be divided into a sequence of 'smaller' or 'less complex'
instructions. These instructions are composed (as in mathematical
composition) in various manners, usually by issuing them in sequential order,
but also by defining the result of one instruction as the operand of
another.</p>
<p>Instructions (or sets of composed instructions) can be divided into
two groups, those that produce a result, and those that
don't. For example, adding two numbers, 3 and 4, together has a result
(7). More specifically, it has a <strong>value</strong>, i.e. some data
the program can continue to work with. Contrast this with an
instruction that puts a line of text on the screen. It produces no
value as a result. Instructions which produce a value are called
<strong>expressions</strong>, instructions which produce no value are
called <strong>statements</strong></p>
<p>In a similar manner to statements, expressions can be combined with
other expressions, to form complex expressions. Expressions are
combined using <strong>operators</strong> such as the mathematical
functions sum, difference, product, quotient, etc... Thus 1+1 is also
an expression that has <strong>a single value</strong> despite the fact
that it is a composite of multiple sub-expressions.</p>
<p>Which we can compare to the much simpler version of the same thing
in python</p>
<pre class='listing'>
a = 12
b = 12
if a*b == 144:
a = 1
</pre>
<p>The basic point is to illustrate that programs are made up of small,
well defined, <strong>easy to understand</strong> steps in sequence. As
in chess, where each individual piece can only move in a very limited
number of ways, yielding a tiny number of potential moves per piece per
turn, these moves can be combined in a near infinite number of ways and
often grouped together into common techniques or strategies. So too can
the simple statements of a programming language be combined in infinite
ways to produce complex but meaningful results.</p>
<h2>Everyone can program!</h2>
<p>Believe it or not, you've programmed before. You've programmed your
friends, and do so every time you give them directions. After all what
is a set of directions but a sequence of instructions.</p>
<pre class='listing'>
OUT OF Cape Town TAKE the N1
TAKE the Sable rd. Offramp
AT the fork VEER LEFT
AT the traffic lights TURN LEFT
AT the NEXT traffic lights TURN RIGHT
AT the traffic circle TURN LEFT
AT the NEXT traffic circle TURN RIGHT
AT the t-junction TURN LEFT
</pre>
<p>You will note that some (in fact a hell of a lot) of the words in
the directions to my place are capitalised. In the language of giving
directions, these are pretty much our atomic instructions. The portion
of the directions left in lowercase are labels or names for things that
are not common to all sets of directions, most often places specific to
the set of directions being given. These have value, and would be our
expressions.</p>
<p>Examining the directions we have</p>
<dl>
<dt>OUT OF</dt>
<dd>meaning I must first be <strong>in</strong> a named place
before performing the next instruction</dd>
<dt>TAKE</dt>
<dd>meaning to drive along, or turn off onto a named offramp</dd>
<dt>AT</dt>
<dd>continue until the named place or situation is reached
<strong>before</strong> performing the next instruction</dd>
<dt>VEER</dt>
<dd>meaning to stay in a particular lane as the road splits</dd>
<dt>TURN LEFT, TURN RIGHT</dt>
<dd>self explanatory</dd>
<dt>NEXT</dt>
<dd>meaning the next object of specified type encountered</dd>
</dl>
<p>At first the description of these concepts may seem obvious, but
recall that computers, the machines executing your programming
instructions, have an IQ of 0. They are not intelligent, fiendishly
annoying at times perhaps, but never intelligent, never aware, never
capable of the massive amounts of inference and contextualization done
by the human brain. They are designed and built to understand and
execute only specific atomic steps. So what is implicit in the
instructions contained in a set of directions given to us, must be
explicitly defined for a computer.</p>
<h2>Data representation and translation of real world problems</h2>
<p>Now that the concept of sequences of statements has been throughly
flogged to death, to what do these statements apply? They apply to
<strong>data</strong>! But data in a computer, like instructions, must
be simple and well defined, or at the very least able to be broken down
into multiple well defined simple pieces. In general computers work
only with numbers. The pictures you see on screen, the text you are
reading, the sound you hear when playing MP3's are all numbers. The
actual physical devices attached to the computer are what are
responsible for transforming the numbers with which the CPU deals into
humanly recognisable phenomena such as sound waves and images. Until
the screen, or the speakers, are reached, everything is numbers. So it
stands to reason that the most basic, atomic, unit of data in a
computer is a number. Fortunately, modern programming languages are
capable of dealing with numbers and sequences of numbers in a few
different ways. Integers, and Reals can be considered atomic data units
in almost every modern computer language, as can text in the form of a
string of characters in sequence.</p>
<p>As programs are usually written to solve problems occurring in the
real world, it falls to the programmer to translate the problem being
solved into something the computer can deal with, i.e. numbers. This is
reminiscent of those annoying word problems we encountered in junior
school mathematics.</p>
<blockquote class='problem'>
Jane has seven apples, Mary has four, Bob has one. They pool their
resources, and divide the apples equally. How many apples does each
one receive?
</blockquote>
<p>The most difficult concept to grasp when learning to program is the
ability to translate a problem expressed in words into a set of
instructions that describe the solution to the problem. Learning a
programming language doesn't teach one to program, it merely provides
one with a specific set of tools with which one can solve a problem.
Learning how to apply these tools is the true skill to programming, and
this comes primarily with experience. The problem set out above is
ridiculously simple, and you've already worked out the answer in your
head, but <strong>how did you do it?</strong> Describe the process! But
what if there were 1000 people involved, and many thousands of apples.
Working it out in your head becomes a tedious task, but the basic
process you followed in your head for three people applies equally well
to the case of a thousand people. And so for our first exercise in
programming let us translate the word problem into the atomic
statements and atomic data units that can be used to provide us with
the answer. Assume we are provided with only the following statements
and expressions to work with, and that statements are numbered from 1
upwards in the order in which they appear in our program:</p>
<ul>
<li>EXPRESSION -- raw_input(): get a number from input</li>
<li>STATEMENT -- labelname = #: Assign the value of number to
a label for storage</li>
<li>STATEMENT -- labelname += #: Addition of the second number
to the number stored in labelname</li>
<li>STATEMENT -- if # != # {}: check if the two numbers
are not equal. If they are not equal perform any instructions
within the braces {}</li>
<li>STATEMENT -- GOTO #: Instead of executing the next
statement, execute the statement numbered '#'</li>
<li>STATEMENT -- labelname /= #: Division of the number stored
in labelname by '#'</li>
<li>STATEMENT -- print #: Output of a number to the
screen</li>
</ul>
<p>Note that "#" can be either an actual number, the name of a
label storing a number, or the instruction "raw_input()" which is the
number received as input.</p>
<blockquote class='problem'>
Each of a number of people has at least one apple. They pool their
resources and divide the apples equally. For any given number of
people and the number of apples each of these people has, how many
apples will each person receive?
</blockquote>
<p>Given only the above statements and expressions to work with, there
are some important questions that need answering</p>
<ul>
<li>Do we need to know who started with how many apples?</li>
<li>How do we represent how many apples there are?</li>
<li>How can one determine the total number of people?</li>
</ul>
<pre class='listing'>
1: apples = 0
2: people = 0
3: a = raw_input()
4: people += 1
5: if a != 0 {
6: apples += a
7: GOTO 3
}
8: apples /= people
9: print apples
</pre>
<h2>Exercises</h2>
<ol>
<li>What is a program?</li>
<li>What is the difference between an expression and a
statement?</li>
</ol>
<div class="centered">
[<a href="index.html">Course Outline</a>] [<a href="invocation.html">Next: Introduction to Python</a>]
</div>
</div>
<div class="pagefooter">
Copyright © James Dominy 2007-2008; Released under the <a href="http://www.gnu.org/copyleft/fdl.html">GNU Free Documentation License</a><br />
<a href="intropython.tar.gz">Download the tarball</a>
</div>
</body>
</html>