Fall Semester 2009


Taking apart the Flag Quiz
What happens if you can't solve Homework Two?

The best answer to that is: we shouldn't let that happen.

Let's do that. That doesn't mean someone else will solve it for you.

It means you will (eventually) be able to do it yourself. Believe!

1. Let's discuss Python.

At every single moment in time we can (and will) run IDLE to check our Python.

We can also write simple programs and run them on silo. Either way works.

So what do we need to know about Python that we already don't know?

Look, we assume you know this inside-out already, OK?

#!/usr/bin/python

print "Content-type: text/html\n\n"

import cgi

data = cgi.FieldStorage()

tries = "0"
if data.has_key("tries"): tries = data["tries"].value

tries = str(int(tries) + 1)

print """
  <form>
    Your counter is now: %s <p>

    Press <input type=submit value="this button"> to add one to it.

    <hr>

     Do not write below this line for office use only. <p>

     <input type=text name=tries value=%s>

  </form>
""" % (tries, tries)
If that's actually true you have a real head start, you know that?

2. Indentation, Python and if statements.

Variables in Python have names: a, name, what are good names.

Assignments statements are like in other languages: x = 3 + 1 assigns 4 to x.

Python does not have any curly braces for blocks, so indentation is vital.

To make a test:

if x == 2: 
  pass
else:
  print "Wow."
The pass is a Python keyword for "don't do anything".

3. Strings

Strings are sequences, but before that let's see how we concatenate them.

a = "blue"
b = "berry"
print a, b
print a + b
c = a + b
print a, b, c
So that's how string concatenation works (run the code to see it).

As I said, strings are sequences. Sequences can be indexed, sliced.

To index a sequence you use an index:

a = "watermelon"
print a[0] + a[8] + a[4] + a[len(a)-1]
You noticed that you can calculate the length of a string with len.

len works the same for other sequences: tuples, lists.

There are two indexing schemes on a sequence:

That means that the last element of a sequence a is a[len(a)-1] but also a[-1]

Finally, slices are sub-sequences. You can get those with two indices (most of the time).

a = watermelon
print a[2:6]
print a[:5]  # this is the same as print a[0:5]
print a[5:]  # this is the same as print a[5:len(a)-1] or print a[5:-1] or print a[5:9]

4. Arrays

Arrays, or lists, are sequences. They are delimited with square brackets.

a = [1, 3, 2, 2, 5, 4, 1, 9]
print a[3]
print a[-1]
print a[3:7]
Of course, the elements of a list can be anything: strings, lists, lists of lists and so on.

Strings can easily explode themselves into a list of substrings.

a = "this is my song for the asking, ask me and i'll play so sweetly -- i'll make you smile..."
words = a.split(" ")
print words
Sometimes when a line is too long you can use backslash to extend it on the next line. Ask me how.

Besides being able to explode into an array strings can also glue a list into a string.

a = ['what', 'is', 'going', 'on', '?']
s = " ".join(a)
print s
You have just witnessed the blank character putting back a sentence from a list of words.

Obviously the words used for splitting or gluing don't need to be one-character only.

5. Loose ends

As you noticed some of the string methods could only be described after we discussed arrays.

So we didn't talk all there was to be talked whern we announced strings.

Likewise we have a few other things to discuss:

n = 3
m = 5
print n, m
n = n + m
m = n - m
n = n - m
print n, m
The scheme above is a clever but arcane way of swapping values of variables n, m.

A more general scheme is this parallel assignment, that works in Python because of tuples:

n = 3
m = 5
print n, m
(n, m) = (m, n)
print m, n
This would be the last thing you need to know to solve the Flag Quiz.

6. More loose ends.

Some things are not part of the language, but part of the libraries shipped with it.

For example: to shuffle a list you need to import a module, then use it.

a = ['what', 'did', 'i', 'tell', 'you', 'just', 'now', '?']
print a
import random
random.shuffle(a)
print a
Obviously we usually put the import at the top, but that's because of good style not because of necessity.

7. A basic template for handling state

Think about it.


Updated by Adrian German for A348/A548, A202/A598 and A290/A590