Journey to Python Part 1: First Steps

After yesterdays rather long introduction into the whys and wherefores of my decision to learn python, today I’m going to begin to talk about my first steps into learning python. In this post I’ll talk about how I fared getting up and running with python, what references I chose, and discuss my impressions after writing my first program.

For someone who does not have experience with programming, getting started developing can be a daunting task. Having the advantage of being a developer, and knowing several other languages, I started out today with a general idea of what I would need to get started and write my first application. To get started I knew that I would need:

  • the Python interpreter, to run the applications
  • an editor, to write applications
  • documentation, to know how to write the programs
  • a program to write

Getting the environment up and running

Since I have been running Python applications before starting development, I knew that I already had the basic environment that I would need. For those of you following along at home, running linux you will probably find that you already have linux installed, or can easily find the packages on your installation disks or in your distribution repositories (Ubuntu and Debian users install the python2.5 and python2.5-doc packages from your repositories). OS X users should already have python installed, and Windows users can grab python from python.org

Setting up a development environment

Once again, I was able to benefit from previous experience. Since I prefer to do my C, C++ and Perl development from the command line with vim, I was ready to go with my editor of choice. Vim already had support for python syntax, and with syntax highlighting and auto-indent already enabled, I had nothing to do to get started. If you haven’t taken the time to set up a development environment already, you can grab my vimrc file, and save it to ~/.vimrc to get a head start (ubuntu users: if you haven’t done so already, you may want to install a more full featured vim (e.g. vim-gnome or vim-full) since the default vim installed on ubuntu is rather limited)

Documentation

Although man pages are a useful reference, they aren’t the best way to get started learning a new programming language. To get started, I decided to check out the Official Tutorial. The tutorial itself seems to be pretty good. I’ve made it through section 4 so far, and it seems to be a good reference. Of course, I also find having a dead-tree reference to be useful, not only for learning but for referring back to later. To supplement the official tutorial, I decided to pick up O’Reilly’s Learning Python since I’ve always been a fan of the Learning $foo series from O’Reilly. I personally prefer Learning Python to the official tutorial, since it seems to get into the details more quickly, plus it has exercises that will serve useful later when I run out of ideas for sample programs to write.

Getting Stuff Done

With an editor and interpreter ready to go, and documentation on hand, I set to work writing my first Python application. I wanted to get a basic feel for the language, and be able to start with something extremely simple. To start with, I knew that I wanted to be able to demonstrate:

  • Putting text on the screen
  • Using conrol structures (loops, branching)
  • Create a function
  • Do some math
  • Use a library

I decided to start with a simple program that would calculate some interesting facts about a list of numbers, and print them to the screen. At this point I am more concerned with simply being able to use the language than being idiomatic about it. Below is my first ever Python application:

#!/usr/bin/python
import math

def fibnum(n):
    if n <= 1:
        return 1
    else:
        return fibnum(n-1) + fibnum(n-2)

def factorial(n):
    if n <= 1:
        return 1
    else:
        return n * factorial(n-1)

def isprime(n):
    if n < 2:
        return False
    for x in range(2,(int)(math.sqrt(n))+1):
        if ((n%x) == 0):
            return False
    return True

for x in range(0,10):
    print "%(x)dth fibonacci number is %(fib)d"%{"x":x,"fib":fibnum(x)}
    print '%(x)d! =%(fact)d'%{'x':x,'fact':factorial(x)}
    print x,"is",
    if not isprime(x):
        print "not",
    print "prime"

and here is the output:

0! =1
0 is not prime
1th fibonacci number is 1
1! =1
1 is not prime
2th fibonacci number is 2
2! =2
2 is prime
3th fibonacci number is 3
3! =6
3 is prime
4th fibonacci number is 5
4! =24
4 is not prime
5th fibonacci number is 8
5! =120
5 is prime
6th fibonacci number is 13
6! =720
6 is not prime
7th fibonacci number is 21
7! =5040
7 is prime
8th fibonacci number is 34
8! =40320
8 is not prime
9th fibonacci number is 55
9! =362880
9 is not prime

As you can see, the program simply iterates through the numbers 0-9 and calculates the nth fibonacci number, n!, and determines if n is prime or not, and displays this information to the screen.

With my first Python program actually written and running, it’s time to go over my thoughts on day 1. To start, I found that the syntax wasn’t that bad, but at the same time, I’m not yet convinced that it’s in any way superior to C, C++, or even perl. Significant whitespaces weren’t as big of a deal as I had expected, but I found the lack of a closing curly brace ‘}’ to end a function made it hard to visually scan and see where one block of code ends and another begins. Since we already have to use a colon ‘:’ to define the start of a block, it just seems like sheer pigheadedness to not to ahead and just end a block of code with some identifiable token. The print statement also seems to be different-for-the-sake-of-being-different. The print formatting is reminiscent of C’s printf function, but the choice of “format”%{dictionary} instead of just “format”,… seems nasty to me. I will have to see if this choice imbues any greater flexibility later that makes up for it’s quirkiness. The for in loop syntax is nice, and very similar to perl’s foreach and nearly identical to php’s for in loops, and I’m glad to see at least one language feature so far that seems reasonable and intuitive to me. Finally, to pick on last nit, making ‘True’ and ‘False’ start with capital letters is irritating; I have no idea how in the world capitalizing them is supposed to make code more intuitive or readable, so once again I’m left with the thought that for all of it’s talk about doing things better, python often does things different just for the sake of it.

Final thought for today: not as painful as it could have been, but certainly less of a joy that programming in C or Perl for me so far. There are quite a few ‘features’ that I’m on the fence about right now- I’ll have to see if they turn out to be more powerful once I’ve gotten some more Python experience under my belt. I hate having to capitalize “True” and “False”.

2 Comments »

  1. noor420 said,

    August 16, 2008 @ 7:06 am

    Great website. I am new to python also and am reading this series of articles with lot of interest.

    Also, if you don’t mine, can you tell us how you feel about the the static vs dynamic typing (c++ vs python in this matter).

    Thanks a lot.

  2. codeninja said,

    August 16, 2008 @ 2:07 pm

    noor:
    Thanks for the response; I’m sure that typing will come up pretty soon.

RSS feed for comments on this post · TrackBack URI

Leave a Comment

You must be logged in to post a comment.