Python books |
Python is a computer scripting language that's often used for numerical calculations. It has a more consistent syntax than Perl, it's more flexible than PHP, and it's easier to use than awk and sed. It's also object-oriented, with a syntax much like a simplified version of C++, but with a more compact notation. It handles garbage collection and memory management automatically. There are lots of graphics libraries out there that make it easy for programmers to avoid re-inventing the wheel. Like PHP, Python is often used for interactive web programming. Since “interpreted” is a euphemism for “slow”, Python is usually used in conjunction with programs written in compiled languages like C for speed.
Unfortunately, Python developers have been insufficiently attentive to backward compatibility. I use a Python program at work called PyRosetta, which interacts with Rosetta to do molecular modeling. PyRosetta only runs in Python 2.6. Version 2.5 doesn't work. Version 2.7 doesn't work. As for version 3.0, fuggedaboutit. This kills any possible use of Python by sysadmins: if you can't rely on a Python script continuing to work after you debug it, you are not going to write it in Python.
Changing syntax at every point release has got to be great fun for the
developers, but what about the poor clods like me who have to use it? Since
programs only run in specific versions of Python, I have to keep
multiple versions of it on my computer.
print "Hello world"
Hello World program. This version only works in Python 2.x.
Even Beazley's Hello World
program on the first page of this book only
runs in Python 2.x. If you're writing a language and Hello World has to be
re-written when you add a new feature, it's a clue that either you're
changing too much stuff or your original design was fundamentally flawed.
And that's surprising, because Beazley's book shows that Python has some
really cool features. But Python programs also tend to get bogged down in
environment variables and libraries scattered everywhere, which
kills portability.
Still, a lot of scientists still use Python, so there's a need for a concise book that brings C and C++ users up to speed quickly. If you're just a casual user, this isn't it. This book is written for programmers. There's nothing about compiling or installing Python, and the critical __init__.py file isn't mentioned until Chapter 8. Beazley doesn't even discuss how to run a Python program until Chapter 10. There's also nothing about graphics packages like tkinter.
If you're familiar with C++, you will find Python pretty easy. Readers are expected to understand object-oriented programming, classes, inheritance, sockets, and file I/O. But the book is well written, concise, and it has the distinct advantage of not having a picture of a big snake on the cover.
There is also a big section on interfacing Python with C. Python comes with numerous packages that make it easy (okay, possible) to write utilities for sending mail, performing asynchronous I/O, and parsing XML and Unicode files. Other packages are useful for network programming. For these, the reader should read W. Richard Stevens' UNIX Network Programming first. I might not be inclined to use Python to write a Web server, as some people have done, but as a quick way to transfer data, Python sounds like the perfect tool for those like me who hate the Swiss Army Chainsaw known as Perl, which is to say ... just about everybody.
jul 02, 2013
SAGE is a very powerful symbolic math package. Though not as fast or as powerful as Mathematica, it runs in an ordinary browser, and it's free. I worked through this book as a raw beginner. I found the examples to be excellent, but SAGE itself was a bit quirky. It is very cool, but it may not be for everybody.
The biggest quirk is lots and lots of typing—Sage is command-line-driven,
and Sage code is bulky, so it can take a whole page of typing to produce a
simple graph. Based on the difficulty I experienced convincing my Windows
friends to use R, which is a lot more concise, this could be a problem.
I found myself skipping many of the examples because of all the
typing. For example, here's the line for adding a dashed line to a
graph:
lines.append(line([(x_value,0),(x_value,y_value)], color='black',
linestyle='--'))
That wordy mishmash of parentheses and brackets would be intimidating for anyone, but especially so for those among us who are pointers-and-clickers. Even though you may only have to type it once, you'd have to start over if you migrated to Mathematica, because the syntax is different. You can download the code examples from the publisher's website, but that doesn't change the fact that new Sage code will require lots of typing and fastidious attention to punctuation.
Here's a typical 3D plot. The 3D plots can be animated with OpenJDK and the Jmol Java Applet. Jmol is a molecule viewer, but it works. It's easy to configure—maybe too easy. While testing it I accidentally set the language to Chinese - Taiwan Characters. All the menus changed to Chinese in a grainy, low-resolution font.
Another challenge is that, in the browser interface, the buttons don't work in a way consistent with their name. For example, if you click on "New Worksheet", the "Rename Worksheet" dialog pops up. If you select "Load Worksheet" from File, it says "Upload worksheet to the SAGE notebook." To open a worksheet, you have to click on "Admin Share Now". A "Share This Document" page opens. You then click on "Worksheet" to open the file.
Now, this is not rocket science, but it took me the better part of ten minutes to figure it out, and it's not described in the book. I guarantee that my math-impaired colleagues, who have problems figuring out simple tasks like installing a printer driver in Windows, will get frustrated.
These aren't flaws, just examples of how the user interface is less
polished than a commercial program.
There are other quirks, too, like the fact that it's easy to
accidentally re-define internal functions and operators like
derivative
. If you do that, your program is totally
screwed:
var('a,x')
derivative(x)=f(x)*pi
... [ many lines of code ] ...
f(x)=a*x^3
g(x)=derivative(f,x)
show(g)
Ack!
I would also have liked to see more about interfacing Sage with other programs. For example, I had hoped to use my spreadsheet-based statistical program, which I wrote in C++, as a front-end to simplify Sage's data entry and plotting. But Sage worksheets are saved in a binary format, which isn't documented here.
Still, Sage grows on you. It's easy to plot simple non-parametric graphs and do symbolic calculus, and its ability to interface with Python and math libraries like Numpy and Maxima makes it even more powerful (and more confusing, since many of the symbols in Numpy conflict with the ones in Sage). Thus, the author also faced the task of teaching the rudiments of Python. The book is far from complete, and you will also need a Python text like the one reviewed at left. Finch's goal was to avoid scaring people away, and he did a good job at that.
This book has lots of code examples and screen shots, all of which are grayscale. There's no way to avoid being squeezed by Python if you use Sage, and Finch also gives some general programming tips. The tips are sensible and reflect what's recommended in other books like Microsoft's Code Complete.
Should you use Sage or Mathematica? Mathematica is better. But the deciding factor for most people will not be quality, but price. The cheapest version of Mathematica that can be used commercially is the "Standard" version, which is currently $2,495. Even the Home version costs twice as much as MS Office. But for unlimited use of Mathematica's latest toy, a computational knowledge engine called the Wolfram Alpha API, you pay through the nose. If you can't swing nasal financing, Matlab and Maple are slightly cheaper—but both of these still discriminate against non-academics.
With Sage, you have to learn five things instead of just one: Python 2.x, Python 3.x, Numpy, Maxima, and Sage. So you pay with your time instead of dollars. But you get the source code, which means you can add features yourself. That also means an army of volunteers will rise up and extend it. So unless you're an academic who gets Mathematica cheap, or mathematician who needs the high-end features, the decision is what my zombie friends call a no-brainer.
feb 05, 2014