



(12 ratings)
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:
print "Hello World\n";
#!/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
20 Random Tutorials from the same category :
Python with other Languages
Python 101 -- Introduction to Python
Parsing in Phyton
Errors and Exceptions
Classes and objects - Python
More Control Flow Tools
Schema evolution Python
History
Data Types in Python
Python vs. Perl
Interactive Input Editing and History Substitution
Simple Statements In Python
Learn Python in 10 Minutes
Classes
Data Structures
Use serialization to store Python objects
Organization in Python
Python and Java - A Side by Side Comparison
Python Issues and some Tips
Functions and Strings -Python













