While loops

In class exercises
Run update21 and change to your w04-loops directory.
[~]$ cs cs21/inclass 
[inclass]$ cd w04-loops/
[w04-loops]$ ls
fivequestions.py  syracuse.py      whileLoop.py
listOps.py        randOps.py       stringOps.py
While loops
In addition to for loops, python supports while loops which combine elements of a for loop and an if statement. Open whileLoop.py to see a basic example.
The random number module

Sometimes we want the computer to pick a random number in a given range, pick a random element from a list, pick a random card from a deck, flip a coin, etc. The random module provides access to functions that support these types of operations. The random module is another library of functions that can extend the basic features of python. The math modules is another example of extending core python functionality. For a full list of python modules, see the online documentation listing all of the default modules. To get access to the random module, we add from random import * to the top of our program (or type it into the python shell).

Open the file randOps.py in vim, and run the program in a separate terminal. Note if you run the program again, you get different (random) results. This program illustrates the functions randrange and random. We will most often be using randrange. This function generates a list just like range, but then returns one item randomly from that list.

Strings as objects
Open stringOps.py in vim. In a separate window, change to the w04-loops directory so you can run the program using python stringOps.py. Strings are a special type of a python class. As objects in a class you can call methods on string objects using the .methodName() notation. For help on string methods type help(str) in a python shell. They are built into python by default.

The program stringOps.py covers some of the string methods you are likely to use. Read over the program, try a few extra examples in a python shell, and let me know if you have any questions.

Lists as objects
Lists are objects too. Open listOps.py. An important method for lists is append(item). Look at the sample code to see how append works. Note that it modifies the existing list. It does not create a new list.

Another new syntactic feature is the if <item> in <list>:. This statement evaluates to True and executes the body of the if statement if the specified item is present one or more times in the given list.

List elements can be modified. The syntax l[0]=x changes the contents of the first item in the list l to now contain the element x. Even though strings support index and slicing syntax (e.g., s[0], s[1:3], strings cannot be modified in this way. You can however convert a string to a list of characters, modify the list and then convert the list back to a string. The end of the program shows an example of this. Could this be how the method s.replace(old, new) works in the string class?

For more on list methods, type help(list) in a python shell. We will talk about some of these methods later in the course as we need them.