Python Tutorial Python
\
H
Release 2.5b2
Guido van Rossum
Fred L. Drake, Jr., editor
11th July, 2006
Python Software Foundation
Email: docs@python.org
Copyright © 2001-2006 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.
See the end of this document for complete license and permissions information.
Abstract
Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple
but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its
interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most
platforms.
Python
´
«
N
´
Æ
S
r
?
§
ó
"
§
¹
p
p
?
ê
â(
§
U
^
{
ü
p
ª
?
1
¡
é
?
§
"
Python
`
ä
{
Ú
Ä
a
.
§
±
9
§
U
,
)
º
U
å
§
¦
Ù
¤
õ
ê
²
þ
2
·
^u
+
n
ó
Ú
m
u
¸
"
The Python interpreter and the extensive standard library are freely available in source or binary form for all major
platforms from the Python Web site, http://www.python.org/, and may be freely distributed. The same site
also contains distributions of and pointers to many free third party Python modules, programs and tools, and additional
documentation.
Python
)
º
ì
9
Ù
*
Ð
I
O
¥
è
Ú
?
È
±
l
Python
Web
Õ
:
, http://www.python.org/,
9
Ù
¤
k
º
Õ
þ
¤
¼
§
¿
±
g
d
u
Ù
"
T
Õ
:
þ
J
ø
Python
1
n
¬
§
§
S
§
ó
ä
§
±
9
N
\
©
"
The Python interpreter is easily extended with new functions and data types implemented in C or C
++
(or other
languages callable from C). Python is also suitable as an extension language for customizable applications.
Python
)
º
ì
±
é
N
´
Ï
L
C
½
ö
C
++
£
½
ö
Ù
§
±
Ï
L
C
N
^
ó
¤
*
Ð
#
¼
ê
Ú
ê
â
a
.
"
Python
±
½
A^
*
Ð
ó
"
This tutorial introduces the reader informally to the basic concepts and features of the Python language and system. It
helps to have a Python interpreter handy for hands-on experience, but all examples are self-contained, so the tutorial
can be read off-line as well.
Ã
þ
Ö
ö
0
Python
ó
9
Ù
X
Ú
Ä
£
V
g
"
Ü
Python
)
º
ì
Æ
S
¬
é
k
Ï
§
Ø
L
¤
k
~
f
Ñ
®
)
3
©
¥
§
¤
±
ù
Ã
þ
é
±
l
Ö
"
For a description of standard objects and modules, see the Python Library Reference document. The Python Refer-
ence Manual gives a more formal definition of the language. To write extensions in C or C
++
, read Extending and
Embedding the Python Interpreter and Python/C API Reference. There are also several books covering Python in
depth.
I
k
'
I
O
é
Ú
¬
[
0
{
§
Î
Python
¥
ë
Ã
þ
©
"
Python
ë
Ã
þ
J
ø
õ
'
u
ó
¡
ª
`
²
"
I
?
C
½
C
++
*
Ð
§
Ö
Python
)
º
ì
*
Ð
Ú
8
¤
±
9
Python/C API
ë
Ã
þ
"
ù
A
Ö
º
X
Ý
þ
Python
£
"
This tutorial does not attempt to be comprehensive and cover every single feature, or even every commonly used
feature. Instead, it introduces many of Python’s most noteworthy features, and will give you a good idea of the
language’s flavor and style. After reading it, you will be able to read and write Python modules and programs, and you
will be ready to learn more about the various Python library modules described in the Python Library Reference.
Ã
þØ
¬
º
X
Python
¤
k
õ
U
§
Ø
¬
)
º
¤
^
¤
k
'
£
"
§
§
0
N
õ
Python
¥
Ú
<
5
8
õ
U
§
ù
¬
éÖ
öÝ
º
ù
ó
º
k
Ï
"
Ö
L
§
§
\
A
T
±
Ö
Ú
?
Python
¬
Ú
§
S
§
e
5
±
l
Python
¥
ë
Ã
þ
¥
?
Ú
Æ
S
Python
E
,
õ
C
¥
Ú
¬
"
CONTENTS
1 Whetting Your Appetite
mmm
èèè
1
2 Using the Python Interpreter
¦¦¦
^^^
Python
)))
ººº
ììì
5
2.1 Invoking the Interpreter
N
^
)
º
ì
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 The Interpreter and Its Environment
)
º
ì
9
Ù
¸
. . . . . . . . . . . . . . . . . . . . . . . . . 7
3 More Control Flow Tools
\\\
666
§§§
11
3.1 if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2 for Statements for
é
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.3 The range() Function range()
¼
ê
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.4 break and continue Statements, and else Clauses on Loops break
Ú
continue
é
,
±
9
Ì
¥
else
f
é
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.5 pass Statements pass
é
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.6 Defining Functions
½
Â
¼
ê
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.7 More on Defining Functions
\
¼
ê
½
Â
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4 Data Structures
êêê
âââ(((
23
4.1 More on Lists
\
ó
L
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.2 The del statement del
é
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.3 Tuples and Sequences
|
Ú
S
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.4 Sets
8
Ü
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
4.5 Dictionaries
i
;
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
4.6 Looping Techniques
Ì
E
â
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4.7 More on Conditions
\
^
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
4.8 Comparing Sequences and Other Types
Ø
Ó
S
a
.
'
. . . . . . . . . . . . . . . . . . . . . 34
5 Modules
¬¬¬
37
5.1 More on Modules
\
¬
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
5.2 Standard Modules
I
O
¬
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5.3 The dir() Function dir()
¼
ê
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
5.4 Packages
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
6 Input and Output
ÑÑÑ
\\\
ÚÚÚ
ÑÑÑ
ÑÑÑ
49
6.1 Fancier Output Formatting
O
Ñ
Ñ
ª
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
6.2 Reading and Writing Files
Ö
©
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
7 Errors and Exceptions
ØØØ
ÚÚÚ
ÉÉÉ
~~~
57
7.1 Syntax Errors
{
Ø
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
7.2 Exceptions
É
~
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
7.3 Handling Exceptions
?
n
É
~
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
i