Python

I’ve decided I need to become a programmer again. As an undergrad, and to a lesser extent as a grad student, I wrote code all the time. But since I started doing research, it’s been pencil-and-paper almost all the way through, with occasional dips into Mathematica or plotting programs.

That must end, so I’ve decided to learn Python. I just need something simple for number-crunching and graphics, and everyone in the know seems to have nice things to say about the language. (Secretly I would like to play around with genetic algorithms and cellular automata, but I’m not going to admit that.) I tried to get Fortran, my previous language of choice, up and running on my Mac … it didn’t go well.

So… any tips? Pointers to well-written resources and tutorials (online or in print) would be especially helpful. Keep in mind that the target audience is an aging theoretical physicist who hasn’t programmed in 20 years, and for that matter has been pretty much command-prompt free (working on my Mac) for the last five.

The things I admit in public on this blog, sheesh.

94 Comments

94 thoughts on “Python”

  1. SciPy is pretty interesting. Lots of handy numeric tools, and easy multidimensional array manipulation. At it’s heart, it’s a wrapper to the ancient, esteemed linear algebra and other numerical codes written in C and Fortran. scipy.org

  2. Just some background info: there’s currently two major versions of python in use, namely 2.x and 3.x. Dive into python covers the latest, but you might still run into a lot of libraries that are 2.x only. So you’ll almost certainly end up writing 2.x code anyway. Just something to keep in mind.

    Anyway, the main tutorial is actually quite good for someone with some experience in coding, I’d definitely start there:
    http://docs.python.org/

    Some people like learning through puzzle solving and the like. The python challenge is one attempt at providing a way to do that. I suspect it might appeal to you:
    http://www.pythonchallenge.com/

    And if you have questions and don’t find them answered, head over to http://stackoverflow.com/ (I think you have linked to one of their sites in the past, but it really is a good place to find and ask questions about basic features).

  3. I’m guessing you’ll need to do numerics, for which numpy, scipy, and matplotlib are essential. I also highly recommend ipython as an enhanced interpreter. “ipython -pylab” drops you into an interface that emulates Matlab quite well, with everything imported into the local namespace and with interactive plotting. http://docs.scipy.org/doc/scipy/reference/tutorial/index.html

    Since you’re on a Mac, all of these are installable via MacPorts. Be prepared to spend several hours compiling. (“sudo port install py26-scipy py26-matplotlib +latex +ghostscript py26-ipython -scientific” should do the trick)

  4. I notice on g+ you are getting some good feedback, followup with a recap here for the benefit of people who read your blog. 🙂

    Btw, I am very enthusiastic about anyone wanting to learn python, but I also want to say that you can install fortran on your mac, possibly. I don’t use fortran, but I wanted to play with scipy, and in order to compile scipy for my mac, I had to get fortran on there. I used the homebrew package manager for the mac to install gfortran.

  5. Here’s a link to a blog post by “one of you own kind” (different sub-species, perhaps?) wondering about the same thing; and right down at the bottom of the comment list you’ll find my post which recommends online tutorials which have already been mentioned here many times.

    So, yes, this comment is essentially redundant.

  6. Python with SciPy and NumPy is very good and easy to learn for number crunching, plotting, linear algebra etc. as long as you are not too worried about speed. Another good, free book to start learning Python is “Think Like a Computer Scientist: Python” : http://www.greenteapress.com/thinkpython/thinkCSpy/thinkCSpy.pdf

    You’ll certainly get a lot of free online support in form of forum discussions etc. for Python.

    Also given your background, I think you should also give Haskell (a functional programming language) a try. Here are some free e-books you can use to start learning Haskell.
    1> Learn you a Haskell for great good: http://learnyouahaskell.com/chapters
    2> Real world Haskell: http://book.realworldhaskell.org/read/
    You can get an easy to install Haskell compiler from: http://hackage.haskell.org/platform/

  7. Reviewing the other comments so far, macports is one of the mac package managers. fink and homebrew are others. I’ve used fink and homebrew. Lately I’ve stuck with homebrew. It has been the smoothest so far. I don’t want to tell you to use my favorite ___ though, so I encourage you to read about them and pick the one best for you.

  8. There is some good documentation, including a tutorial, at docs.python.org. wiki.python.org is also a good site with links to tutorials and information about math and science libraries at wiki.python.org/moin/NumericAndScientific

  9. Developer library books, (the purple one) python essential reference. Single best python reference I’ve found. Also look for pythonchallenge, if it’s still working, a great reason to learn what are initially some obscure but useful aspects of the python standard lbrary.

  10. EDIT: Micheal Scneider already suggested Enthought…

    Enthought puts out a terrific Python package containing almost everything you will want to have when you begin (Numpy, SciPy, Matplotlib, etc).

    Also, Sage contains a Python distribution ‘inside of itself’ – and there are numerous Python packages that can be installed ‘into’ it. (Sorry for the scare-quotes… you’ll see what I’m trying to get at if you pursue this option.)

    And a tip: ask questions on Stack Overflow or AskSage (with all of the usual caveats about etiquette). Oh yeah, one more thing: Have fun!

  11. If you know how to construct modern computer programs, then I third Relay and Eduardo’s recommendations of http://docs.python.org .

    If you don’t have a lot of practice constructing modern computer programs, then you want to avoid cluttering yourself up with style decisions. In that case I third Relay and Eduardo’s recommendation’s of http://docs.python.org .

    If you really want to mess around with cellular automata and genetic algorithms then get a good book on the subject. Then use http://docs.python.org for reference.

    The tutorial is a good start and the library reference will tell you specifically how things work in user-space.

Comments are closed.

Scroll to Top