(12 ratings)   
By: Evelyn Mitchell
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,...
Added: 02 June 2008    Views: 69  
PathComputers    Programming    Python
Keywords: computers   python   programming   code   coder   language   coding  
Do you like this tutorial? Now you can support our team to add more :     
 
 
 
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 World
For the sake of comparison, here's the same program in Perl:


#!/usr/bin/perl

print "Hello World\n";
You probably noticed a couple of things about the Python code right away. Consider the command:


#!/usr/bin/env python
Portability 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 World
The example below concatenates the two strings without a space in between:

print "Hello" + "World"
produces the result:

HelloWorld
About the Author :
For the last five years, Evelyn Mitchell has concentrated on Web development in Perl, PHP, Python and, most recently, Zope. In the financial industry, she has programmed for online trading Web sites. Currently as managing partner of tummy.com, she is responsible for project leadership and direct supervision of developers and admins. She knows firsthand that open source development makes for better software and for better programmers. She can be reached at efm@tummy.com.
 Rate this tutorial : Rate 1Rate 2Rate 3Rate 4Rate 5
  |    Add to Favorites
  |    Send to Friend
  |    Print
Comments