Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

Chapter 3. Getting Started

Interacting with Python

Python is an interpreted, dynamic language. The Python interpreter can be used in two modes: interactive and scripted. In interactive mode, Python responds to each statement while we type. In script mode, we give Python a file of statements and turn it loose to interpret all of the statements in that script. Both modes produce identical results. When we're producing a finished application program, we set it up to run as a script. When we're experimenting or exploring, however, we may use Python interactively.

We'll describe the interactive command-line mode for entering simple Python statements in Command-Line Interaction. In The IDLE Development Environment we'll cover the basics of interactive Python in the IDLE environment. We'll describes the script mode for running Python program files in Script Mode.

Command-Line Interaction

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

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 you run cmd.exe, enter the command python to start interactive Python. If you run Python (Command Line) from the Start menu, the result is essentially the same.

To exit from Python, enter the end-of-file character sequence, Control-Z and Return .

Mac OS, GNU/Linux and Unix. You will run the Terminal tool. You can enter the command python to start interactive Python.

To exit from Python, enter the end-of-file character, Control-D .

Entering Python Statements

When we run the Python interpreter (called python, or Python.exe in Windows), we see a greeting like the following:

52:~ slott$ 
env python

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 invisible Return ( ) key. When we start using compound statements in Chapter 7, Truth, Comparison and Conditional Processing , 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 ↵

5
>>>

This shows Python doing simple integer arithmetic. When you entered 2 + 3 and then hit Return , the Python interpreter evaluated this statement. Since the statement was only an expression, Python printed the results.

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: +, -, *, /, % and ** 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 follows:

>>> 
(65 - 32) * 5 / 9 ↵

18
>>> 
(65.-32)*5/9
18.333333333333332
>>>

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
    (65-32)*5 /
              ^
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 them.

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
18
>>>

It is also possible to continue a long statement using a \ escape just before hitting Return at the end of the line.

>>> 
5 + 6 *\ ↵

... 
7↵

47
>>>

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

>>>       5+6
      
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 up-arrow 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.


 
 
  Published under the terms of the Open Publication License Design by Interspire