CoCalc Shared Fileswww / wiki / 10(2f)480b(2f)lectures(2f)lec3.html
Author: William A. Stein
10/480b/lectures/lec3
 [top] [TitleIndex] [WordIndex]

Lecture 3, 4/2/10

(Still woefully incomplete)

A few more words about our first Python program

More on lists

So last time, we talked about how to create lists, and started to hint that there were plenty of interesting things you could do with lists. Of course, we forgot the most basic thing: once you put something in a list, how do you get it back out? You can access the elements of a list with brackets, where the first element of the list is stored at index 0:

>>> ls = range(5)
>>> ls[3]
3
>>> ls[0]
0


If it helps, you can think of the index as telling you how many elements past the first an entry is stored. This is also why range(n) starts at 0 -- the entries in range(n) are exactly the indices of all the elements of a list containing n elements. This is also referred to as the length of the list:

>>> len(range(30))
30
>>> len([5,2] * 30)
60


Python also does one more thing to make lists especially convenient: rather than just counting from the start of the list, you can count backwards:

>>> ls = range(10)
>>> ls[-1]
9
>>> ls[-10]
0


Of course, this means that the valid positive indices and the valid negative indices are different -- you can't win 'em all, I guess.

There are a bunch of other useful things you can do with lists -- we'll talk about them soon, but if you just can't wait, you can look [http://docs.python.org/tutorial/datastructures.html#more-on-lists here] or, if you're really feeling motivated, [http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange here].

Classes and objects

I've been using the term "object," because it has a fairly intuitive meaning. In fact, it also has a technical meaning in the world of computer science. The notion of object-oriented programming (OOP) is a way of structuring programs that packages together related functions and data into classes and objects -- classes are the templates, and objects are the actual instantiations that exist in the program. We'll talk a lot more about this soon, but I want to stop and mention now that just about everything in Python is an object. (Well, not quite at the Smalltalk/Squeak level, but close.) Numbers, functions, strings, lists -- they're all objects. In particular, it means that the way we customize various kinds of Python behavior is uniform across the board. ("There should be one, and preferably only one, obvious way to do it.")

Running Python programs as scripts

So now we've pretty much discussed all the pieces of collatz.py; we still haven't talked about what happens when you actually run it from the command line, though. Luckily, it's really straightforward: it just steps through the source file, and runs every line of code that it sees. So the first thing it sees is a string that starts with """, followed by a definition of a function called collatz, and then this business with if __name__ == '__main__'. So let's talk about this last part: it's an if statement, which we're now at least a little familiar with -- and the __name__ == '__main__' bit just says "only do this stuff if we're being run as a script. So, when we something like python collatz.py 3 at the command line, this is exactly what we're doing. Of course, there's another possibility -- using the import statement to play with our code from the Python interpreter.

Modules and the import statement

One of the most basic and important lessons to learn about programming is the value of modularity -- the ability to take parts of the programs you write and reuse them again later. (One could cynically claim that most, but definitely not all, of the business of software engineering is attempting to make code more easily composable.) The absolute first step is the ability to make the functions in one part of the code available in another -- and this is exactly what the import statement is for.

2013-05-11 18:32