A few days ago, my core group of friends finally decided to give me my “Welcome back to Earth!” gift that they had gotten for me. After tearing through the layers of tissue paper, duct tape, newspaper, Franzia and Hot Pockets boxes, I finally got to the actual gift: a Python programming book! Technically speaking, this Python programming book. Since I’ve never learned a programming language from a book, or in any systematic manner whatsoever,* this is a pretty new experience for me. Yes, I do have a PDF book that one of my past Astrophysics professors gave the class, but I haven’t really dipped into that one (just the first few chapters, mostly covering things I already knew). So, for the past couple of days, I’ve been learning Python according to the way a group of four computer scientists feel it should be taught.
And I have to say, they did a pretty good job! The only problems I’ve had with the book so far (I’m 93 pages in) is the fact that some of the modules required to complete some of the exercises or test the examples aren’t compatible with Python 2.6.4, which is what I’ve been using. They are only compatible up to version 2.5, so I may “downgrade” to 2.5 so that I fully utilize the book. Unfortunately, those modules are also the pretty cool ones, like media that allows you to edit and display images. For that reason alone, I will probably drop down, but I haven’t fully decided to make that plunge yet.
I also recently encountered a problem with the open() command that took a while to solve. I encountered this on page 92, and spent a good hour sifting through various documentation sources to figure out why it wasn’t working for me. I finally concluded that Python was setting the working directory to the root (/Users/mikemoran), so only files there could be opened. Using some of my Unix-based knowledge, I figured that I could include the path to a file in the open command to access files elsewhere, and I was right! It’s the little triumphs that really matter, after all. Once I solved that, I decided to take a break from the book for the night, and to grab some sleep (since it is pretty late here…), and coincidentally, write this post up.
So obviously I got distracted, but not just by this post. I checked out XKCD, as I always do to read the most recent upload, and as always, I clicked the previous button to check that I didn’t skip a comic. Of course, I read this comic on Friday when it came out, but I didn’t really think about it that much. This time, however, I decided to read up on the Collatz Conjecture on Wikipedia, and while doing so, I decided to write a program to print the step sequence for a given integer:
def collatz(start): print start, if start > 1: if start % 2 == 0: start = start / 2 collatz(start) else: start = start * 3 + 1 collatz(start) collatz(10) 10 5 16 8 4 2 1
Of course, this doesn’t save the output since it was just a fun little exercise for me to get back into writing actual programs instead of just copying those examples in the book or editing some of my older ones. Plus, it’s really interesting, and I’ll get to expand and improve that simple command as I get bored with the book. For example, it needs error checking to make sure that “start” is an integer, among other small edits and tweaks.
I’m hoping to finish half the book by the end of Spring Break (which started on Friday for me), since I do have to work on actual homework at some point as well. I want to get ahead in school, after all!
* – I’ve only learned the various programming languages (Python, Unix, TI-BASIC) and webpage design (HTML and CSS) by teaching myself and through someone else giving me some initial guidance. I would just look things up online as I needed them, check out examples and try to figure them out by myself, and figuring things out as I stumbled through the learning process. Unix and Python are also the only ones that I had any outside guidance on, from Kiri Wagstaff and Prof. Robert Bell, respectively. Of course, once their tutelage ended, I was back to figuring things out as I went along.