Programming Digital Media

Python as a language

Analogies between Python and human language

Words

     Some words are English ("reserved words")
        print
        list
        for
        not
        while
     Some words are symbols
        +
        *
        /
        =

Punctuation

Group arithmetic operations with parentheses

    (1 + 2) * 3

Create lists using square brackets and commas

    [1, 2, 3]

Dependent clauses

"Expressions" in Python

    1 + 2 + 3
    (1 + 2 + 3) * 10
    ((1 + 2 + 3) * 10) / 7

Sentences

"Statements" in Python

    print "A red circle"
    red = 1
    red_color = [1, 0, 0]

Paragraphs

"Blocks" in Python

     Statements with the same amount of indentation

Essay

Essay (uh, oh ... the analogy is breaking down)

Beyond the language analogy

A "function" in Python

     Predefined Python functions
        range(5) => [0,1,2,3,4]

Defining a new Python "word"

    def add_one(number): 
        return number + 1 
 
    current = 12 
    next = add_one(current) 

Beyond the language analogy

A "script" in Python

     A Python program
     A set of functions and statements that implements some process

We will be writing Python scripts that will act just like Unix commands.

Beyond the language analogy?

"It should not be forgotten that communication is just one part of language. Language is also a means of conceptualization, a way of organizing the world, and thus is much more than mere communication."

          Roland Barthes




Words now, pictures soon.

The language categories in Python

Naming

Choosing

Repeating

Grouping

Naming

"Variables" in Python

    height = 71.9
    digits = [0,1,2,3,4,5,6,7,8,9]
    red = .3
    green = .4
    blue = .5
    color = [red, green, blue]
    image_filename = 'waterfall.0042.tif'

Choosing

"Conditionals" in Python

     If (something is true) then (do this).
    if red < .5: 
        print 'Dark red' 

Choosing

"Conditionals" in Python

     If (something is true) then (do this) else (do that).
    if red < .5: 
        print 'Dark red' 
    else: 
        print 'Bright red' 

Repeating

"Loops" in Python

Use each item in a list in turn

    for n in [1, 2, 3]: 
        print n 

Repeat while a condition holds

    n = 1 
    while n < 10: 
        print n 
        n = n + 1 

Grouping

"Blocks" defined by indentation

    n = 1 
    while n < 10: 
        print n 
        n = n + 1 
    print n 

Grouping

"Functions" are an abstract definition of a process

          Uh...

Grouping

Functions process stuff

Grouping

Functions process input to produce output

Grouping

Functions take arguments and return a result

Grouping

Functions take arguments and return a result

    def add(a, b): 
        return a + b 
    sum = add(98,2) 

The basic programming language categories

Naming

     "variables"

Choosing

     "conditionals"

Repeating

     "loops"

Grouping

     "functions"

Tutorial in the class website: Beginning Python

Naming

     "variables"

Choosing

     "conditionals"

Repeating

     "loops"

Grouping

     "functions"