The development process
How to write, build an run Python programs.
First you need to install Python itself. On Windows, this is as easy as running the installer .exe and you're practically set to go. On Linux you often already have a Python installed on your system, otherwise you have to download a precompiled package for your distribution or build it from the sources. This is very simple in fact (it is described in detail in the README or INSTALL file in the source archive). Here are the [http://www.python.org/download/ downloads].
When Python is installed, you can try to start it. Type python at a command prompt. You should see something like this on Unix/Linux:
[irmen@atlantis Pyro]$ python Python 2.2.1 (#1, Nov 5 2002, 13:31:44) [GCC 3.2 (Mandrake Linux 9.0 3.2-1mdk)] on linux-i386 Type "help", "copyright", "credits" or "license" for more information. >>>
and something like this on Windows:
C:\> python Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
On to developing your programs!
Interactive Python
Python has a mighty powerful feature that is unavailable in Java: the interactive prompt. You can type Python commands and even blocks of code at the prompt and Python evaluates them on the fly, displaying the results. Python remembers state so you can use this mode to try things out, calculate things, and to find out what's wrong after your program crashed. Like most interactive prompts, everything you type in is volatile, it is not stored and will be lost when you exit Python.
The prompt is usually displayed as >>>. An example of the interactive mode:
>>> yearBorn=1974 >>> currentYear=2003 >>> print "You were born",currentYear-yearBorn,"years ago." You were born 29 years ago. >>>
The text after the >>> was typed in. The You were born 29 years ago. text is the result of the print statement that was typed in.
Interactive mode is often used to dynamically inspect your program's environment, to create classes and code on the fly, and to look into existing code or objects. More on that later perhaps.
Writing a program
To write programs that persist, you have to create source files that contain your program, just like you do in Java. Java has .java source files, Python has .py source files. Python has similar file naming conventions as Java. There is one big difference: while Java requires you to name the source file after the public class that it defines within the source file, Python allows any name. That's because the Java source file is closely tied to the class it defines (there may be only one public class) and the Python source file is a module that may contain many classes and functions!
For this example, use a text editor to create a file called myprogram.py with the following contents:
import time
name = raw_input("What is your name? ")
yearBorn = input("In what year were you born? ")
currentYear = time.localtime() [0]
print
print name+", you were born",currentYear-yearBorn,"years ago." Don't bother about the meaning of the different lines in the program. That will be explained later or you can read the [http://www.python.org/doc/current/ Python documentation] to learn it.
Before running the program, there is one important thing that has to be explained first: the concept of Python modules.
Multiple modules and combining them
Java requires a separate source file for each public class. Python allows you to put all of your code in one source file, because there is no such rule for classes. But to keep your program manageable, it is usually better to divide it in different modules. Every module could contain all code necessary to perform a specific function or purpose, like a single module for user input, a single module for program output, and a module for program logic (just an example).
Calling code in another module is done by importing the module. Imagine a function localtime that is present in the time.py source file. This time.py file is the source for the time module (every module is named as the source file minus the .py suffix). You import the module from your other source files just like you would import another class in Java, like this:
import time
(but without the semicolon (;) at the end). After that, everything from that module is now accessible, like in Java, with the time. prefix. So we call the localtime function as follows:
result = time.localtime()
That is exactly what our example program does above. The time module is part of Python's standard library. That is why we didn't have to write a time.py source file for it. Exactly the same like Java's java.lang.String that is part of the standard Java runtime (rt.jar), and you don't have a String.java source file for it.
Running your software
Finally we will run our program. Python programs are executed by the Python compiler/interpreter, the same tool you started earlier to try out the interactive shell. When you start Python without arguments, you will enter the interactive mode. When you provide the filename of a module, it will load and execute that file. This is different from Java, where you give a class name. So let's try it out. Type at a command prompt: python myprogram.py and see what happens:
C:\> python myprogram.py What is your name? Irmen In what year were you born? 1974 Irmen, you were born 29 years ago. C:\>
Irmen and 1974 where typed in as answer to the questions that Python asked. The last line is the output of the program. After that, the program exits and you are returned to the command prompt.
