Академический Документы
Профессиональный Документы
Культура Документы
Chapter 1
Unless otherwise noted, the content of this course material is licensed under a Creative
Commons Attribution 3.0 License.
http://creativecommons.org/licenses/by/3.0/.
Copyright 2010- Charles Severance
http://www.pythonlearn.com/install.php
http://www.pythonlearn.com/install.php
What
Next?
What
Next?
What
Next?
What
Next?
What
Next?
What
Next?
What
Next?
Pick
Me!
Pick
Me!
Pick
Me!
Pick
Me!
Pick
Me!
Pay
Me!
User
Computer
Hardware + Software
Data
Programmers have some tools that allow them to build new tools
Programmers sometimes write tools for lots of users and sometimes
programmers write little helpers for themselves to automate a task
Networks
....
From a software creators point of view, we build the software. The end users
(stakeholders/actors) are our masters - who we want to please - often they pay
us money when they are pleased. But the data, information, and networks are
our problem to solve on their behalf. The hardware and software are our
friends and allies in this quest.
Why be a programer?
Information
Programmer
Programs for
Humans...
http://www.youtube.com/watch?v=vlzwuFkn88U
http://www.youtube.com/watch?v=sN62PAKoBfE
Programs for
Humans...
Programs for
Humans...
Programs for
Humans...
the clown ran after the car and the car ran into the tent and the
tent fell down on the clown and the car
python words.py
Enter file: words.txt
to 16
Hardware Architecture
python words.py
Enter file: clown.txt
the 7
What
Next?
Software
Input
and Output
Devices
Generic
Computer
Central
Processing
Unit
Secondary
Memory
Main
Memory
http://upload.wikimedia.org/wikipedia/commons/3/3d/RaspberryPi.jpg
Definitions
What
Next?
Software
What
Next?
Input
and Output
Devices
Central
Processing
Unit
if x< 3: print
Generic
Computer
Main
Memory
Secondary
Memory
Input
and Output
Devices
What
Next?
Software
Central
Processing
Unit
01001001
00111001
Secondary
Memory
What
Next?
Main
Memory
Machine
Language
http://www.youtube.com/watch?v=y39D4529FM4
Python as a Language
http://www.youtube.com/watch?v=9eMWG3fwiEU
http://harrypotter.wikia.com/wiki/Parseltongue
When you make a mistake, the computer does not think you are cute. It
says syntax error - given that it *knows* the language and you are just
learning it. It seems like Python is cruel and unfeeling.
You must remember that *you* are intelligent and *can* learn - the
computer is simple and very fast - but cannot learn - so it is easier for you
to learn Python than for the computer to learn English...
Talking to Python
csev$ python
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
What next?
csev$ python
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 1
>>> print x
1
>>> x = x + 1
>>> print x
2
This is a good test to make sure that you have
>>> exit()
Python correctly installed. Note that quit()
also works to end the interactive session.
What do we Say?
Elements of Python
bigcount = None
bigword = None
for word,count in counts.items():
if bigcount is None or count > bigcount:
bigword = word
bigcount = count
A short Story
about how to count
words in a file in
Python.
python words.py
Enter file: words.txt
to 16
Reserved Words
Sentences or Lines
x=2
Assignment Statement
x=x+2
print x
Print statement
Variable
Operator
Constant
Reserved Word
Python Scripts
Programming Paragraphs
Interactive Python is good for experiments and programs of 3-4 lines long
But most programs are much longer so we type them into a file and tell
python to run the commands in the file.
Interactive
Script
You enter a sequence of statements (lines) into a file using a text editor
and tell Python to execut the statements in the file
Sequential Steps
print 'Smaller'
n=5
X > 20 ?
Yes
print 'Bigger'
print 'Finis'
No
n>0?
Program:
x=5
if x < 10:
print 'Smaller'
if x > 20:
print 'Bigger'
x=2
print x
x=x+2
print x
Output:
2
4
Conditional Steps
Yes
print x
print x
X < 10 ?
Program:
x=x+1
x=5
x=1
print 'Blastoff'
Chapter 3
Program:
n = n -1
Smaller
Finis
print 'Finis'
Repeated Steps
Yes
print n
Output:
Chapter 5
Chapter 2
n=5
while n > 0 :
print n
n=n-1
print 'Blastoff!'
Output:
5
4
3
2
1
Blastoff!
Sequential
Repeated
Conditional
bigcount = None
bigword = None
for word,count in counts.items():
if bigcount is None or count > bigcount:
bigword = word
bigcount = count
25
191
31
112
44
20
181
60
129
179
1
121
139
163
54
164
178
187
73
79
114
104
113
177
106
4
173
198
45
50
126
14
122
197
81
69
145
17
183
184
25
191
31
112
44
20
181
60
129
179
1
121
139
163
54
164
178
187
73
79
114
104
113
177
106
4
173
198
45
50
126
14
122
197
81
69
145
17
183
184
179
129
60
181
20
44
112
31
197
25
79
73
187
178
164
54
163
139
121
1
50
45
169
173
4
106
177
113
104
114
76 34 33 185 102
9
24 188 42 151
99 15 82 26 8
148 62 142 170 72
11 131 0 107 71
143 59 38 3
41
48 78 101 130 83
94 97 149 154 140
116 160 005 89 125
117 150 152 120 46
193
51
092
37
159
93
35
195
40
19
184
183
17
145
69
81
197
122
14
126
179
129
60
181
20
44
112
31
197
25
79
73
187
178
164
54
163
139
121
1
50
45
169
173
4
106
177
113
104
114
76 34 33 185 102
9
24 188 42 151
99 15 82 26 8
148 62 142 170 72
11 131 0 107 71
143 59 38 3
41
48 78 101 130 83
94 97 149 154 140
116 160 005 89 125
117 150 152 120 46
193
51
092
37
159
93
35
195
40
19
184
183
17
145
69
81
197
122
14
126
41 12
9 74 15
41 12
largest_so_far
9 74 15
-13 41 74
A short "Story"
about how to count
words in a file in
Python.
A word used to read
data from a user.
A sentence about
updating one of
many counts.
A paragraph about
how to find the
largest item in a list.
Summary