Video lectures: Python stream
download;
Image Tool: stream
download
This outline discusses a small subset of the Python programming
language. We will need this for the image processing labs.
There are a number of terms defined here, and given in italics. As you
might expect, you'll want to know those terms for the exam.
- A programming language is just a langauge for writing algorithms and
which the computer can understand.
- There are many programming languages.
C, C++, Java, C#,
Pascal, Ada, perl, Python,
Javascript, etc.
- Compiled v. Interpreted.
- Compiled: Translated to machine language (numbers).
- Interpreted: Another program reads the program and does what it says.n
- Python is interpreted.
- Programs are text files.
- You can create them with Notepad.
- We'll use a more specific tool.
- Like HTML: A text file containing directions the computer follows.
- Variables, operators and expressions.
- Variables are names for data values.
- Variables can be assigned with a value which is used later.
The α and β from
the sorting algorithm are variables that hold a position in the CD list.
x = 5
y = 3*x*x + 4*x + 18
The above gives x the value 5, then uses x to compute
another value and give it to y.
- The symbols * for multiply and + are called
operators.
- The part 3*x*x + 4*x + 18 is an expression.
- Variables can be changed:
x = x + 3
That increases the value
of x by 3.
- Setting the value of a variable is an assignment statement.
- Control statements.
- Control statements decide whether or how many times other
statements are performed by the computer.
- Conditional statements are control statements which
enable or disable other statements.
if a > b:
max = a
else:
max = b
In the above, exactly one assignment statement is performed, so that
the variable max will become equal to the larger of a and
b.
- Iteration statements are control statements which
repeat other statements.
t = 0
for n in [ 2, 5, 8 ]:
t = t + n*n
In the above, the for loop causes the (second) assignment
statement to be performed three times, increasing t three times.
The effect is to compute
22 + 52 + 82
and leave that value in t.
- Python control statements end in a colon and have control over statements
which are indented below them.
- Controlled statements are collectively called the body of the
control statement.
- Functions.
- Functions are groups of statements which is given a name.
- Functions are sent input values and perform some desired computation
based on those input values.
- Functions often send a value back to the caller using a
return statement.
- The inputs are sent using special variables called parameters
def findmax(a, b):
if a > b:
max = a
else:
max = b
return max
The above accepts two values and finds the largest. I could use it this
way:m = findmax(5,9)
This makes a become 5 and b become 9. It then performs
the if statement which will determine that 9 is larger and send it back
to where it was called from. The variable m will receive the
value 9.
- Pixels for our lab.
- Under some circumstances, variables may have parts. These parts are
really just other variables.
- In our lab, we will have variables which hold a pixel. It has three
parts, r, g and b for the red, blue and green
components.
pix.r = 0
pix.g = pix.g + 10
pix.b = 2*pix.b
In the above, pix is a pixel, and the assignment statements
set its red part to zero, increase its green part by 10, and
double its blue part.