07.02.2015 Views

Python Tutorial

Python Tutorial

Python Tutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Python</strong> <strong>Tutorial</strong>, Release 2.6.2<br />

When a script file is used, it is sometimes useful to be able to run the script and enter interactive mode afterwards.<br />

This can be done by passing -i before the script. (This does not work if the script is read from standard input, for<br />

the same reason as explained in the previous paragraph.)<br />

2.1.1 Argument Passing<br />

When known to the interpreter, the script name and additional arguments thereafter are passed to the script in<br />

the variable sys.argv, which is a list of strings. Its length is at least one; when no script and no arguments<br />

are given, sys.argv[0] is an empty string. When the script name is given as ’-’ (meaning standard input),<br />

sys.argv[0] is set to ’-’. When -c command is used, sys.argv[0] is set to ’-c’. When -m module<br />

is used, sys.argv[0] is set to the full name of the located module. Options found after -c command or -m<br />

module are not consumed by the <strong>Python</strong> interpreter’s option processing but left in sys.argv for the command<br />

or module to handle.<br />

2.1.2 Interactive Mode<br />

When commands are read from a tty, the interpreter is said to be in interactive mode. In this mode it prompts<br />

for the next command with the primary prompt, usually three greater-than signs (>>>); for continuation lines it<br />

prompts with the secondary prompt, by default three dots (...). The interpreter prints a welcome message stating<br />

its version number and a copyright notice before printing the first prompt:<br />

python<br />

<strong>Python</strong> 2.6 (#1, Feb 28 2007, 00:02:06)<br />

Type "help", "copyright", "credits" or "license" for more information.<br />

>>><br />

Continuation lines are needed when entering a multi-line construct. As an example, take a look at this if statement:<br />

>>> the_world_is_flat = 1<br />

>>> if the_world_is_flat:<br />

... print "Be careful not to fall off!"<br />

...<br />

Be careful not to fall off!<br />

2.2 The Interpreter and Its Environment<br />

2.2.1 Error Handling<br />

When an error occurs, the interpreter prints an error message and a stack trace. In interactive mode, it then returns<br />

to the primary prompt; when input came from a file, it exits with a nonzero exit status after printing the stack<br />

trace. (Exceptions handled by an except clause in a try statement are not errors in this context.) Some errors<br />

are unconditionally fatal and cause an exit with a nonzero exit; this applies to internal inconsistencies and some<br />

cases of running out of memory. All error messages are written to the standard error stream; normal output from<br />

executed commands is written to standard output.<br />

Typing the interrupt character (usually Control-C or DEL) to the primary or secondary prompt cancels the<br />

input and returns to the primary prompt.<br />

1<br />

Typing an interrupt while a command is executing raises the<br />

KeyboardInterrupt exception, which may be handled by a try statement.<br />

2.2.2 Executable <strong>Python</strong> Scripts<br />

On BSD’ish Unix systems, <strong>Python</strong> scripts can be made directly executable, like shell scripts, by putting the line<br />

1 A problem with the GNU Readline package may prevent this.<br />

6 Chapter 2. Using the <strong>Python</strong> Interpreter

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!