Problematic Python Programming

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.

Advertisements

12 responses to “Problematic Python Programming

  1. brennydoogles

    You should check out “Dive into Python”. It is a great Python book, and is up to date. You should check out my blog, because you and I have a similar history when it comes to programming (I started out with TI-BASIC and then moved on to BASH), and I write about Python quite a bit. http://brennydoogles.wordpress.com

    • Just grabbed a copy of the book (thanks for the link on your blog!), and Project Euler also looks pretty interesting as well… I might be using some of those problems for more random programming exercises. And I’ll definitely be checking out your blog more often as well (just read a couple of posts).

  2. brennydoogles

    I’m going to toss your blog up on my blogroll if you don’t mind… a fellow XKCD fan and Python programmer is always welcome. By the way, are you familiar with Generators in Python? Your Collatz function would be interesting to do as a generator (although I’m not sure it would handle the recursion well). You should check out my post about the Python “yield” keyword to learn more.

    • Of course I don’t mind! As for generators, no I haven’t heard about them. I’ll check your post out right now…

      EDIT: Just looked. Not sure if the generator would help out since the Collatz numbers follow multiple branches instead of just a single linear path, but I’ll think it over and see if it could possibly be implemented.

  3. brennydoogles

    I will make a generator out of it and show you what I mean. Also, wordpress has a built-in method for displaying source code on your blog. if you do

    
    

    it will automatically display it with line numbers, syntax highlighting, and the ability to copy the code just by hovering over it.

  4. Pingback: Collatz Conjecture in Python « Brennydoogles' Programming Blog

  5. brennydoogles

    Ok, this conversation was intriguing enough for me to blog about. Let me know what you think!

  6. brennydoogles

    Hey, I posted a new entry with some performance testing between the two approaches, let me know what you think!

  7. i genuinely love your own posting kind, very charming,
    don’t give up and keep creating simply because it just simply worth to read it.
    excited to browse through even more of your well written articles, stunning day!

  8. Pingback: The Importance of the Self « Inside a Calculator