03 - Introduction to Programs and Python
-
Upload
sandile-synthaxerror-mabika -
Category
Documents
-
view
223 -
download
0
Transcript of 03 - Introduction to Programs and Python
-
8/20/2019 03 - Introduction to Programs and Python
1/42
Introduction to Programs andPython
Introductory Computing
-
8/20/2019 03 - Introduction to Programs and Python
2/42
Computers want to be helpful...
• Computers are b uilt for one p urpose - t o dothings f or us
• But we need to speak t heir language todescribe what we want done
• Users have i t easy - s omeone already p utmany d ifferent programs ( instructions) intothe computer and users just pick t he oneswe want to use
WhatNext?
WhatNext?
WhatNext?
WhatNext?
-
8/20/2019 03 - Introduction to Programs and Python
3/42
Programmers AnticipateNeeds
• iPhone Applications ar e a market
• iPhone Applications h ave over 3 Billiondownloads
• Programmers h ave left their jobs t o befull-time iPhone developers
• Programmers know the ways of theprogram
PickMe!
PickMe!
PickMe!
PickMe!
-
8/20/2019 03 - Introduction to Programs and Python
4/42
Users vs. Programmers
• Users s ee computers as a s et of tools - word processor, spreadsheet, map,todo list, etc.
• Programmers learn the computer “ways ” an d the computer language
• Programmers have s ome t ools that allow them to build new tools
• Programmers s ometimes w rite tools f or lots of users and sometimesprogrammers w rite little “helpers ” for themselves to automate a task
-
8/20/2019 03 - Introduction to Programs and Python
5/42
ComputerHardware + Software
Networks....From a software creator ’s point of view, we build the software.
The end users (stakeholders actors! are our masters " who wewant to please " often the# pa# us mone# when the# are pleased.$ut the data, information, and networks are our problem to solveon their behalf. The hardware and software are our friends and
allies in this %uest.
&nformation'ata
User
ogram
-
8/20/2019 03 - Introduction to Programs and Python
6/42
Why learn to code?Programming is a p owerful tool you can use to solve all kinds ofproblems
• What do you want to do?
Build a p hone app to help you find directions
Calculate how much m oney you n eed t o buy a c ar
See what people are saying a bout your business on socialmedia
Program a wearable d evice so i t tweets you when youshould re-apply sunscreen
-
8/20/2019 03 - Introduction to Programs and Python
7/42
Why be a programmer?
• To get some t ask done - we are t he u ser and programmer
> Clean up survey d ata
•To produce something for others to use - a p rogramming job> Fix a p erformance p roblem in the Sakai software
> Add guestbook to a w eb site
-
8/20/2019 03 - Introduction to Programs and Python
8/42
h# )#thon*
There are a T of di-erent pro rammin lan ua es out there
)#thon is one of the easier ones to learn
There are lots of free tools out there #ou can use to code or learn)#thon
There are a lot of di-erent wa#s to use )#thon code
-
8/20/2019 03 - Introduction to Programs and Python
9/42
/nd as a bonus
nce #ou learn how to code in one pro rammin lan ua e it will be easier to learn anotherpro rammin lan ua e, and another, and another0
C1
2avaScript
C++
)erl
***
-
8/20/2019 03 - Introduction to Programs and Python
10/42
$ut let3s be clear about somethin 0
4ou won3t learn enou h in this course to start addin special e-ects tothe ne5t bi superhero movie
4ou & learn enou h to start solvin real world problems with code
6 to 7ust start havin some fun
-
8/20/2019 03 - Introduction to Programs and Python
11/42
So how do & et started*
-
8/20/2019 03 - Introduction to Programs and Python
12/42
How do & know & installed ever#thincorrectl#*
There is a tradition amonpro rammers
e alwa#s test our installation b#writin the same pro ram8
Hello o
-
8/20/2019 03 - Introduction to Programs and Python
13/42
Creating your Hello World program!
-
8/20/2019 03 - Introduction to Programs and Python
14/42
You have now created your first applicationprint( 'Hello World'
)
-
8/20/2019 03 - Introduction to Programs and Python
15/42
Congratulations you are now a coder
-
8/20/2019 03 - Introduction to Programs and Python
16/42
What is Code? Software?A Program?
• a seq uence of instructions t hat specifies ho w to perform a co mputation
> It is a little piece of our intelligence in the computer
> It is a little piece of our intelligence we can give to others - w e figuresomething o ut and then we e ncode i t and then give i t to someone e lse t osave them the time and energy of figuring it out
• A piece of creative art - p articularly when we do a g ood job on userexperience
-
8/20/2019 03 - Introduction to Programs and Python
17/42
Some basic i nstructions•input : data you type a te t you load
•output : display data on the screen send data to a file
•math : perform "asic mathematical operations li#e $ % & :
•conditional e ecution : chec# for certain conditions ande ecute the appropriate instructions
•repetition : perform some action repeatedly usually withsome variation
'rogramming ( "rea#ing a large comple tas# into smallerand smaller su"tas#s until the su"tas#s are simple enoughto "e performed with one of these "asic instructions
-
8/20/2019 03 - Introduction to Programs and Python
18/42
Python as a Language
-
8/20/2019 03 - Introduction to Programs and Python
19/42
http:// harrypotter.wikia.com/wiki/Parseltongue
Parseltongue is t he language of serpents an dthose who can converse with them. Anindividual who can speak Parseltongue is
known as a Parselmouth . It i s a veryuncommon skill, and may b e h ereditary.
Nearly all known Parselmouths are d escendedfrom Salazar Slytherin .
http://harrypotter.wikia.com/wiki/Salazar_Slytherinhttp://harrypotter.wikia.com/wiki/Salazar_Slytherin
-
8/20/2019 03 - Introduction to Programs and Python
20/42
Python is the language of the PythonInterpreter and those who can converse with it.An individual who can speak Python is knownas a Pythonista . It is a v ery uncommon skill,
and may b e h ereditary. Nearly a ll known
Pythonistas u se software initially developed by Guido van Rossum .
-
8/20/2019 03 - Introduction to Programs and Python
21/42
Compilers and Interpreters• Two kinds of programs process high-level languages into low-level
languages : interpreters and compilers .
• An interpreter reads a h igh-level program and executes i t, meaning that itdoes w hat the program says. It processes t he program a l ittle at a t ime,
alternately reading lines an d performing computations.
-
8/20/2019 03 - Introduction to Programs and Python
22/42
Compilers and Interpreters• Acompiler reads the p rogram and translates it completely b efore t he
program starts r unning.
• In this ca se, the high-level program is ca lled the source cod e , and thetranslated program is cal led the object code or the executable .
• Once a program is compiled , you can execute it repeatedly without furthertranslation.
-
8/20/2019 03 - Introduction to Programs and Python
23/42
Early Learner: Syntax Errors• We n eed to learn t he Python language so we can communicate our instructions
to Python. I n the b eginning w e w ill make l ots of mistakes an d speak g ibberish l ikesmall children.
• When you m ake a m istake, the computer does not think you are “cute“syntax e rror ” - gi ven that it *knows* t he language and you are just learning it. I tseems l ike P ython is cr uel and unfeeling.
• You must remember that *you* are intesimple and very fast - b ut cannot learn - s o it is ea sier for you to learn Pythonthan for the computer to learn English ...
-
8/20/2019 03 - Introduction to Programs and Python
24/42
Early Learner: Debugging• Programming is a c omplex p rocess, and because i t is done b y h uman beings, it
often leads t o errors. For whimsical reasons, programming errors ar e calledand the p rocess of tracking t hem down and correcting t hem is called bng
•)hree #inds of "ugs:
•synta errors % means that you have violated the “grammar” rules of
'ython•runtime errors % do not appear until after the program has started to ru
•semantic errors % program will run perfectly "ut it will not produceresults you wanted: the meaning of the program *semantics+ is wrong
-
8/20/2019 03 - Introduction to Programs and Python
25/42
-
8/20/2019 03 - Introduction to Programs and Python
26/42
csev$ pythonPython 2.5 (r25:51918, Sep 19 2006, 08:49:13)[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwinType "help", "copyright", "credits" o r "license" f or moreinformation.>>>
What next?
-
8/20/2019 03 - Introduction to Programs and Python
27/42
csev$ pythonPython 2.5 (r25:51918, Sep 19 2006, 08:49:13)[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwinType "help", "copyright", "credits" o r "license" f or moreinformation.>>> x = 1>>> print x1>>> x = x + 1>>> p rint x2>>> exit()
This is a g ood test to make sur e that you havePython correctly installed. Note t hat quit()also works t o end the interactive session.
-
8/20/2019 03 - Introduction to Programs and Python
28/42
Let’s T alk to Python...
-
8/20/2019 03 - Introduction to Programs and Python
29/42
-
8/20/2019 03 - Introduction to Programs and Python
30/42
Elements of Python
• Vocabulary / W ords - Variables and Reserved words (Chapter 2)
•Sentence st ructure - va lid syntax patterns (Chapters 3 -5)
• Story st ructure - constructing a p rogram for a p urpose
-
8/20/2019 03 - Introduction to Programs and Python
31/42
Reserved Words
• You can not use reserved words a s va riable names / identifiers
and del for is raise assert elif from
lambda return break else globalnot try class except if or whilecontinue exec import pass yield
def nally in print as with :
-
8/20/2019 03 - Introduction to Programs and Python
32/42
Sentences or Lines
x = 2x = x + 2print x
Variable Operator Constant Reserved Word
Assignment statementAssignment with expression
Print statement
-
8/20/2019 03 - Introduction to Programs and Python
33/42
Programming Paragraphs
-
8/20/2019 03 - Introduction to Programs and Python
34/42
Python Scripts
• Interactive Python is go od for experiments an d programs of 3-4 lineslong
• Most programs are m uch longer, so we type t hem into a f ile a nd tellPython to run the commands in the file
• In a s ense w e ar e “giving Python a scr ipt ”
• As a convention, we ad d “.py ” as t he suffix o n the end of these files t oindicate they contain Python
-
8/20/2019 03 - Introduction to Programs and Python
35/42
Writing a Simple Program
-
8/20/2019 03 - Introduction to Programs and Python
36/42
Interactive versus S cript
• Interactive> You type directly to Python
• Script> You enter a sequence of statments and tell Python to execute the statements i n the file
-
8/20/2019 03 - Introduction to Programs and Python
37/42
Program Steps or Program Flow
• Like a r ecipe or installation instructions, a p rogram is a se quence ofsteps to be d one in order
• Some st eps are conditional - they may b e skipped
• Sometimes a s tep or group of steps are t o be r epeated• Sometimes w e st ore a set of steps to be u sed over and over as
needed several places t hroughout the program (Chapter 4)
-
8/20/2019 03 - Introduction to Programs and Python
38/42
Sequential Steps
Program:
x = 2print xx = x + 2print x
Output:
24
x = 2
print x
x = x + 2
print x
When a p rogram is running, it flows from one st ep to the n ext.As programmers, we set up “paths ” for the program to follow.
-
8/20/2019 03 - Introduction to Programs and Python
39/42
Conditional Steps
Program:
x = 5if x < 10: print ' Smaller’
if x > 20: print ' Bigger'
print ' Finis'
x = 5
x < 10 ?
print 'Smaller'
x > 20 ?
print 'Bigger'
print ' Finis'
Yes
No
-
8/20/2019 03 - Introduction to Programs and Python
40/42
Repeated StepsProgram:
n = 5while n > 0 : print n n = n – 1
print 'Blasto ff!'
n > 0 ?
Loops (repeated steps) have iteration variableschange e ach t ime t hrough a loop. Often these
iteration variables go t hrough a sequence o f numbers.
No
print ' Blasto ff'
Chapter 5
Yes
n = 5
print n
n = n -1
-
8/20/2019 03 - Introduction to Programs and Python
41/42
,c#nowledgements - Contri"utions
-
8/20/2019 03 - Introduction to Programs and Python
42/42
g
)hese slides are Copyright ./0/% Charles 12 3everance *www2dr%chuc#2com+ of the 4niversity of 5ichigan 3chool of6nformation and open2umich2edu and made availa"le under aCreative Commons ,ttri"ution 72/ 8icense2 'lease maintain thislast slide in all copies of the document to comply with theattri"ution re9uirements of the license2 6f you ma#e a changefeel free to add your name and organi ation to the list ofcontri"utors on this page as you repu"lish the materials2
6nitial ;evelopment: Charles 3everance 4niversity of 5ichigan3chool of 6nformation
< 6nsert new Contri"utors and )ranslators here
222
http://var/www/apps/conversion/tmp/scratch_4/www.dr-chuck.comhttp://open.umich.edu/http://open.umich.edu/http://var/www/apps/conversion/tmp/scratch_4/www.dr-chuck.com