The Python Shell and Real-time Rendering:
On most computers, there is the operating system shell. This means that one can open a "terminal" and interact with the operating system by using commands and giving those commands arguments.
For Python, the shell is a program which can be used to interact with the Python interpreter. When one executes a Python program, the interpreter reads that file and executes the commands. But in the shell, that interpreting happens in real-time as you type the program into the computer.
Starting The Shell:
To start the shell, open a terminal and type "python" at the command line. On my computer this looks like this:
al@osprey:~$ python Python 2.4.1 (#2, Mar 30 2005, 21:51:10) [GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
Now you can enter your program keeping in mind one thing. Because everything is read in real-time, loops iterate immediately unless they are part of a function or class. This may not be what you want to happen.
Calculating With The Shell:
Because the input is interpreted in real-time, this also makes it perfect for complex tasks that would normally require a lot of scribbling on paper, like calculations. But, because it is the Python shell, I can define variables and call upon them at once like this:
>>> pi = 3.14159 >>> d = 25 >>> circumference = pi*d >>> circumference 78.53981
The Plain File:
Most Python programmers write their programs to a plain file using a text editor. Instead of ending the file with a '.txt' suffix, Python programs usually end with '.py', like 'myprogram.py'. The only time that this convention does not apply is when one writes CGI scripts.
In case you are not familiar with them, before there were wordprocessers, there were text editors. Editors do not format your text at all. Wordprocessors do. This is critical for your programming endeavors because Python does not know how to read Microsoft Word documents. If you would like help finding a text editor, see http://php.about.com/od/phpbasics/p/text_editor.htm
Calling the Interpreter:
To execute the program file, type the following at a command prompt:
This will work for any computer on which Python has been installed. However, Unix-like systems have a way of making things easier.
> python myprogram.py
Unix's Simpler Way:
Unix-like systems allow you to add a "bang line" to your program.A "bang line" is a line that tells the computer where to find the interpreter. It is always the first line of the program and always begins with a special sequence of characters: #!. The bang line for a Python program looks like this:
Instead of "/path/to/interpreter", enter the path which the computer must follow to find the interpreter. This also assumes that you make the file executable.