You are on page 1of 52

Why Program?

Chapter 1

Python for Informatics: Exploring Information


www.py4inf.com
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


Computers want to be helpful...
What

• Computers are built for one purpose - Next?

to do things for us

• But we need to speak their language to


describe what we want done
What What What
• Users have it easy - someone already Next? Next? Next?
put many different programs
(instructions) into the computer and What What What
users just pick the ones we want to use Next? Next? Next?
Programmers Anticipate Needs
• iPhone Applications are a market

• iPhone Applications are over 3 Billion


downloads

• Programmers have left their jobs to be


Pick Pick Pick
full-time iPhone developers Me! Me! Me!

• Programmers know the ways of the Pick Pick Pay


program Me! Me! Me!
Users .vs. Programmers

• Users see computers as a set of tools - word processor, spreadsheet,


map, todo list, etc.

• Programmers learn the compter “ways” and the computer language

• 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
User

Computer Programmer
Hardware + Software

Data Information .... Networks

From a software creator’s 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?

• To get some task done - we are the user and programmer

• Clean up survey data

• To produce something for others to use - a programming job

• Fix a performance problem in the Sakai software

• Add guestbook to a web site


What is Code? Software? A
Program?
• A set of stored instructions

• It is a little piece of our intelligence in the computer

• It is a little piece of our intelligence we can give to others - we figure


something out and then we encode it and then give it to someone
else to save them the time and energy of figuring it out

• A piece of creative art - particularly when we do a good job on user


experience
Writing programs (or programming) is a very creative and rewarding activity. You can
write programs for many reasons ranging from making your living to solving a difficult
data analysis problem to having fun to helping someone else solve a problem. This
book assumes that everyone needs to know how to program and that once you know
how to program, you will figure out what you want to do with your newfound skills.
We are surrounded in our daily lives with computers ranging from laptops to cell
phones. We can think of these computers as our ``personal assistants'' who can take
care of many things on our behalf. The hardware in our current-day computers is
essentially built to continuously ask us the question, ``What would you like me to do
next?''.
Our computers are fast and have vasts amounts of memory and could be very helpful
to us if we only knew the language to speak to explain to the computer what we
would like it to ``do next''. If we knew this language we could tell the computer to do
tasks on our behalf that were reptitive. Interestingly, the kinds of things computers can
do best are often the kinds of things that we humans find boring and mind-numbing.
name = raw_input("Enter file:")
handle = open(name, "r")
text = handle.read()
words = text.split()
counts = dict()
for word in words:
counts[word] = counts.get(word,0) + 1
python words.py
Enter file: words.txt
bigcount = None
to 16
bigword = None
for word,count in counts.items():
if bigcount == None or count > bigcount:
bigword = word
bigcount = count

print bigword, bigcount


Hardware Architecture
Generic
Software What
Next? Computer
Input Central
and Output Processing
Devices Unit
Secondary
Memory

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

• Input Devices: Keyboard, Mouse, Touch Screen

• Output Devices: Screen, Speakers, Printer, DVD Burner

• 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
Terms

• Source code - the programs we humans write - and read - written in a


programming language - source code is generally portable across
systems

• Machine code - what really runs on the machine - not very readible -
produced by a compiler - machine code is unique to hardware and
operating system - Windows machine code is different from Macintosh
machine code.
Programmer Tools
Programmer Tools

• Since the Central Processing Unit must ran Machine Code, we must
“translate” our source code to machine code

• Compiler - Takes our source code and makes a machine code


version of our program

• Interpreter - Reads our source code and runs it directly - Python is


an interpreted language - Python is an interpreter
A programmer
Input develops a program.
Data
If a compiler is used
the compiler
translated the
source to machine
code for
distribution.
Input
Data
If an interpreter is
used, the
programmer simply
distributes the
source code.
Compiler .vs. Interpreter
• Only the programmer needs to have the compiler - once the compiler
is done - the machine code program is self-contained

• The programmer keeps the source code and distributes the machine
code - different machine code is needed for Mac, PC, etc.

• Both the programmer and user need to have the interpreter installed
on their system

• Generally the programmer distributes the source code of the


