Title here - University of Glasgow
Download
Report
Transcript Title here - University of Glasgow
Accelerated Programming 2
Part I: Python Programming
1
Python Overview
© 2010 David A Watt, University of Glasgow
The Python programming language
Python was designed by Guido van Rossum,
around 1990.
Python is a simple but high-level programming
language, suitable for a variety of purposes
– particularly scripting and application programming.
Python programs are interpreted, so:
– a program can be run immediately
– a program can be run on any computer.
1-2
Example: a first program
Here is a trivial Python program:
print 'Hullo!'
When run, this program outputs:
Hullo!
1-3
Example: a simple program
Here is a more interesting Python program:
last = 4
n = 1
while n < last:
n = n+1
print n, n**2
print '****'
When run, this program outputs:
2 4
3 9
4 16
****
1-4
Literals
A literal stands for a known value. E.g.:
1
1.0
2
10
2.5
'hullo'
2010
3.1416
'B. Obama'
(integer literals)
(floating-point literals)
(string literals)
1-5
Variables (1)
A variable contains an unknown value. The
variable may be updated from time to time.
Each variable has a name.
A name is a sequence of letters, digits, and
underscores, not starting with a digit. E.g.:
n
last
s1
running_total
You are free to choose the names of variables in
your programs. It is good practice to choose
meaningful names.
– E.g., total is more meaningful than t.
1-6
Variables (2)
Visualise a variable as a box containing a value:
total
10
When the variable is updated, change the value
inside the box:
total
14
Notice that the old value is lost when a variable is
updated.
1-7
Expressions (1)
An expression is a formula for computing a
value.
An expression may be as simple as a literal or a
variable.
An expression may be an operator applied to one
or two sub-expressions. E.g.:
n+1
-(n+1)
n**2
(a+b+c)/2
An expression may be a named function applied
to one or more sub-expressions. E.g.:
abs(10-n)
max(a, b)
1-8
Expressions (2)
An expression is evaluated to yield a value.
– That value could subsequently be printed, assigned to
a variable, etc.
E.g., if variable n contains the value 13:
n
yields 13
n+1
yields 14
-(n+1)
yields –14
n**2
yields 169
abs(10-n)
yields 3
1-9
Statements
A statement is an instruction or command to the
computer.
A statement is executed to achieve some effect.
Python has several forms of statement, including:
– print-statement
– assignment-statement
– return-statement (§2)
– if-statement (§4)
– while-statement (§5)
– for-statement (§8).
1-10
Print-statements
A print-statement displays a line of output on
the screen.
The line of output may be made up from strings,
integer literals, etc.
E.g., if variable n contains 13:
print n
displays 13
print n, n*2
displays 13 26
print 'Answer is', n
displays Answer is 13
1-11
Assignment-statements (2)
An assignment-statement stores a value in a
variable.
– An expression is evaluated to yield a value, which is
then stored in anamed variable.
E.g., if variable n contains 6:
p = 1
makes p contain 1
n
6
p
1
p = n+1
makes p contain 7
n
6
p
7
n = n+1
makes n contain 7
n
7
p
7
1-12
Assignment-statements (2)
An assignment-statement has the general form:
variable = expression
In Python, “=” means to store a value in a
variable. E.g.:
– Read “p = 1” as “store 1 in p”.
– Read “p = n+1” as “store the value of n+1 in p”.
– Read “n = n+1” as “store the (old) value of n+1 in n”.
Note in Python that “=” does not mean “is equal
to”, as in maths
– “n = n+1” wouldn’t make sense in maths!
1-13
Tracing a program
Tracing a program helps us to predict its
behaviour.
“Execute” the program on paper, one statement
at a time.
Keep track of the variables, displayed data, etc.
1-14
Example: tracing a simple program (1)
Recall this simple program:
last = 4
n = 1
while n < last:
n = n+1
print n, n**2
print '****'
This uses two variables, named last and n.
Let us trace this program, keeping track of the
values contained in last and n.
1-15
Example: tracing a simple program (2)
Trace:
last
Execute “last = 4”:
4
Execute “n = 1”:
4
1
4
1
4
2
Test “n < last”:
yields True
Execute “n = n+1”:
n
Execute “print n, n**2”:
displays “2 4”
4
2
Test “n < last”:
yields True
4
2
4
3
Execute “n = n+1”:
1-16
Example: tracing a simple program (3)
Trace (continued):
last
n
Execute “print n, n**2”:
displays “3 9”
4
3
Test “n < last”:
yields True
4
3
4
4
Execute “n = n+1”:
Execute “print n, n**2”:
displays “4 16”
4
4
Test “n < last”:
yields False
4
4
Execute “print '****'”:
displays “****”
4
4
1-17
Keywords
Certain words may not be used as names for
variables (or anything else).
These are called keywords:
and
del
for
is
raise
assert
elif
from
lambda
return
break
else
global
not
try
class
except
if
or
while
continue exec
import
pass
with
def
in
print
yield
finally
1-18
Comments
A comment is a piece of explanatory text within
a program.
It is good practice to include comments
explaining what each part of the program does.
In Python, a comment starts with “#” and
continues to the end of the line. E.g.:
last = 4
# Print squares of numbers from 2 to last …
n = 1
while n < last:
n = n+1
print n, n**2
1-19
Program layout
Generally write one statement per line.
Blank lines (and lines containing only comments)
are ignored.
Indentation is used to indicate the extent of each
block of statements.
1-20