We'll look at interaction on the command line first, because it is
the simplest way to interact with Python. It parallels scripted execution,
and helps us visualize how Python application programs work. This is the
heart of IDLE as well as the foundation for any application programs we
Starting and Stopping Command-Line Python
Starting and stopping Python varies with your operating system.
Generally, all of the variations are nearly identical, and differ only
in minor details.
Windows. There are two ways to start interactive Python under Windows.
You can run the command tool (
cmd.exe) or you can
run the Python (Command Line) program. If
cmd.exe, enter the command
to start interactive Python. If you run
menu, the result is essentially the
To exit from Python, enter the end-of-file character sequence,
Mac OS, GNU/Linux and Unix. You will run the Terminal tool. You
can enter the command
to start interactive
To exit from Python, enter the end-of-file character,
Entering Python Statements
When we run the Python interpreter (called
Windows), we see a greeting like the following:
Python 2.5.1 (r251:54863, Oct 5 2007, 21:08:09)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
When we get the
>>> prompt, the Python
interpreter is looking for input. We can type any Python statements we
want. Each complete statement is executed when it is entered. Some
statements are called compound statements; a
compound statement has a suite of statements indented inside of it.
Python will wait until the entire compound statement is entered before
it does does the evaluation.
In this section, we'll emphasize the prompts from Python. This can
help newbies see the complete cycle of interaction between themselves
and the Python interpreter. In the long run we'll be writing scipts and
won't see this level of interaction.
The Return Key. For this section only, we'll also emphasize the usually
) key. When we start using compound
statements in Chapter 7, Truth, Comparison and Conditional
, we'll add some additional
syntax rules. For now, however, statements can't be indented; they
must begin without any leading spaces or tabs.
2 + 3 ↵
This shows Python doing simple integer arithmetic. When you
2 + 3
and then hit
, the Python interpreter evaluated this
statement. Since the statement was only an expression, Python printed
The usual assumption for numbers is that they are 32-bit integers,
with a range of -2,147,483,648 to 2,147,483,647. If you include a
decimal point, you get floating decimal-point numbers, called floats.
Python's floats are anachronistically called “double
precision”. We'll dig into to the various kinds of numbers in
Simple Numeric Expressions and Output.
Arithmetic operators include the usual culprits:
** standing for addition, subtraction, multiplication,
division, modulo (remainder after division) and raising to a power. The
usual mathematical rules of operator precedence (multiplys and divides
done before adds and subtracts) are in full force, and ()'s are used to
group terms against precedence rules.
For example, converting 65° Fahrenheit to Celsius is done as
(65 - 32) * 5 / 9 ↵
Note that the first example used all integer values, and the
result was an integer result. In the second example, the presence of a
float caused all the values to be coerced to float.
End-Of-Statement Rules. What happens when the expression is obviously incomplete?
( 65 - 32 ) * 5 / ↵
File "<stdin>", line 1
SyntaxError: invalid syntax
One basic syntax rule is that statements must be complete on a
single line. There are few formal lexical rules for the structure of
statements; we'll present them more formally after we've experienced
There is an escape clause in the basic rule of “one
statement one line”. When the parenthesis are incomplete, Python
will allow the statement to run on to multiple lines.
( 65 - 32
) * 5 / 9
It is also possible to continue a long statement using a
escape just before hitting
at the end of the line.
5 + 6 *\ ↵
This escape allows you to break up an
extremely long statement for easy reading. It creates an escape from the
usual meaning of the end-of-line character; the end-of-line is demoted
to just another whitespace character, and loses it's meaning of
“end-of-statement, commence execution now”.
Indentation. Python relies heavily on indendentation to make a program
readable. When interacting with Python, we are often typing simple
expression statements, which are not indented. Later, when we start
typing compound statements, indentation will begin to matter.
Here's what happens if we try to indent a simple expression
SyntaxError: invalid syntax
Command History. When we type a complete expression, Python evaluates it and
displays the result. When we type a complete statement, Python
executes it silently. We'll see more of this, starting in Chapter 6, Variables, Assignment and Input
. We'll make heavy use of this feature in Chapter 4, Simple Numeric Expressions and Output
Small mistakes can be frustrating when typing a long or complex
statement. Python has a reasonable command history capability, so you
can use the
key to recover a previous
statement. Generally, you'll prefer to create script files and run the
scripts. When debugging a problem, however, interactive mode can be
handy for experimenting.
One of the desirable features of well-written Python is that most
things can be tested and demonstrated in small code fragments. Often a
single line of easy-to-enter code is the desired style for interesting
programming features. Many examples in reference manuals and unit test
scripts are simply captures of interactive Python sessions.