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 • Users have it easy - some
Trang 1Why Program?
Chapter 1
Trang 2Computers 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
• Users have it easy - someone already
put many different programs
(instructions) into the computer and
users just pick the ones we want to use
What Next?
What Next?
What Next?
What Next?
What Next?
What Next? What Next?
Trang 3Programmers Anticipate Needs
• iPhone Applications are a market
• iPhone Applications are over 3 Billion
downloads
• Programmers have left their jobs to be
full-time iPhone developers
• Programmers know the ways of the
program
Pick Me!
Pick Me!
Pick Me!
Pick
Pick Me!
Trang 4Users 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
Trang 5Computer Hardware + Software
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.
Information Data
User
Programmer
Trang 6Why 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
Trang 7What 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
Trang 8Writing 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.
Trang 9name = raw_input("Enter file:")
for word,count in counts.items():
if bigcount == None or count > bigcount:
bigword = word
bigcount = count
print bigword, bigcount
python words.py Enter file: words.txt
to 16
Trang 10Hardware Architecture
Trang 11Main Memory
Secondary Memory
GenericComputer
What Next?
Trang 12• Central Processing Unit : Runs the Program - The CPU is
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
What Next?
Trang 13Main Memory
Secondary Memory
GenericComputer
What Next?
if x< 3: print
Trang 14Main Memory
Secondary Memory
MachineLanguage
What Next?
01001001 00111001
Trang 15Totally Hot CPU
http://www.youtube.com/watch?v=y39D4529FM4
What Next?
Trang 16Hard Disk in Action
http://www.youtube.com/watch?v=9eMWG3fwiEU
Trang 18Programmer Tools
Trang 19Programmer Tools
• Since the Central Processing Unit must ran Machine Code, we must
“translate” our source code to machine code
version of our program
• Interpreter - Reads our source code and runs it directly - Python is
an interpreted language - Python is an interpreter
Trang 20A programmer develops a program.
If a compiler is used the compiler translated the source to machine
code for distribution
If an interpreter is
used, the programmer simply distributes the source code
Input
Data
Input
Data
Trang 21Compiler 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
Trang 22The C LanguageCompiler Reads your source code and produces your machine code.
Trang 23è^@^@^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^@^@^@^@^@^@^@
cstring^@^@^@^@^@^@^@ TEXT^@^@^@^@^@^@^@^@^@^@^@^@.<80>^@^@^AX
^@^@^^<80>^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^ A^@^@^AÐ DATA^@^@^@^@^@^@^@^@^@^@^@^@0^@^@^@^P^@^@^@
print “hello world”
Hello world
Trang 24Python 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
Trang 26Running Python Interactively
Trang 28This is a good test to make sure that you have
Python correctly installed
Trang 29Python 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
Trang 30Python as a Language
Trang 31Early 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
Trang 32Elements 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
Trang 33name = raw_input("Enter file:")
for word,count in counts.items():
if bigcount == None or count > bigcount:
bigword = word
bigcount = count
print bigword, bigcount
python words.py Enter file: words.txt
to 16
A short “Story” about how to count words in a file in
Python.
Trang 34Reserved 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
Trang 36Programming Paragraphs
Trang 37Program 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
Trang 38x = 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
Trang 39Conditional Steps
Output:
SmallerFinis
Trang 40Repeated Steps
Output:
54321Blastoff!
Program:
n = 5
while n > 0 : print n
n = n - 1print 'Blastoff!'
Trang 41Stored (and reused) Steps
Output:
HelloFun
Zip
HelloFun
Program:
def hello(): print "Hello"
Trang 42name = raw_input("Enter file:")
for word,count in counts.items():
if bigcount == None or count > bigcount:
Trang 43An Animated Short Story
Finding the largest number in a list of numbers
Trang 463 41 12 9 74 15
What is the Largest Number?
Trang 473 41 12 9 74 15
What is the Largest Number?
largest_so_far -13 41 74
Trang 48name = raw_input("Enter file:")
for word,count in counts.items():
if bigcount == None or count > bigcount:
bigword = word
bigcount = count
print bigword, bigcount
A short “Story” about how to count words in a file in
Python.
A paragraph about how to find the largest item in a list.
A sentence about updating one of many counts.
A word used to read data from a user.
Trang 49• This is a quick overview of Chapter 1
• We will revisit these concepts throughout the course
• Focus on the big picture