Python

By Sean Carroll | September 8, 2011 8:31 am

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.

CATEGORIZED UNDER: Computing, Personal
  • 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

  • Derek

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

  • http://wpgaurav.wordpress.com Gaurav Tiwari

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

  • 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

  • http://scienceisdelicious.net Tríona

    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)

  • 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

  • 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

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

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

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

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

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

  • http://mjhudson.uwaterloo.ca Mike Hudson

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

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

  • http://constructal.blogspot.com Sameer

    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/

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

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

  • jpd

    what was the error with fortran?

  • 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

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

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

  • Chris

    2nding the python challenge if you are into puzzles.

  • 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!

  • http://www.theeternaluniverse.com/ Joseph Smidt

    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

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

  • Nirdosh

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

  • http://mattleifer.info Matt Leifer

    If you want something as powerful as Mathematica then you should try Sage: http://www.sagemath.org/ This is what I use. It is built on top of Python, so the syntax is the same and you can call Python libraries from it. It includes all the mathematics and graphics packages that you will ever need, can do symbolic manipulation as well as numerics, and has an interactive notebook interface that will be familiar from Mathematica, Maple, etc. A good way to start is to go through the tutorials in the documentation that comes with Sage. This will teach you Python syntax, whilst introducing the mathematics libraries at the same time.

    If you want to go with a more bare-bones approach then you will still need to install SciPy on top of Python. This will give you everything you need to do numerics with Python and is more like using Matlab than Mathematica (except with a syntax that actually makes logical sense). The docs on the site are a good place to start.

    For learning Python on its own, many people recommend the book “Dive into Python” by Mark Pilgrim. An online version is available for free at http://diveintopython.org/ It is more focussed on things like web development and databases, but it should be OK for the basics. The version based on 2.x Python is a bit out of date, whereas the 3.x version is fairly current. However, you will probably want to learn 2.x to begin with because most of the number crunching libraries have not been ported to 3.x yet. There are an infinite variety of similar books available, but to be honest, unless you want to become a web developer, you are better off starting with Sage and its tutorials. Also, follow @sagemath on Twitter to get links to articles about Sage.

  • http://www.freemanadvertising.com Kevin

    Objective C is the way to go! Smart phones and tablets are the future. Good luck :)

  • http://homepages.uc.edu/~carrahle lee

    for scientific use the main packages to check out are:
    numpy – most of the base numeric/matrix functions from matlab
    – and a nice set of matlab-> python conversions http://mathesaurus.sourceforge.net/matlab-numpy.html
    pylab – contains numpy, so if you import pylab you dont need to import pylab
    – contains all of matplotlib plotting tools, tons of nice options for rendering graphs/charts/plots whatever you want
    scipy – again contains pylab and numpy, or rather pylab, which contains numpy, implements all functions from the scilab package
    – extends numpy math functions with a host of math/scientific functions (linear regression, filters, optimization,signal analysis…)
    – def make sure to check out http://www.scipy.org/Cookbook , this site hosts a bunch of really cool examples using scipy for various scientific visualizations, and solutions.

    *all of these packages are supported by very large communities of developers and scientists and have full reference manuals for each and tons of active discussion for problems and questions.

    honorable mentions
    pickle — for persistently storing datasets
    PyWavelets — wavelet transforms
    pyevolve – lots of genetic and evolutionary algorithms for python
    ctypes – allows you to somewhat easily interface with programs written in the c langauge

  • http://thoughtcarnage.wordpress.com Gorbin Wafflemunch

    If you’re looking for an editor for Python I’ve found Komodo Edit to be very useful.

    NetBeans is a great IDE, but apparently there’s a bit of work in getting it setup for Python – depending on your needs it may be worth the effort.

    As has already been mentioned Dive Into Python and Stack Overflow are your friends.

    Python is a fantastic language, I wish I had more opportunities to use it in my day job…but I digress.

  • Thomas Larsson

    /shameless plug/ If you are interested in using python for doing 3D graphics, you could start with my python cookbook for Blender /end shameless plug/

  • BillC

    First, I agree with the recommendations for SciPy, NumPy, and Matplotlib. If you have been using Fortran exclusively, the biggest decision you should make is whether to learn the object-oriented paradigm for programming. Python is a fully object-oriented, but you don’t HAVE to use it that way. It works perfectly well as a procedural language (like Fortran) but you will sacrifice a lot of power if you use it that way. On the other hand, be warned that really understanding object-oriented programming is a steep learning curve.

  • Matt

    Many people have mentioned it and I must agree: SciPy, NumPy, and Matplotlib are ideal. If you are looking for some basic examples to crib off, I have a few that I use available. These are mostly focused on determining latency characteristics of large systems, but you can get a rough idea how to use some of the plotting functions from it.

    https://bitbucket.org/fowles/python-plotting-scripts

  • bystander
  • X

    Python is not a Mathematical programming language, it’s a scripting language. If you want something that solves Mathematical equations, performs Mathematical transformations of numerical data and makes Mathematical graphics, you should probably learn Mathematica. I’ve done everything from chiral perturbation theory to random-lattice gauge theory to optical recognition of vicinal steps in electron microscopy data using Mathematica. It is the Swiss-army knife of scientific computing. Python is a toy for computer engineers to hack together utilities on Linux (of course, it’s great at doing that, and I’ve often used it for that purpose myself), which is not one of your stated goals.

  • Mark P

    Fortran on the Mac – there are two choices. One is open-source Fortran, using your Unix shell. The second is something like Parallels or Boot Camp. I used the first, but found the Fortran less than optimal. Now I just boot into Windows and use a Windows-compatible Fortran.

  • Chris Woerz

    I would have to give yet another vote for the Python Essential Reference (purple book). I have the 4th edition and it has been a great tool for learning and using python.

  • Michael Zappe

    Download iTerm for your Mac, and discover the joy of MacPorts, which will make doing Python, SciPy, NumPy, gfortran, g95, etc. much more pleasant. Check out the large set of science ports.

  • Joseph C

    “Learn Python The Hard Way” is another good introduction to the language. The pace is quick and is quite minimalist in terms of exposition. The reader gets to code much sooner than other beginner texts. This is a good gateway to learn the syntax and eventually move on to more advanced books. http://learnpythonthehardway.org/

  • C.Bojechko

    A really good intro to python is the google class. Goes over most of the key objects like lists, dictionaries.
    http://www.youtube.com/watch?v=tKTZoB2Vjuk

    To get practice there are some interesting problems found at the python challenge.
    http://www.pythonchallenge.com/
    These problems can get addictive!

  • SRT

    What kind of computing do you want to do? I suspect that you might be better off using Matlab than Python. It is really powerful and very easy to at least prototype code in. If you need to do some serious number crunching later on then you can shift to a traditional compiled language such as C++.

  • Stan

    A lesson I have recently appreciated: Learn to love Numpy! It provides much of the same benefit as Matlab, and runs nearly as fast as compiled code. Plus it makes your code a lot shorter and often more readable when you operate on entire arrays at once.

    Some other hard-learned lessons from a physicist who programs a lot:

    The vast majority of scientific computing time is dominated by writing and verifying code that will be used a few times, not running it so much. Don’t be distracted by complaints that Python is “too slow”, because 70% of the time, that doesn’t matter. (And if you really make good use of Numpy, that fraction is more like 90%) If the time comes that performance actually is a problem, you can learn how (or find some help from a student/colleague) to use something like Cython, PyROOT, Boost::Python, f2py or whatever to call compiled code.

    Since the biggest problem is producing correct, reproducible results, you will want to learn about two important things: automated testing and version control. Automated testing lets you verify that you haven’t broken your code by feeding functions inputs and checking that the results match the answer you know you are supposed to get. There can be a bit of religion about correct testing among software developers, but for the scientist I’ve found that the most important thing is to start doing something, anything really. Then you can iterate and improve things as you go.

    Version control is really critical for creating reproducible results. Plus, it gives you the confidence (along with your tests) to fiddle with things, secure in the knowledge that you can go back to a working revision if you totally mess things up. I would personally recommend Mercurial for the mixture of easy command interface and the ability to use it without a special server. There are other choices, and like automated testing, the most important thing is that you do something other than keep a bunch of poorly named copies of files floating around. Plus, if you want to share your code, then places like GitHub and BitBucket offer a way for you to publish code so that your colleagues can see it, run it, and make changes to it (if you allow them).

  • Paul Buckley

    Python will provide you with all the experience you need to cement bad – really bad – programming habits in place. While easy and powerful it should never be considered (even though it often is) for anything that has critical requirements. Hopefully you won’t be programming any satellite fine-guidance algorithms with it. It’s a simple programming tool with an easy to learn, easy to shoot yourself in the foot syntax.

  • GC

    Some one mentioned above Dive into python, which is really good. There’s also an interactive tutorial which might help http://www.trypython.org/ .

  • Doug

    For astronomical data analysis with python, a good tutorial is at http://www.scipy.org/Additional_Documentation/Astronomy_Tutorial

  • Craig W

    I recommend Project Euler as a fun way to solidify understanding of any new programming language. The site contains lots of math-based problems at a wide range of difficulty levels. Once you solve each problem, you gain access to a discussion board where other solvers post their code, which is often as enlightening as your own coding process. After “book-learning” Python a few times over the years, and then promptly forgetting it, this site is what brought me to a working knowledge of the language.

  • Sheila

    “Hopefully you won’t be programming any satellite fine-guidance algorithms with it. ”

    it’s too late. they already do.

  • http://tropicalbioinformatics.wordpress.com Spulido99

    As everybody is saying, use SciPy, Numpy and Matplotlib ( http://docs.scipy.org/doc/ ). In their site they recommend iPhyton, I liked better Eclipse ( http://www.eclipse.org/downloads/ ) with PyDev plugin ( http://pydev.org/ ).

    Enjoy!

  • Pingback: Webmasters And Community Python | Cosmic Variance

  • Naked Bunny with a Whip

    Holy crap. Sean is playing around with Python to try it out. He’s not programming weather simulations or the next Mars probe with it. Some of you need to chill.

  • http://www.idle-logic.com Chris Zeh

    I use Python everyday at work for collecting data, crunching numbers and spitting out fancy graphics, so I have to put my two cents in.

    The best (free/open-source) IDE for Python in my opinion is pyScripter (http://code.google.com/p/pyscripter/). Those Eclipse based solutions are too cumbersome and are better for writing full applications. If you want small to moderate sized scripts, go with pyScripter. (This runs on Windows… but I’m not sure about on a Mac)

    I recommend going with the Python 2.7 versions, and staying away from Python 3 until it is supported by more 3rd party packages.

    Next get NumPy & SciPy, which will let you work with multidimentional arrays and do scientific computing.

    For graphics MATPLOTLIB is the way to go. Similar graphics capabilities as MatLab if you’re familiar with that. I recommend changing the “backend” to WXAgg instead of the default Tk though. (Matplotlib supports LaTex as well).

  • http://www.enthought.com Jason

    Hi Sean, we are developing (and soon releasing) a product targeted at exactly this sort of use case. It is based around Python but focused on people who aren’t so much looking to write software as much as get specific tasks done (like data analysis). If you are interested shoot me an email message and I’d love to get your thought and hear more about what you want to do.

  • http://mattleifer.info Matt Leifer

    Why no love for Sage? This makes me very sad. In case you missed my earlier comment, SAGE, SAGE, SAGE, SAGE, SAGE!

  • Rich

    Get the box set, although the Life of Brian is my favourite.

  • Carl

    My situation is very similar to yours. I was fluent in C (and Fortran, and Pascal, and …) when younger but haven’t programmed in anger for well over a decade. And by coincidence I started teaching myself Python about two weeks ago.

    So far I’ve found it a very welcoming language. There is a natural and succinct syntax for the operations and data structures you need most often. In some ways it feels like having the best parts of C and Lisp in one place, and it definitely feels like it was designed by people who have actually absorbed the language design lessons of the past (unlike certain other languages I could mention…).

    Despite the warnings above I’ve jumped straight into Python 3, since I’m not doing anything mission- or business-critical. My text of choice is “Programming in Python 3″ (Summerfield).

  • Ryan

    Have you considered LabVIEW? It’s graphical, effective, and designed for scientists.

  • Edward Carney

    Consider VPython. It is Python plus a 3D graphics module called “Visual.” It has been used to create an interactive, calculus based, physics curriculum at North Carolina State University (http://www.matterandinteractions.org) in addition to projects on 3D systems and data visualization.

    http://www.vpython.org
    http://en.wikipedia.org/wiki/VPython

    The Wikipedia entry states, “Real-time, navigable 3D animations are generated as a side effect of computations. This makes it easy to create simple visualizations, allowing programmers to focus more on the computational aspect of their programs.”

  • http://blogs.discovermagazine.com/cosmicvariance/sean/ Sean

    Thanks for all the input! Very helpful, especially about the most useful libraries for science. (I went and installed the enthought packages, which include scipy, numpy, and matplotlib, among others. Now to figure out what those do…)

    I’m going to take a crack at Python itself, just because I would really like to have a simple all-purpose way of writing small programs when the occasion arises, and the distribution is already installed on my Mac. After an hour of fiddling, it’s amazingly easy to use, for which I am grateful. But I might check out SAGE at some later point.

    I promise that any satellites I cause to crash will create minimal damage.

  • http://about.me/elibressert Eli Bressert

    If you’re looking for a good resource of Python packages (not sure how much of astrophysics is relevant to your work) you may want to look at the resources page on astropython.org: http://www.astropython.org/resources

    Also check out astrobetter.com for Python tips. There’s a lot of useful blog posts on Python there.

    Everyone above has made excellent suggestions. The most important packages for Python are Numpy, Scipy and Matplotlib. If you need to visualize 3D data from simulations (common for cosmology) I’d recommend yt: http://yt-project.org/

    Last but not least, using the Enthought Python Distribution (EPD) is highly recommended for new users. It’s free for those from academic institutes and it uses some features that will speed up Scipy significantly (Intel specific libraries for fortran wrapped files): http://www.enthought.com/products/epd.php

  • Z

    Sean, you can program in mathematica – loops and all. I’ve tried python and mathematica is, by far, still better if speed of execution is not critical but time invested in coding is.

    Like someone else above said, python is not a serious language for scientific computing, but more of a scripting language when dealing with data structures and sets. Python is used in astronomy, along with IDL — you could also try IDL – but I have a feeling you’re not doing astronomy.

    For normal stuff, stick with mathematica. If you need speed, use C/C++ or Fortran.

  • Eric

    Once you know the basics, every few weeks, reread this:
    http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html

    When programmers know one language and switch to another, one of the most common problems is to keep to many old habits. Unfortunately, what might be a good habit in language A is often possible in language B, but a bad idea, or at least sub-optimal. It’s very easy to “write FORTRAN in python,” and I see a lot of it from astronomers, but the results are often painful. The hardest part of learning a language isn’t learning the syntax, it’s learning the patterns of thought that lead to using the language well.

  • Duncan

    Tom Aldcroft at the Harvard Center for Astrophysics wrote a pretty good walkthrough on how to use python and many subsidiary packages, plus it has some good examples as well. Many astronomers I’ve spoken to say python is the future of astronomy programming, and Tom has been writing a lot of packages to make this so.

    http://python4astronomers.github.com/

  • Bryan

    To the few stating that “python is not a serious language for scientific computing, but more of a scripting language”, please recognize that it is 2011, not 1999, and update your opinion of python accordingly. Even as someone who actually does like Mathematica quite well, there are only a few specific use-cases I would recommend it over python.

  • Z

    It just isn’t, especially for high performance applications. If the extent of your scientific computing is a small MC run, or doing a bit of statistical or data analysis on your laptop, then Python is fine – just as Mathematica is — or IDL. My contention is Mathematica is faster (in time spent coding), and better for such ‘routine’ tasks, unless you’re an astronomer (IDL is more commonly used).

    However, if you want to run a high performance DFT on a Tesla workstation, run some sort of Fisher matched filter analysis for GW signal hunting, or do a complex 3D magnetohydrodynamic simulation then you’re not going to use Python on your laptop. Compilers for C are much better tuned for specific architectures too, regardless of the level of language abstraction. I haven’t kept up with compilers, but a few years ago, the intel C compiler was much faster than third party compilers for C, or any other language, for x86 intel processors (maybe GCC finally caught up?).

  • Ali

    Sean,

    I’d also get a good IDE to work in, unless you already are an expert with vim or emacs. It will make your programming a lot more pleasant, if you’re used to tools from the old, old days — syntax highlighting, object properties, accurate code completion, context-specific documentation, debugging, etc.. ActiveState has a free version of their Komodo Python IDE which I would recommend. Alternatively, there are also a number of open-source IDEs for Linux which I suspect also have Mac versions; I’ve used Spyder which was solid, and Eclipse+PyDev which was comprehensive but a bit slow and clunky for everyday use. I’ve heard that Wing IDE and PyCharm are great commercial IDEs but I’ve never used them. Avoid the mini-editors that come with Python distribs (like IDLE.)

    I also *love* ipython as a standalone shell for Python. Since it’s an interpreted language, you will probably often find yourself trying things out in a running Python shell before you write them into an actual program. iPython is far superior to the python shell for this… it has syntax highlighting, it lets you go back through old commands easily, it lets you easily inspect live objects and quickly get help and see the source of functions/objects, and it lets you “magically” use shell commands inside of Python.

    numpy lets you do array computation FORTRAN-style in Python. That’s essentially what it’s for. Instead of having to write loops to go through element-by-element, with a numpy array you just write the array expression and it is automatically done element-wise. It also lets you use more powerful & general indexing schemes, it will “auto-promote” arrays to a higher dimension when needed, etc.
    scipy gives you a bunch of useful mathematical libraries.

  • thomas

    Python is incredibly straightforward, you should have no trouble with it. The official documentation at http://python.org/doc/ is excellent, in particular the language and library references are incredibly useful.

    If you’re interested in python for math, you can use Sage, a python-based computer algebra system, which by now is better than Mathematica for some things (including programming, since it’s based on python). Sage is at http://sagemath.org . SAGE isn’t as straightforward as python itself, and if you want to install on a mac, you have to download the exact version corresponding to your version of MacOS.

  • Bryan

    Z, again you are showing a decades-old mindset. The first article I saw about using python to direct parallel simulations on supercomputers at LANL was in the late 90s. Nowadays the new versions of ipython have made casting parallel numercal tasks onto clusters almost effortless. You also apparently aren’t aware of pyrex, or cython, for easily generating efficient C extensions for those numerical tasks automatically, on the fly. And I guess you aren’t aware that the large numerical extension packages are all written in C and Fortran to begin with, and simply wrapped in python. Or that lots of people have been doing big-data on big-iron and putting their results on display at SciPy and other conferences for years. Having used both Mathematica and python I don’t really buy the programmer-efficiency argument, either. They both have excellent documentation, and offer clear learning curve advantages over C or Fortran or C++. But if you have practical concerns like interfacing with remote datasets or experimental hardware, or providing engineering guis for feckless graduate students, python is a clear win. Maybe if you are doing purely symbolic work I would recommend Mathematica, but Sage neatly wraps up Maxima, so I’m not even sure I could claim that in every case.

    I mean, I like Mathematica. It’s a highly developed professional and polished product. If you or anyone else is productive with it, great. But the notion that python is a toy or wholly unsuited to scientific computing ignores the reality of scientists who are, in fact, using it exactly and effectively towards that purpose.

  • tim Rowledge

    Oh for goodness’ sake – use a real language with a real IDE and a proper computational model behind it. The only language even approaching good enough to be worth criticising is Smalltalk; several free versions exist including perhaps the most popular one, Squeak. It has the virtue that it really is write once run everywhere.
    http://www.squeak.org

  • Vesa

    For learning Python I’d give a vote to “Learning Python” from O’Reilly. You can use that while learning the language as well as a reference afterwards. Available in the traditional dead tree format as well as the more portable digital file. One has to admit that it does not quite reach the compactness of “Kernighan and Richie”, but Python comes with more stuff included… =)

  • miguel

    In case you’re still here…

    Evolutionary Computation (a unified approach) by Kenneth A. de De Jong
    The table of contents and the first few pages are available at amazon.

  • Pablo Landherr

    Sean,
    You really, really need to take a look at http://www.jsoftware.com for a free and fantastic alternative to Python.

  • John Peacock

    I’d heard all the good things about Python and resolved to educate myself in it. I didn’t get very far, but far enough to encounter one slightly disturbing aspect, which is that the people developing Python don’t seem to believe in backwards compatibility. In other words you can have a piece of code that works fine in Python 2.n, but fails to run in 2.m where m > n. This seems poor. Fair enough to add new bells and whistles, so that a recent piece of code won’t run on an old machine, but old code should always work. I can dig out fortran that I wrote 20 years ago and it still does something (on a MacBook, moreover: just use g95). So this left me nervous about a wholesale shift to Python as my main way of doing things, since I could imagine spending months building up a repertoire of code to do all the things I commonly need, only to find that Python stick out a new version and suddenly none of it works any more.

  • Sheila

    I thought the first two comments about python not being a powerful tool for scientific programming were trolls, but after continuing to read the thread I think they are sincere.

    I am following up to reiterate what Bryan says above. We’ve had people from Argonne show up to our usergroup to discuss python running on bluegene. We’ve had the primary dev of matplotlib come give a talk on it, and discuss why they created it and how they’ve used it, for example http://python.mirocommunity.org/video/1136/matplotlib-the-popular-2d-plot

    Heck, I couldn’t remember some of the talks I’ve gone to, so I googled and found that the scipy website breaks out examples in to categories for you. http://www.scipy.org/Topical_Software

  • martin smith

    I strongly suggest looking at Langtangen’s book on computational scripting with python. I do scientific computing in python and have an earlier edition of this book which was, out of the many good books and resources for python, the best by my measure. I believe he has a lower-level primer out as well.

  • Shane

    I have very much enjoyed the approach of this book: http://learnpythonthehardway.org/

  • Mark Groeneveld

    One program to use: Pylab
    It mixes Python with Numpy (arrays & related math), Scipy (numerical tools), and matplotlib (plotting) all into one easy to use package.

  • Leonardo Uieda

    Software Carpentry (http://software-carpentry.org/) has excellent video lectures on Python (and lot more) that are directed towards scientific computing. I very highly recommend it!

  • brad

    Intel Fortran is available for the mac, and descends from good old DEC Fortran. It’s not free, but they offer academic pricing.

    Octave is a free version of Matlab, useful for quick one-offs and graphs.

  • Chris Tunnell

    I also highly recommend Software Carpentry for both general concepts and Python specifics.

  • Walter

    I know it has been mentioned before but Software Carpentry is a very nice resource, I use all the time. It has very concise and useful introductions on a number of every day computer problems.

  • http://www.kachergis.com George

    If you want to make great graphs and crunch numbers, I highly recommend R rather than Python. I love Python, and use it for developing games and experiments, but R has better graphing packages, and has implemented math functions more intuitively (and efficiently) than Numpy, IMHO.

  • Craig

    Python is a horrible programming language. Seriously you are a smart guy you could learn C in about the same amount of time and what you learn would be applicable to pretty much any language. Alternatively you might want to consider learning how to write Matlab scripts. The downside to that is the cost of a Matlab license. Maybe your University could provide you a license? Matlab is excellent for number crunching and plotting and its easy as hell to learn.

  • emeris

    I would add that if you do use emacs already, the ipython package,py-shell, and the community python-mode (not the one shipped with emacs) make a very nice IDE

    I also nth the main python docs as a resource.

  • http://jbg.f2s.com/quantum2.txt James Gallagher

    All you really need is a simple EXAMPLE of Python code which does something close to where you want to start – ie displays a basic cellular automata and evolves it according to some basic rules. Together with instructions how to get this running on your Mac (if that’s what you’re using)

    You could easily work out how to adapt it to more advanced stuff by perusing docs suggested in various links above (well, I mean it’s “easy” compared to understanding modern physics papers)

    So if you explain a very basic starting point you’d like, myself or someone else should be able to post code. Otherwise just google for a simple example, and find a few free hours over several weekends to experiment :-)

  • Gerard Hammond

    we use REALbasic for our medical research apps. but we are gui people first and algorithm second. perhaps a REALbasic /python/R mix might be good for your enjoyment Sean

  • http://nosingularity.blogspot.com/ Limits

    The real question is how much programming and processing you need to perform for the task at hand.

    http://nosingularity.blogspot.com/2011/09/riduculousness-of-processing-and.html

  • http://lighthouseinthesky.blogspot.com/ Anne

    I’m an observational astronomer, but we use python all the time, for everything from scripting massive pulsar survey processing jobs to analyzing X-ray observations to simulating pulsar timing noise. I’ve got a blog where from time to time I do a sort of “worked example” of solving some (usually) astrophysical problem with python, that you might find useful as a more apropos example.

  • Earl

    Have you considered MATLAB? Most valuable tool I’ve ever used in 40 years of engineering analysis.

  • http://www.metaschema.blogspot.com Earl Flask

    Python is a good way to go. Here is a crazy thought. For something completely different there is Javascript. The advantage to that is that you could easily publish Javascript programs on web browsers. However, Javascript environments are probably harder to work with than Python.

    Someone else may point you to tools that allow you to publish Python scripts onto browsers too.

  • http://base22.com Ben Shoemate

    I would also recommend Javascript. Combined with html and css (which you must already work with a little on this blog) you can do just about anything and have it ready to share online. Make sure you look at jQuery (a javascript set of functions for doing some fancy stuff and making basic stuff easy). Javascript, as one of the languages of the web, is by far the most well documented (since the internet is very vain and loves to talk about itself – it’s self documenting).

  • http://www.astro.multivax.de:8000/helbig/helbig.html Phillip Helbig

    Fortran (the current standard is Fortran 2008) is still there and is great for number crunching and graphics. Don’t give up on it.

  • Dana Nourie

    Sean,

    Python is excellent, and scientists use it a lot. If you Google python applications, you will get lots and lots of app written by scientists.

    Ruby is also great, as well as Ruby on Rails for those who want to do web development.

    By and far the best and funniest programming book ever is Poignant Guide to Ruby:
    http://mislav.uniqpath.com/poignant-guide/book/

    Read some of it even if you have no interest in Ruby programming. It’s hilarious and a great model for making tech material fun! Plus, it’s free!

    Dana

  • Deepak Vaid

    Dear Sean,

    Fortran was your previous “language of choice”? Good heavens. That language scares the bejezus out of me.

    About Python. The problem with Python is that there are so many versions 2.5, 2.6, 2.7 and now 3.x. And having installed a version you have to install all the modules for that version all over again. Then when you’ve gone through all that you find that one module doesn’t work with the Python version you installed, so you start all over again with a different version …

    Enthought’s Python Distribution (built around Python 2.7) (EPD) – which is free for academic use, btw (!) – has solved these problems for me. It contains every toolset the modern physicist could need. All of Scipy, Numpy, IDLE, Mayavi, you-name-it, EPD has it.

    You’ll probably need to install OpenGL and PyQt4 separately if you want to do some serious graphics programming. OpenGL is straightforward enough. PyQt4 is extremely tedious but straightforward to install. But its worth the effort. You’re left with the Python bindings for Nokia’s Qt api which is far superior to wxWidgets and other alternatives for building GUI apps. EPD + PyQt4 + OpenGL rocks!

    Good luck on your Python adventures,

    Deepak

  • http://www.astro.multivax.de:8000/helbig/helbig.html Phillip Helbig

    “Fortran was your previous “language of choice”? Good heavens. That language scares the bejezus out of me.”

    Have you ever actually used it?

  • John

    I had the same problem – finding good python resources. But I fought my way through it and have quite a few self-written sample programs I would be happy to zip up and send you.

NEW ON DISCOVER
OPEN
CITIZEN SCIENCE
ADVERTISEMENT

Discover's Newsletter

Sign up to get the latest science news delivered weekly right to your inbox!

Cosmic Variance

Random samplings from a universe of ideas.

About Sean Carroll

Sean Carroll is a Senior Research Associate in the Department of Physics at the California Institute of Technology. His research interests include theoretical aspects of cosmology, field theory, and gravitation. His most recent book is The Particle at the End of the Universe, about the Large Hadron Collider and the search for the Higgs boson. Here are some of his favorite blog posts, home page, and email: carroll [at] cosmicvariance.com .

ADVERTISEMENT

See More

ADVERTISEMENT
Collapse bottom bar
+

Login to your Account

X
E-mail address:
Password:
Remember me
Forgot your password?
No problem. Click here to have it e-mailed to you.

Not Registered Yet?

Register now for FREE. Registration only takes a few minutes to complete. Register now »