forked from ninas/umonya_notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvocation.html
More file actions
264 lines (193 loc) · 8.15 KB
/
invocation.html
File metadata and controls
264 lines (193 loc) · 8.15 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
<!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: Running Python and Python Code</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>Introductory Programming in Python: Lesson 2<br />
Running Python and Python Code</h1>
<div class="centered">
[<a href="basic_concepts.html">Prev: Introduction to Programming</a>] [<a href="index.html">Course Outline</a>] [<a href="basic_output.html">Next: Basic Output</a>]
</div>
<h2>Starting the interactive shell</h2>
<p>Starting python is easy.</p>
<ol>
<li>We will be using IDLE to get things started</li>
<li>On the Desktop you will see a link named "Python"</li>
<li>To open the Interpreter click on <strong>"Run" > "Python Shell"</strong></li>
<li>The screen will open up and look similar to the following</li>
</ol>
<!--<p>Similarly from a terminal in Linux simply type <code>python</code> and press Enter.</p>-->
<pre class="listing">
Python 2.6.4 (r264:75706, Dec 7 2009, 18:43:55) [MSC v.1310 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
****************************************************************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.
****************************************************************
IDLE 2.6.4 ==== No Subprocess ====
>>>
</pre>
<p>What you see now is known as the python interactive shell. The first
line tells you what version of python you are running. The second line
gives you some information about how this particular copy of python was
built, and on what system it is running. The third line lists some
commands you can use to get more information. Whilst in the interactive
shell, you can enter python expressions and see their results
immediately. Try it now, type in a simple mathematical expression such
as <code>4 + 7</code>.</p>
<pre class='listing'>
>>> 4 + 7
11
>>>
</pre>
<p>As you can see, the answer or result of the expression, is printed
out, and you are returned to the prompt ">>>" Now try
something different; type in <code>a = 2 + 7</code>.</p>
<pre class='listing'>
>>> a = 2 + 7
>>> a
9
>>>
</pre>
<p>This should be somewhat familiar from the end of the basic concepts
section, where we were assigning a value to a name. In this case the
name is 'a' and the value is the result of '2 + 7'. Note that no value
is printed out immediately after the assignment. The interactive shell
always prints out the value of expressions, but not of statements. This
means however that the labels to which we assign values, more correctly
called <strong>variables</strong>, are expressions that evaluate to a
value.</p>
<h2>Running a saved python Script</h2>
<p>Whilst ideal as a calculator and for exploring new ideas quickly, it
would be pretty tedious if we had to re-enter our program into the
interactive shell every time we wanted to run it. So instead we can,
and in fact most often will, save our program code to a file. Program
source code is plain raw text. As such word processors like Microsoft
Word, Wordperfect, Open Office etc... are not suitable to the task. We will
look for Idle which is specifically geared towards creating python programs,
and comes with
python. </p>
<p>So now, instead of running the interactive shell, let's write our
first python program, this is also known as a script. Open up the fist
window again and do the following</p>
<ol>
<li>Click on <strong>"File" > "Save"</strong></li>
<li>Navigate to F:</li>
<li>Save the file as <strong>"hello.py"</strong></li>
<li>Type the following:</li>
</ol>
<pre class='listing'>
print "Hello World!"
</pre>
<p>Hit F5, or Click <strong>"Run" > "Run Module"</strong> to
run your program.<p>
<pre class='listing'>
>>>
Hello World!
>>>
</pre>
<p>This is a very simple program, but it should help you get the
idea. The computer is only following the instructions you provided
to it. Lets convert the test we did on the Interpreter to a program.
save it as 'second.py'
</p>
<pre class='listing'>
#My second python program
4 + 7 #This should add two numbers and output the result
a = 2 + 7
a
</pre>
<p>Notice the first and second lines. They contain what are called
<strong>comments</strong>. Any text following a hash character on a
line in a python program is a comment, including the hash itself.
Comments are completely ignored by python, and are there purely to
annotate code and make things easier for humans reading the code. We
use comments to place small reminders within the code for ourselves, or
explain the logic behind particularly tricky sections, etc... As we
progress through the course, you will find the code examples used
sprinkled more and more liberally with comments explaining how they
work.</p>
<p>Now it would be reasonable to expect that if we ran this program we
would get the same results as given to us by the interactive shell. So
let's try it.
<pre class='listing'>
>>>
>>>
</pre>
<p>No output? Nothing? The python interpreter (python), when called
without a file name following, starts up in the interactive shell. Only
then will it output the results of expressions entered. When invoked
with a file name following, and that file contains python code, python
will only produce output if explicitly told to do so. So lets use a trick
from our first program. Modify the code so it looks like the following</p>
<pre class='listing'>
#My second python program
4 + 7 #This should add two numbers and output the result
a = 2 + 7
print a
</pre>
<pre class='listing'>
>>>
9
>>>
</pre>
<p>Ah, that's better. Again, you should recognise the print statement
from the end of the basic concepts section. The print statement will be
explained in greater detail the next section.</p>
<p>One more important consideration is that python is <strong>case
sensitive</strong>. A variable 'a' and another variable 'A' are not the
same, as illustrated below ...</p>
<pre class='listing'>
#This program illustrates how python is case sensitive
a = 3 #assign a the value 3
A = "Hi" #assign A the value "Hi"
#check whether assigning to A has changed a
print a
#check that A and a are still different
print "A = ", A, " and a = ", a
</pre>
<p>which produces as output</p>
<pre class='listing'>
3
A = Hi and a = 3
</pre>
<h2>Exercises</h2>
<ol>
<li>Start the Python Interpreter? </li>
<li>Try using the Interpretor as a calculator? </li>
<li>Write the Hello World program. </li>
<li>Write the second program. </li>
<li>Write a program that prints out your name. </li>
</ol>
<p>Consider the following lines code...</p>
<pre class='listing'>a = 9
b = 3
a/b</pre>
<ol start='7'>
<li>For each of these three lines, which are expressions and
which are statements?</li>
<li>What will the output be if these lines are entered into the
python interactive interpreter?</li>
<li>What will the output be if I run these lines from a
file/script?</li>
<li>What changes need to be made to produce output when I run
these lines from a file/script?</li>
</ol>
<div class="centered">
[<a href="basic_concepts.html">Prev: Basic Concepts</a>] [<a href="index.html">Course Outline</a>] [<a href="basic_output.html">Next: Basic Output</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>