Why Program?: Python For Informatics: Exploring Information
Why Program?: Python For Informatics: Exploring Information
Chapter 1
• Programmers have some tools that allow them to build new tools
Computer Programmer
Hardware + Software
http://www.youtube.com/watch?v=sN62PAKoBfE
while music is playing:
Left hand out and up
Programs for
Right hand out and up
Flip Left hand Humans...
Flip Right hand
Left hand to right shoulder
Right hand to left shoulder
Left hand to back of head
Right ham to back of head
Left hand to right hit
Right hand to left hit
Left hand on left bottom
Right hand on right bottom
Wiggle
Wiggle
Jump http://www.youtube.com/watch?v=sN62PAKoBfE
while music is playing:
Left hand out and up
Programs for
Right hand out and up
Flip Left hand Humans...
Flip Right hand
Left hand to right shoulder
Right hand to left shoulder
Left hand to back of head
Right ham to back of head
Left hand to right hit
Right hand to left hit
Left hand on left bottom
Right hand on right bottom
Wiggle
Wiggle
Jump http://www.youtube.com/watch?v=sN62PAKoBfE
while music is playing:
Left hand out and up
Programs for
Right hand out and up
Flip Left hand Humans...
Flip Right hand
Left hand to right shoulder
Right hand to left shoulder
Left hand to back of head
Right hand to back of head
Left hand to right hip
Right hand to left hip
Left hand on left bottom
Right hand on right bottom
Wiggle
Wiggle
Jump http://www.youtube.com/watch?v=sN62PAKoBfE
the clown ran after the car and the car ran into the tent and the
tent fell down on the clown and the car
Main
Memory
Definitions
What
• Central Processing Unit: Runs the Program - The CPU is Next?
always wondering “what to do next”? Not the brains
exactly - very dumb but very very fast
• Main Memory: Fast small temporary storage - lost on reboot - aka RAM
• Secondary Memory: Slower large permanent storage - lasts until deleted - disk
drive / memory stick
Generic
Software What
Next? Computer
Input Central
and Output Processing
Devices Unit
Secondary
if x< 3: print Memory
Main
Memory
Software What
Next?
Input Central
and Output Processing
Devices Unit
Secondary
01001001 Memory
00111001
Main
Memory
Machine
Language
Totally Hot CPU
What
Next?
http://www.youtube.com/watch?v=y39D4529FM4
Hard Disk in Action
http://www.youtube.com/watch?v=9eMWG3fwiEU
Python as a Language
Parseltongue is the language of serpents and
those who can converse with them. An
individual who can speak Parseltongue is
known as a Parselmouth. It is a very
uncommon skill, and may be hereditary.
Nearly all known Parselmouths are descended
from Salazar Slytherin.
http://harrypotter.wikia.com/wiki/Parseltongue
Python is the language of the Python
Interpreter and those who can converse with it.
An individual who can speak Python is known
as a Pythonista. It is a very uncommon skill,
and may be hereditary. Nearly all known
Pythonistas use software initially developed by
Guido van Rossum.
Early Learner: Syntax Errors
• We need to learn the Python language so we can communicate our instructions
to Python. In the beginning we will make lots of mistakes and speak gibberish like
small children.
• 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.
Let’s Talk to Python...
What Do We Say?
Elements of Python
counts = dict()
for word in words:
counts[word] = counts.get(word,0) + 1
bigcount = None
bigword = None python words.py
Enter file: words.txt
for word,count in counts.items():
if bigcount is None or count > bigcount: to 16
bigword = word
bigcount = count
print bigword, bigcount
Reserved Words
x = 2 Assignment statement
x = x + 2 Assignment with expression
print x Print statement
• Most programs are much longer, so we type them into a file and tell
Python to run the commands in the file
• Interactive
> You type directly to Python one line at a time and it responds
• Script
> You enter a sequence of statements (lines) into a file using a text editor
and tell Python to execute the statements in the file
Program Steps or Program Flow
• Like a recipe or installation instructions, a program is a sequence of
steps to be done in order
print 'Finis'
print 'Finis'
Chapter 3
n=5
Repeated Steps
No Yes Output:
n>0? Program:
print n 5
n=5
4
while n > 0 :
3
n = n -1 print n
2
n=n–1
1
print 'Blastoff!'
Blastoff!
print 'Blastoff'
Loops (repeated steps) have iteration variables that
change each time through a loop. Often these
Chapter 5 iteration variables go through a sequence of numbers.
name = raw_input('Enter file:') Sequential
handle = open(name, 'r')
text = handle.read() Repeated
words = text.split()
Conditional
counts = dict()
for word in words:
counts[word] = counts.get(word,0) + 1
bigcount = None
bigword = None
bigcount = None
bigword = None A sentence about
for word,count in counts.items(): updating one of the
if bigcount is None or count > many counts
bigcount:
bigword = word
bigcount = count
A paragraph about how
to find the largest item
print bigword, bigcount in a list
Summary