First Summer 2006


Lab Six: Python, the road ahead.
At this stage we hope everybody has the Python book and you're reading it.

The first two chapters should be thoroughly clear by now.

Let's look at chapters 3, 4 and 5 now.

But before that here's your lab assignment for today:

Lab Assignment This is your lab assignment for today, Thursday May 18, 2006:

Chapter 4 and 5 develop a number of programs as examples.

Please turn in:

Turn in your programs in OnCourse (under A201-7055) by the end of the day.

Random numbers could be obtained by

You can find more information about that here (and on page 56 in the text).

Comparison operators are listed in the text on page 59.

A first use of the if structure is on page 58 in the text.

Of particular importance is the section "Using Inentation to Create Blocks" (on page 60).

There are three types of if structures:

There's an example on pp. 64-65, and a summary on page 66, at the bottom.

Next the while loop is introduced (page 67).

Take a look at this program:

i = 1
if (i < 10): 
  i = i + 1
print i

Compare it with this program:

i = 1
while (i < 10): 
  i = i + 1
print i

Do you see the difference in i's value at the end of the program?

Infinite loops are discussed on page 69.

The nature of truth values in Python is revisited on page 73.

Two statements are sometimes convenient in conjuction with a loop:

Pages 77-78 discussed when and how to use them.

Another example (pp. 79-80) leads into a discussion of boolean operators on pp. 81-84.

Chapter 3 ends with an example of step by step design of a program (pp. 84-88).

With the next chapter another variant of loop (the for loop) is introduced.

Along with it we study sequences, as for loops are more suited for iteration over sequences.

Strings could be traversed as follows:

a = raw_input("Enter a string: ")
for c in a: 
  print c

Counting with a for loop is done over a range(...) (see more here and here).

Pages 96-97 discuss example of counting (forwards, backwards, counting by fives).

Sequence operators and functions with strings are studied next (page 97).

The len(...) function is mentioned, then the boolean in operator is discussed (page 99).

Indexing strings is done as discussed in class on Wednesday, see pages 99-101 in the text.

Positive positions with a string are intuitive, negative positions allow indexing from the end of the string.

An example of all possible indexings of a string is shown on page 104.

Strings are immutable, one cannot change them.

However new strings could be built and reassigned, and the the old strings discarded.

There's an interesting program on page 106.

Creating new strings from existing ones is covered on page 108 (along with an explanation of the += operator).

Slicing strings is the topic of pages 108-110 (we discussed this on Wednesday in class).

The use of the value None (similar to null in other languages) is discussed on page 110.

Slicing is discussed in reasonable detail on pp. 111-112.

The discussion then moves to tuples.

Tuples are our first example of data structure.

Since they are so similar in how they are addressed to strings, their discussion is appropriate for this chapter.

The only difference is in the syntax: ("one", "two", "three", "four") is different from "abcd", but functions in a similar way.

Page 121 also presents the use of the function random.choice(...) to select randomly from a sequence.

The next chapter we will review today concludes our introduction to data structures.

Lists are sequences, just like tuples, only mutable.

Notice the syntax (for the initialization of a list) is with square brackets.

Everything we knew about strings was useful for tuples, and is now useful for lists.

The novelty is that lists can change.

Deleting a slice is discussed on page 134.

Some common list methods are summarized on page 138.

One can nest these structures, so pp. 140-144 discuss nested sequences.

Shared references are discussed on pages 144-146, and we move to dictionaries.

Dictionaries are the first (pure) two-dimensional structures so far.

(Nested sequences are also two or more dimensional).

Dictionaries methods are listed on page 155.

Curly braces and a special syntax identify these new structures.

Creating dictionaries is covered on page 148, accessing dictionary values on pp. 149-150.

Adding, replacing and deleting a key-value pair are on pp. 151-154.

The rest of the chapter designs and implements the Hangman game.

This was a short summary of what can be found in chapters 3, 4, and 5.


Last updated: May 17, 2006 by Adrian German for A201/A597