strings as objects, lists as objects, and the random library

Sample code
In the code you copied over from my public/cs21/week06 subdirectory are three files, stringOps.py, listOps.py, randomOps.py, that contain example code of using strings as objects, lists as objects, and using the random library.
Strings as objects
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. The string class is available by default in python, and using string methods is preferred to using string functions in python's string module. Now that we know about objects and methods, you do not need to add from string import * anymore to get string functions. For help on string methods (recall that a method is just a function that is part of a class and uses the dot notation), type help(str) in a python shell. Again, you do not need to import anything to get access to these methods. 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. An important method for lists is append(item). Look at the sample code in listOps.py 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.

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. Other modules we have seen so far are string, math, time and graphics. With the exception of the graphics module, all of these modules are built into python. 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.