Intro to Programming
Chapter 1
Computers want to be
helpful...
• Computers are built for one
purpose - to do things for us
• But we need to speak their
language to describe what we want
done
What What
What What
What
• Users have it easy - someone Next? Next?
Next? Next?
Next?
already put many different
programs (instructions) into the What What What
computer and users just pick the Next? Next? Next?
ones we want to use
Users .vs. Programmers
• Users see computers as a set of tools - word processor,
spreadsheet, maps, to-do list, email, etc.
• Programmers learn the computer “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
Why be a programmer?
• To get some task done - we are the user and
programmer
• Help analyze and process data (many programs have
APIs)
• To produce something for others to use – write programs
• Fix an issue with a program
What is Code? Software? A
Program?
• A sequence 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
while music is playing:
Left hand out and up Programs for
Right hand out and up
Flip Left hand
Flip Right hand
Humans...
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 hit
Right hand to left hit
Left hand on left bottom
Right hand on right bottom
Wiggle
Wiggle
Jump
Program for a Computer
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
bigcount = Nonebigword = None
for word,count in counts.items():
if bigcount is None or count > bigcount:
bigword = word
bigcount = countprint bigword, bigcount
Hardware Architecture
Computer
Generic
Computer
Input Central
and Processin
Output g
Devices Unit
Secondary
Memory
Main
Memory
Definitions
• Central Processing Unit: Runs the Program - The CPU is
always wondering “what to do next”? Not the brains
exactly – not smart 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 Random Access Memory (RAM)
• Secondary Memory: Slower large permanent storage - lasts
until deleted - disk drive / memory stick
Motherboard
Source:
http://commons.Wikimedia.org
Central Processing Unit
(CPU)
Source:
http://commons.Wikimedia.org
Memory (RAM)
Source:
http://commons.wikimedia.org
Hard Drive
Source:
http://commons.Wikimedia.org
Python as a Language
Python: A Computer
Language
• We need to learn the Python language so we can
communicate our instructions to Python. In the beginning we
will make lots of mistakes
• 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.
• 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 : The
Interpreter
The Prompt ...
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 purpose
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
Statements
x=2 Assignment Statement
x=x+2 Assignment with expression
print x Print statement
Variable Operator Constant Reserved Word
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
Interactive vs. Script
• 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 and Program
Flow
• Like a recipe or installation instructions, a program is a
sequence of steps to be done in sequence
• Some steps are conditional
• 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 x=1
print x
print x x=x+1
print x
x=x+1
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 ?
print
'Smaller'
Yes
X > 20 ?
print 'Bigger'
print 'Finis'
Chapter 3
n=5
Repeated Steps
No Yes
n>0? Program:
print n n=5
while n > 0 :
n = n -1 print n
n=n–1
print 'Blastoff!'
print
'Blastoff' Loops (repeated steps) have iteration variables that
change each time through a loop. Often these iteration
variables go through a sequence of numbers.
Chapter 5
Summary
• This is a quick overview of Chapter 1
• We will revisit these concepts throughout the course
• Focus on the big picture