Programming and Python

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.

  1. A programming language is just a langauge for writing algorithms and which the computer can understand.
    1. There are many programming languages.
      C, C++, Java, C#, Pascal, Ada, perl, Python, Javascript, etc.
    2. Compiled v. Interpreted.
      1. Compiled: Translated to machine language (numbers).
      2. Interpreted: Another program reads the program and does what it says.n
      3. Python is interpreted.
    3. Programs are text files.
      1. You can create them with Notepad.
      2. We'll use a more specific tool.
      3. Like HTML: A text file containing directions the computer follows.
  2. Variables, operators and expressions.
    1. Variables are names for data values.
    2. 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.
    3. The symbols * for multiply and + are called operators.
    4. The part 3*x*x + 4*x + 18 is an expression.
    5. Variables can be changed:
      x = x + 3
      That increases the value of x by 3.
    6. Setting the value of a variable is an assignment statement.
  3. Control statements.
    1. Control statements decide whether or how many times other statements are performed by the computer.
    2. 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.
    3. 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.
    4. Python control statements end in a colon and have control over statements which are indented below them.
    5. Controlled statements are collectively called the body of the control statement.
  4. Functions.
    1. Functions are groups of statements which is given a name.
    2. Functions are sent input values and perform some desired computation based on those input values.
    3. Functions often send a value back to the caller using a return statement.
    4. 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.
  5. Pixels for our lab.
    1. Under some circumstances, variables may have parts. These parts are really just other variables.
    2. 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.