Home arrow Python arrow Page 2 - Python The Other Scripting Language That Starts With

Hello World!&toc - Python

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.

TABLE OF CONTENTS:
  1. Python The Other Scripting Language That Starts With
  2. Hello World!
  3. Python and Java
  4. What is Python Bad For?
By: Evelyn Mitchell
Rating: starstarstarstarstar / 15
December 24, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

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

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

Python and Perl
If Perl is the first post-modern programming language as Larry Wall (Perl's creator) describes it, then Python can be called the first neo-classical programming language.

Both Python and Perl build on a strong understanding of the available tools for problem solving. Larry Wall used awk, sed, and shell scripting as the primary design inspirations when he started writing Perl because he was writing a language to make system administration tasks easier. Python was inspired more by object-oriented design and object-oriented tools.

Rather than striving for a maximally expressive language as Larry Wall did, Guido van Rossum (the designer of Python) chose to create a simple, powerful, elegant base for creating systems out of components. The expressive power is still there in Python, but it tends to be compartmentalized, which makes reading Python code much easier. When you need more detail on how a certain line of code works, you look at the modules and functions it is using, rather than see the full complexity on the surface.

Python and Tcl
Tcl on the surface looks a lot like Python. Both languages can be used to write elegant, simple code. Tcl is primarily a string processing language. The only datatypes in Tcl are strings and one-dimensional arrays of strings.

Python, in contrast, comes with a full range of data types including numbers (integers, long integers, floating point, octal, hex, and complex numbers), strings, lists, dictionaries (arrays indexed by keys, like hashes in Perl), and tuples (simple lists that can't be changed in place).

Tcl and Python share the same GUI toolkit, Tk. Python's interface to Tk is called Tkinter, and it is included with all recent versions of Python. You are not limited to using just Tk to build GUIs in Python, however. You can also use the Gimp Tool Kit (GTK) and Gnome extensions to GTK+.

 
 
>>> More Python Articles          >>> More By Evelyn Mitchell
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: