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

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

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

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

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

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

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

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

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

  10. “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/

  11. 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++.

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

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

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

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

    it’s too late. they already do.

  16. Pingback: Webmasters And Community Python | Cosmic Variance

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

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

Comments are closed.

Scroll to Top