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.

Comments

94 responses to “Python”

  1. jonsnow Avatar
    jonsnow

    Hi Sean,

    here’s a nice intro book:
    http://diveintopython.org/

    and xkcd’s take on python:
    http://xkcd.com/353/

    love the blog

  2. Derek Avatar
    Derek

    a very good resource: http://diveintopython.org/

  3. Gaurav Tiwari Avatar

    This youtube channel has excellent videos on Python programming http://www.youtube.com/user/thenewboston

  4. BP Avatar
    BP

    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

  5. Tríona Avatar

    I’ve started working through http://www.swaroopch.com/notes/Python_en:Table_of_Contents (as a nonever programmer, it’s doable so far)

    (i’ve heard great things about diveintopython too, but this was suggested as being more suited to me)

  6. Michael Schneider Avatar
    Michael Schneider

    You’ll probably want the tools in the scipy and matplotlib packages. Don’t install these yourself, but instead grab the free package from Enthought: http://www.enthought.com/products/epd_free.php

  7. Relay Avatar
    Relay

    Hi, i used to Python; its a very good starting language, along with VB and others.
    This helped me alot : http://docs.python.org/tutorial/

    If you need any help give me a shout at http://www.joe.to
    My user name is Relay

  8. ix Avatar
    ix

    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).

  9. Foton Avatar
    Foton

    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)

  10. Sheila Avatar
    Sheila

    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.

  11. Sheila Avatar
    Sheila

    Python mirocommunity is the most fantastic way to find python videos. for example, http://python.mirocommunity.org/search/?q=scipy

    I have friends and acquaintances who keep the site going.

  12. Navneeth Avatar
    Navneeth

    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.

  13. Mike Hudson Avatar

    The AstroPython blog recommends the following notes for
    Scientific Computing with Python

    http://www.astropython.org/tutorial/2011/8/Python-Scientific-Lecture-Notes

  14. Sameer Avatar

    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/

  15. Sheila Avatar
    Sheila

    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.

  16. Callie Avatar
    Callie

    Its been a few years since I’ve worked in Python, but the Quick Python book by Manning Publishers

    http://www.amazon.com/Quick-Python-Daryl-Harms-Ph-D/dp/1884777740

    is a wonderful programming and Python book, if maybe slightly out of date.

  17. jpd Avatar
    jpd

    what was the error with fortran?

  18. bystander Avatar
    bystander

    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

  19. Peter Avatar
    Peter

    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.

  20. Eduardo Ruiz Avatar
    Eduardo Ruiz

    I recently took up Python for my own research, and I’ve never looked back. Some great resources are the python tutorial: http://docs.python.org/tutorial/
    There’s also a library package called numpy that greatly helps with numerical analysis. It can be found at http://numpy.scipy.org/
    Numpy tutorial is: http://www.scipy.org/Tentative_NumPy_Tutorial#
    Hope this helps.

  21. Chris Avatar
    Chris

    2nding the python challenge if you are into puzzles.

  22. tom Avatar
    tom

    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!

  23. Joseph Smidt Avatar

    Scientists really should start here: http://scipy-lectures.github.com/

    And to install the packages you need go to enthought’s website: http://www.enthought.com/products/epd.php

  24. David Avatar
    David

    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.

  25. Nirdosh Avatar
    Nirdosh

    I found this tutorial very crafty and useful.
    http://www.swaroopch.com/notes/Python