Chapter 1. Python Pocket Reference
Introduction
Python is a general-purpose, object-oriented, and open source computer programming language. It is commonly used for both standalone programs and scripting applications in a wide variety of domains, by hundreds of thousands of developers.
Python is designed to optimize developer productivity, software quality, program portability, and component integration. Python programs run on most platforms in common use, including mainframes and supercomputers, Unix and Linux, Windows and Macintosh, Java and .NET, and more.
This pocket reference summarizes Python types and statements, special method names, built-in functions and exceptions, commonly used standard library modules, and other prominent Python tools. It is intended to serve as a concise reference tool for developers and is designed to be a companion to other books that provide tutorials, code examples, and other learning materials.
This fourth edition covers both Python versions 3.0 and 2.6, and later releases in the 3.X and 2.X lines. This edition is focused primarily on Python 3.0, but also documents differences in Python 2.6, and so applies to both versions. It has been thoroughly updated for recent language and library changes and expanded for new language tools and topics.
This edition also incorporates notes about prominent enhancements in the imminent Python 3.1 release, which is intended to subsume Python 3.0 (in this book, Python 3.0 generally refers to the language variations introduced by 3.0 but present in the entire 3.X line). Much of this edition applies to earlier Python releases as well, with the exception of recent language extensions.
Conventions
The following conventions are used in this book:
[]
Items in brackets are usually optional. The exceptions are those cases where brackets are part of Pythons syntax.
*
Something followed by an asterisk can be repeated zero or more times.
a
|
b
Items separated by a bar are often alternatives.
ItalicUsed for filenames and URLs and to highlight new terms.
Constant width
Used for code, commands, and command-line options, and to indicate the names of modules, functions, attributes, variables, and methods.
Constant width italic
Used for replaceable parameter names in command syntax.
Using Code Examples
This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless youre reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from OReilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your products documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: Python Pocket Reference , Fourth Edition, by Mark Lutz. Copyright 2010 Mark Lutz, 978-0-596-15808-8.
If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .
Safari Books Online
Note
Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly.
With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features.
OReilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from OReilly and other publishers, sign up for free at http://my.safaribooksonline.com.
Command-Line Options
Command lines are used to launch Python programs from a system shell prompt. Command-line options intended for Python itself appear before the specification of the program code to be run. Options intended for the code to be run appear after the program specification. Command lines have the following format:
python [
option*
] [
scriptfilename
| -c
command
| -m
module
| - ] [
arg*
]
Python Options
-b
Issues warnings for calling str()
with a bytes
or bytearray
object, and comparing a bytes
or bytearray
with a str
. Option -bb
issues errors instead.
-B
Do not write .pyc or .pyo byte-code files on imports.
-d
Turns on parser debugging output (for developers of the Python core).
-E
Ignores Python environment variables described ahead (such as PYTHONPATH
).
-h
Prints help message and exit.
-i
Enters interactive mode after executing a script. Useful for postmortem debugging.
-O
Optimizes generated byte-code (create and use .pyo byte-code files). Currently yields a minor performance improvement.
-OO
Operates like -O
, the previous option, but also removes docstrings from byte-code.
-s
Do not add the user site directory to the sys.path
module search path.
-S
Do not imply import site on initialization.
-u
Forces stdout and stderr to be unbuffered and binary.