program
main() { Your Source Code
printf("Hello world\n"); in the C Language
}
The C Language
^@^@^@^@__DATA^@^@^@^@^@^@^@^@^
@^@^@^@0^@^@^@^@q^@^@
Compiler Reads
^@^@^@^@^B^@^@^@^@^@^@^@^@^@^@^
@^@^@^@^@^@^@^@^@^@__nl_symbol_
your source code
ptr^@__DATA^@^@^@^@^@^@^@^@^@^@
^@^@0t^@^@^@^P^@^@
and produces your Your
t^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@
^F^@^@^@^Q^@^@^@^@__la_symbol_p machine code. Machine
tr^@__DATA^@^@^@^@^@^@^@^@^@^@^
@^@0<84>^@^@^@D^@^@@^A^@^@/usr/
Code
lib/
libmx.A.dylib^@^@^@^@^@^L^@^@^@
4^@^@^@^XC½m¥^@X^A^C^@^A^@^@/ ^?ELF^A^A^A^@^@^@^@^@^@^@^@^@^B^@^C^@^A^@^@^@\xa0\x82^D^H4^@^@^@\x90^]^@^@^@^@^@
usr/lib/libSy ^@4^@ ^@^G^@(^@$^@!^@^F^@^@^@4^@^@^@4\x80^D^H4\x80^D^H\xe0^@^@^@\xe0^@^@^@^E^@^@
^@^D^@^@^@^C^@^@^@^T^A^@^@^T\x81^D^H^T\x81^D^H^S^@^@^@^S^@^@^@^D^@^@^@^A^@^@^@^A\
^D^HQVhT\x83^D^H\xe8\xb7\xff\xff\xff\xf4\x90\x90U\x89\xe5S\xe8^@^@^@^@[\x81\xc3_
^R^@^@P\x8b\x83\xfc\xff\xff\xff\x85\xc0t^B\xff\xd0\x8b]\xfc\xc9\xc3\x90\x90\x90\
x90\x90\x90\x90\x90\x90\x90U\x89\xe5\x83\xec^H\x80=L\x95^D^H^@t^L\xeb^\\x83\xc0^
D\xa3H\x95^D^H\xff\xd2\xa1H\x95^D^H\x8b^P\x85\xd2u\xeb\xc6^EL\x95^D^H^A\xc9\xc3\
x90U\x89\xe5\x83\xec^H\xa1\\x94^D^H\x85\xc0t!\xb8^@^@^@^@\x85\xc0t^X\xc7^D$\\x94

Hello world ^D^H\xe8\xbc|\xfb\xf7\x8d\xb6^@^@^@^@\x8d\xbf^@^@^@^@\xc9\xc3\x90\x90U\x89\xe5\x


print “hello world” è^@^@^Qp^@^@^@^B^@^@^@^@^@^@^@^@<80>^@^D^@^@^@^@^@^@^@^@^@__picsy
mbol_stub__TEXT^@^@^@^@^@^@^@^@^@^@^@^@,X^@^@^@^@^@^@^
\X^@^@^@^B^@^@^@^@^@^@^@^@<80>^@^@^H^@^@^@^@^@^@^@
$__symbol_stub^@^@^@__TEXT^@^@^@^@^@^@^@^@^@^@^@^@,X^@^@^@^@^@^@^
\X^@^@^@^B^@^@^@^@^@^@^@^@<80>^@^@^H^@^@^@^@^@^@^@^T__picsymbolst
ub1__TEXT^@^@^@^@^@^@^@^@^@^@^@^@,`^@^@^B ^@^@^
\`^@^@^@^E^@^@^@^@^@^@^@^@<80>^@^D^H^@^@^@^@^@^@^@
Machine
__cstring^@^@^@^@^@^@^@__TEXT^@^@^@^@^@^@^@^@^@^@^@^@.<80>^@^@^AX
^@^@^^<80>^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^
Code
A^@^@^AÐ__DATA^@^@^@^@^@^@^@^@^@^@^@^@0^@^@^@^P^@^@^@
^@^@^@^P^@^@^@^@^G^@^@^@^C^@^@^@^F^@^@^@^@__data^@^@^@^@^@^@^@^@^
for the
@^@__DATA^@^@^@^@^@^@^@^@^@^@^@^@0^@^@^@^@q^@^@
^@^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@__nl_symbol_ptr

Hello world Python


^@__DATA^@^@^@^@^@^@^@^@^@^@^@^@0t^@^@^@^P^@^@
t^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^F^@^@^@^Q^@^@^@^@__la_symbol_ptr^
Interpreter
@__DATA^@^@^@^@^@^@^@^@^@^@^@^@0<84>^@^@^@D^@^@
<84>^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^G^@^@^@^U^@^@^@^@__dyld^@^@^@^
@^@^@^@^@^@^@__DATA^@^@^@^@^@^@^@^@^@^@^@^@0È^@^@^@^\^@^@
È^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@__bss^@^@^@^@^@^
@^@^@^@^@^@__DATA^@^@^@^@^@^@^@^@^@^@^@^@0ä^@^@^@^D^@^@^@^@^@^@^@
^B^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^@^@^@^@__common^@^@^@^@^@^@^@^
@__DATA^@^@^@^@^@^@^@^@^@^@^@^@0ð^@^@^@4^@^@^@^@^@^@^@^D^@^@^@^@^
@^@^@^@^@^@^@^A^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@8__LINKEDIT^@^@^@^@^
@^@^@^@@^@^@^@^F^D^@^@0^@^@^@^F^D^@^@^@^G^@^@^@^A^@^@^@^@^@^@^@^D
Python is an Interpreter
• To run Python programs, users must install Python on their computers

• Development is quick and easy - we simply make a change to our


program and run it again in a single step

• For data analysis - Python is just a tool that you keep on your
computer

• Interpreters are more convenient when the user and programmer are
the same person
Running Python Interactively
Python Interactive
• Since Python is interpreted we can just type programs directly into
Python

• See Also http://datamech.com/devan/trypython/trypython.py

>>> x = 1
x=1 >>> print x
print x 1
x=x+1
>>> x = x + 1
>>>
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
>>> exit()
This is a good test to make sure that you have
Python correctly installed.
Python Scripts

• 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.

• In a sense we are “giving Python a script”

• As convention, we add “.py” as the suffix on the end of these files to


indicate they contain Python
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 inititially 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...
Elements of Python

• Vocabulary / Words - Variables and Reserved words (Chapter 2)

• Sentence structure - valid syntax patterns (Chapters 3-5)

• Story structure - constructing a program for a a purpose


name = raw_input("Enter file:") A short “Story”
handle = open(name, "r") about how to count
text = handle.read()
words = text.split() words in a file in
counts = dict() Python.
for word in words:
counts[word] = counts.get(word,0) + 1

bigcount = None
python words.py
bigword = None
Enter file: words.txt
for word,count in counts.items():
to 16
if bigcount == None or count > bigcount:
bigword = word
bigcount = count

print bigword, bigcount


Reserved Words

• You can not use reserved words as variable names / identifiers

and del for is raise


assert elif from lambda return
break else global not try
class except if or while
continue exec import pass yield
def finally in print
Chapter 2
Sentences or Lines
x=2 Assignment Statement

x=x+2 Assignment with expression

print x Print statement

Variable Operator Constant Reserved Word


Programming Paragraphs
Program Steps or Program Flow

• Like a recipe or installation instructions, a program is a sequence of


steps to be done in order

• Some steps are conditional - they may be skipped

• Sometimes a step or group of steps are to be repeated

• Sometimes we store a set of steps to be used over and over as


needed several places throughout the program
Sequential Steps
x=1 Program:
Output:
print x x=2
print x 2
x=x+1 x=x+2 4
print x
print x

When a program is running, it flows from one step to the next.


We as programmers set up “paths” for the program to follow.
Chapter 2
x=5
Conditional Steps
Yes
X < 10 ? Program:

print “Smaller” x=5 Output:


if x < 10:
print "Smaller" Smaller
Yes
X > 20 ? Finis
if x > 20:
print “Bigger” print "Bigger"

print “Finis”
print “Finis”
Chapter 3
n=5
Repeated Steps
No Yes
n>0? Output:
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.
Stored (and reused) Steps
Program:
def
hello():
print “Hello”
def hello():
print "Hello" Output:
print “Fun”
print "Fun"
Hello
hello()
hello() Fun
print “Zip” Zip
print “Zip” Hello
hello()
Fun
hello()
We call these reusable pieces of code “functions”. Chapter 4
name = raw_input("Enter file:")
handle = open(name, "r")
text = handle.read() Sequential
words = text.split() Repeated
counts = dict()
Conditional
for word in words:
counts[word] = counts.get(word,0) + 1

bigcount = None
bigword = None
for word,count in counts.items():
if bigcount == None or count > bigcount:
bigword = word
bigcount = count

print bigword, bigcount


An Animated Short Story...
Finding the largest number in a list of numbers...
25 1 114 117 150 152 120 46 19 126
198 121 104 116 160 105 89 125 40 14
31 139 113 94 97 199 154 140 195 122
112 163 177 48 78 101 130 83 35 197
44 54 106 143 59 38 3 41 93 81
20 164 4 11 131 0 107 71 159 69
181 178 173 148 62 142 170 72 37 145
What is the Largest Number?
60
129
187
73
169
45
99 15 82 26 8 192
9 24 188 42 151 51
17
183
179 79 50 76 34 33 185 102 193 184
179 79 50 76 34 33 185 102 193 184
129 73 45 9 24 188 42 151 51 183
60 187 169 99 15 82 26 8 192 17
181 178 173 148 62 142 170 72 37 145
20 164 4 11 131 0 107 71 159 69

What is the Largest Number?


44
112
54
163
106
177
143 59 38 3 41
48 78 101 130 83
93
35
81
197
31 139 113 94 97 199 154 140 195 122
198 121 104 116 160 105 89 125 40 14
25 1 114 117 150 152 120 46 19 126
What is the Largest Number?

3 41 12 9 74 15
What is the Largest Number?

3 41 12 9 74 15

largest_so_far -13 41 74
name = raw_input("Enter file:") A short “Story”
handle = open(name, "r") about how to count
text = handle.read()
words = text.split() words in a file in
counts = dict() Python.
for word in words:
counts[word] = counts.get(word,0) + 1
A word used to read
data from a user.
bigcount = None A sentence about
bigword = None
for word,count in counts.items(): updating one of
if bigcount == None or count > bigcount: many counts.
bigword = word A paragraph about
bigcount = count
how to find the
print bigword, bigcount largest item in a list.
Summary

• This is a quick overview of Chapter 1

• We will revisit these concepts throughout the course

• Focus on the big picture

You might also like