Enumerate and readlines

23Sep08

If you have Python on your computer, you have access to a powerful way to learn: the Python interpreter itself. It allows you to interactively test out code and see the result. So with that said, fire up your Python interpreter. If you’re on windows, either open your command prompt (start menu->run->cmd.exe) and type “python,” or navigate to your start menu, click on programs, then find Activestate Python and click the interpreter.

If you’re on Mac or Linux, open your terminal and simply type “python” — It should look like something like this:

Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Picking up from the last post (sorry this didn’t get up sooner):

If you don’t have one already, create a file called links.txt on your desktop and add in a few dummy links on separate lines. Three should suffice. Now, at the “>>>” prompt, paste in the following: f = file("C:/Documents and Settings/YOUR-USER-NAME/Desktop/links.txt", "r") and hit enter. Nothing should happen, and that’s fine. All you did was open a file.

Now, type f.readline() and hit enter. Woah! The first link in your file. An aptly named function, eh?

So if f.readline() reads a single line, it stands to reason that f.readlines() will collectively read in all of the lines in a file. It also does something extra useful, which is split them up into a list by line. In Python, you access elements of a list with the [] operator, so f.readlines()[0] (because as we all know, in programming you index starting with 0) is the same as f.readline().

However, if you call f.readline() followed by f.readlines() you might notice that the second time around, you’re missing the first link. This is because the file object remembers where you were in the file while using these functions, and reads only the lines you haven’t accessed yet.

So what’s with enumerate() and the two variables we had in that for loop before?

Go back to the interpreter and type:

>>> for x in enumerate(f.readlines()):
... print x

Make sure you hit at least two spaces before print, because Python is whitespace-sensitive, meaning things like a for loop, which will execute code within its scope, only know what to execute if it’s spaced properly. When a block of code is indented properly under other code, like a loop, we say it’s within the “scope” of the loop.

You should see a set of information displayed on your screen now for each link. We call this a tuple. It’s like a list, but you can’t change the contents. The first number is number returned from the enumerate() function, letting us know where we are in the loop. The other is the link itself.

When you supply one variable to hold the value of a function that returns a tuple, that variable will hold the tuple itself. However, you can split the tuple into two (or more) different variables by providing multiple variables to hold the values, just like we did.

So that’s the explanation I promised you on Friday. Sorry about that.

Advertisements


No Responses Yet to “Enumerate and readlines”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: