|
Helping ordinary people create extraordinary websites! |
Python 101By Evelyn Mitchell2005-04-27
The Other Scripting Language that Starts with "P" Columnist Evelyn Mitchell gives an introduction to Python, a unique, open-source scripting language popular with many Linux coders. Once you have glimpsed the power and simplicity of Python, you may never go back. Evelyn compares Python with Perl, Java, and Tcl, illustrating differences along the way with explicit code segments. Python is the other scripting language that starts with "P". It was named for Monty Python, the comedy troupe, and references to their skits and films (most commonly to "Spam") appear frequently in Python programs. Highly popular with Linux users and in the open source community in general, Python has a number of advantages over other languages like Java and Perl, some of which we will cover in this article. Let's jump right in with the unavoidable and always instructive Hello World example, as a springboard into the discussion: #!/usr/bin/env python print "Hello World"produces the result: Hello WorldFor the sake of comparison, here's the same program in Perl: #!/usr/bin/perlYou probably noticed a couple of things about the Python code right away. Consider the command: #!/usr/bin/env pythonPortability is one of Python's strong points. Using this #! line rather than the actual location of python on your system allows you to move this code, without modification, to any system that has /usr/bin/env (the equivalent #! line for Perl points directly at the Perl binary).The result is that you can run Python code on more platforms than Java. You can even run Python code within a Java Virtual Machine, using JPython (see Resources). Plus, because Python is an open-source language, it is not subject to the same sort of OS-specific tweaking that Java is known for. (Actually, there are some "OS-specific" extensions to Python -- and not just for Microsoft operating systems. There are SGI-specific sound libraries, for example. These extensions notwithstanding, it is still quite easy to write OS-agnostic programs under Python.) Python is a scripting language, so you don't have to define constants or variables before you use them. This can speed code creation when you're trying out alternative solutions to a problem for a prototype. What's more, as the Hello World example illustrates, Python doesn't use a lot of punctuation. In particular, Python doesn't use the semicolon (;) to mark the end of line, eliminating a whole class of errors that I tend to make in languages like Perl due to mistyping a character. You'll also notice that you didn't have to write "Hello World\n", as in Perl. This is because the print statement in Python is smart enough to assume that you usually want a newline after your prints. If you don't want a newline, just add a comma at the end. The comma operator inserts a space: print "hello", "world"produces the result: Hello WorldThe example below concatenates the two strings without a space in between: print "Hello" + "World"produces the result: HelloWorld Tutorial Pages: » The Other Scripting Language that Starts with "P" » Python and Perl » Python and Tcl » Python and Java » Why is Python popular with Linux users? » What is Python Bad For? » Who uses Python? » How to get Python » Resources First published by IBM DeveloperWorks |
|