1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
|
Python 2.3 Quick Reference
25 Jan 2003 upgraded by Raymond Hettinger for Python 2.3
16 May 2001 upgraded by Richard Gruet and Simon Brunning for Python 2.0
2000/07/18 upgraded by Richard Gruet, rgruet@intraware.com for Python 1.5.2
from V1.3 ref
1995/10/30, by Chris Hoffmann, choffman@vicorp.com
Based on:
Python Bestiary, Author: Ken Manheimer, ken.manheimer@nist.gov
Python manuals, Authors: Guido van Rossum and Fred Drake
What's new in Python 2.0, Authors: A.M. Kuchling and Moshe Zadka
python-mode.el, Author: Tim Peters, tim_one@email.msn.com
and the readers of comp.lang.python
Python's nest: http://www.python.org Developement: http://
python.sourceforge.net/ ActivePython : http://www.ActiveState.com/ASPN/
Python/
newsgroup: comp.lang.python Help desk: help@python.org
Resources: http://starship.python.net/
http://www.vex.net/parnassus/
http://aspn.activestate.com/ASPN/Cookbook/Python
FAQ: http://www.python.org/cgi-bin/faqw.py
Full documentation: http://www.python.org/doc/
Excellent reference books:
Python Essential Reference by David Beazley (New Riders)
Python Pocket Reference by Mark Lutz (O'Reilly)
Invocation Options
python [-diOStuUvxX?] [-c command | script | - ] [args]
Invocation Options
Option Effect
-c cmd program passed in as string (terminates option list)
-d Outputs parser debugging information (also PYTHONDEBUG=x)
-E ignore environment variables (such as PYTHONPATH)
-h print this help message and exit
-i Inspect interactively after running script (also PYTHONINSPECT=x) and
force prompts, even if stdin appears not to be a terminal
-O optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)
-OO remove doc-strings in addition to the -O optimizations
-Q arg division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew
-S Don't perform 'import site' on initialization
-t Issue warnings about inconsistent tab usage (-tt: issue errors)
-u Unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x).
-v Verbose (trace import statements) (also PYTHONVERBOSE=x)
-W arg : warning control (arg is action:message:category:module:lineno)
-x Skip first line of source, allowing use of non-unix Forms of #!cmd
-? Help!
-c Specify the command to execute (see next section). This terminates the
command option list (following options are passed as arguments to the command).
the name of a python file (.py) to execute read from stdin.
script Anything afterward is passed as options to python script or command,
not interpreted as an option to interpreter itself.
args passed to script or command (in sys.argv[1:])
If no script or command, Python enters interactive mode.
* Available IDEs in std distrib: IDLE (tkinter based, portable), Pythonwin
(Windows).
Environment variables
Environment variables
Variable Effect
PYTHONHOME Alternate prefix directory (or prefix;exec_prefix). The
default module search path uses prefix/lib
Augments the default search path for module files. The format
is the same as the shell's $PATH: one or more directory
pathnames separated by ':' or ';' without spaces around
(semi-)colons!
PYTHONPATH On Windows first search for Registry key HKEY_LOCAL_MACHINE\
Software\Python\PythonCore\x.y\PythonPath (default value). You
may also define a key named after your application with a
default string value giving the root directory path of your
app.
If this is the name of a readable file, the Python commands in
PYTHONSTARTUP that file are executed before the first prompt is displayed in
interactive mode (no default).
PYTHONDEBUG If non-empty, same as -d option
PYTHONINSPECT If non-empty, same as -i option
PYTHONSUPPRESS If non-empty, same as -s option
PYTHONUNBUFFERED If non-empty, same as -u option
PYTHONVERBOSE If non-empty, same as -v option
PYTHONCASEOK If non-empty, ignore case in file/module names (imports)
Notable lexical entities
Keywords
and del for is raise
assert elif from lambda return
break else global not try
class except if or while
continue exec import pass yield
def finally in print
* (list of keywords in std module: keyword)
* Illegitimate Tokens (only valid in strings): @ $ ?
* A statement must all be on a single line. To break a statement over
multiple lines use "\", as with the C preprocessor.
Exception: can always break when inside any (), [], or {} pair, or in
triple-quoted strings.
* More than one statement can appear on a line if they are separated with
semicolons (";").
* Comments start with "#" and continue to end of line.
Identifiers
(letter | "_") (letter | digit | "_")*
* Python identifiers keywords, attributes, etc. are case-sensitive.
* Special forms: _ident (not imported by 'from module import *'); __ident__
(system defined name);
__ident (class-private name mangling)
Strings
"a string enclosed by double quotes"
'another string delimited by single quotes and with a " inside'
'''a string containing embedded newlines and quote (') marks, can be
delimited with triple quotes.'''
""" may also use 3- double quotes as delimiters """
u'a unicode string' U"Another unicode string"
r'a raw string where \ are kept (literalized): handy for regular
expressions and windows paths!'
R"another raw string" -- raw strings cannot end with a \
ur'a unicode raw string' UR"another raw unicode"
Use \ at end of line to continue a string on next line.
adjacent strings are concatened, e.g. 'Monty' ' Python' is the same as
'Monty Python'.
u'hello' + ' world' --> u'hello world' (coerced to unicode)
String Literal Escapes
\newline Ignored (escape newline)
\\ Backslash (\) \e Escape (ESC) \v Vertical Tab (VT)
\' Single quote (') \f Formfeed (FF) \OOO char with octal value OOO
\" Double quote (") \n Linefeed (LF)
\a Bell (BEL) \r Carriage Return (CR) \xHH char with hex value HH
\b Backspace (BS) \t Horizontal Tab (TAB)
\uHHHH unicode char with hex value HHHH, can only be used in unicode string
\UHHHHHHHH unicode char with hex value HHHHHHHH, can only be used in unicode string
\AnyOtherChar is left as-is
* NUL byte (\000) is NOT an end-of-string marker; NULs may be embedded in
strings.
* Strings (and tuples) are immutable: they cannot be modified.
Numbers
Decimal integer: 1234, 1234567890546378940L (or l)
Octal integer: 0177, 0177777777777777777 (begin with a 0)
Hex integer: 0xFF, 0XFFFFffffFFFFFFFFFF (begin with 0x or 0X)
Long integer (unlimited precision): 1234567890123456
Float (double precision): 3.14e-10, .001, 10., 1E3
Complex: 1J, 2+3J, 4+5j (ends with J or j, + separates (float) real and
imaginary parts)
Sequences
* String of length 0, 1, 2 (see above)
'', '1', "12", 'hello\n'
* Tuple of length 0, 1, 2, etc:
() (1,) (1,2) # parentheses are optional if len > 0
* List of length 0, 1, 2, etc:
[] [1] [1,2]
Indexing is 0-based. Negative indices (usually) mean count backwards from end
of sequence.
Sequence slicing [starting-at-index : but-less-than-index]. Start defaults to
'0'; End defaults to 'sequence-length'.
a = (0,1,2,3,4,5,6,7)
a[3] ==> 3
a[-1] ==> 7
a[2:4] ==> (2, 3)
a[1:] ==> (1, 2, 3, 4, 5, 6, 7)
a[:3] ==> (0, 1, 2)
a[:] ==> (0,1,2,3,4,5,6,7) # makes a copy of the sequence.
Dictionaries (Mappings)
{} # Zero length empty dictionary
{1 : 'first'} # Dictionary with one (key, value) pair
{1 : 'first', 'next': 'second'}
dict([('one',1),('two',2)]) # Construct a dict from an item list
dict('one'=1, 'two'=2) # Construct a dict using keyword args
dict.fromkeys(['one', 'keys']) # Construct a dict from a sequence
Operators and their evaluation order
Operators and their evaluation order
Highest Operator Comment
(...) [...] {...} `...` Tuple, list & dict. creation; string
conv.
s[i] s[i:j] s.attr f(...) indexing & slicing; attributes, fct
calls
+x, -x, ~x Unary operators
x**y Power
x*y x/y x%y x//y mult, division, modulo, floor division
x+y x-y addition, subtraction
x<<y x>>y Bit shifting
x&y Bitwise and
x^y Bitwise exclusive or
x|y Bitwise or
x<y x<=y x>y x>=y x==y x!=y Comparison,
x<>y identity,
x is y x is not y membership
x in s x not in s
not x boolean negation
x and y boolean and
x or y boolean or
Lowest lambda args: expr anonymous function
Alternate names are defined in module operator (e.g. __add__ and add for +)
Most operators are overridable.
Many binary operators also support augmented assignment:
x += 1 # Same as x = x + 1
Basic Types and Their Operations
Comparisons (defined between *any* types)
Comparisons
Comparison Meaning Notes
< strictly less than (1)
<= less than or equal to
> strictly greater than
>= greater than or equal to
== equal to
!= or <> not equal to
is object identity (2)
is not negated object identity (2)
Notes :
Comparison behavior can be overridden for a given class by defining special
method __cmp__.
The above comparisons return True or False which are of type bool
(a subclass of int) and behave exactly as 1 or 0 except for their type and
that they print as True or False instead of 1 or 0.
(1) X < Y < Z < W has expected meaning, unlike C
(2) Compare object identities (i.e. id(object)), not object values.
Boolean values and operators
Boolean values and operators
Value or Operator Returns Notes
None, numeric zeros, empty sequences and False
mappings
all other values True
not x True if x is False, else
True
x or y if x is False then y, else (1)
x
x and y if x is False then x, else (1)
y
Notes :
Truth testing behavior can be overridden for a given class by defining
special method __nonzero__.
(1) Evaluate second arg only if necessary to determine outcome.
None
None is used as default return value on functions. Built-in single object
with type NoneType.
Input that evaluates to None does not print when running Python
interactively.
Numeric types
Floats, integers and long integers.
Floats are implemented with C doubles.
Integers are implemented with C longs.
Long integers have unlimited size (only limit is system resources)
Operators on all numeric types
Operators on all numeric types
Operation Result
abs(x) the absolute value of x
int(x) x converted to integer
long(x) x converted to long integer
float(x) x converted to floating point
-x x negated
+x x unchanged
x + y the sum of x and y
x - y difference of x and y
x * y product of x and y
x / y quotient of x and y
x % y remainder of x / y
divmod(x, y) the tuple (x/y, x%y)
x ** y x to the power y (the same as pow(x, y))
Bit operators on integers and long integers
Bit operators
Operation >Result
~x the bits of x inverted
x ^ y bitwise exclusive or of x and y
x & y bitwise and of x and y
x | y bitwise or of x and y
x << n x shifted left by n bits
x >> n x shifted right by n bits
Complex Numbers
* represented as a pair of machine-level double precision floating point
numbers.
* The real and imaginary value of a complex number z can be retrieved through
the attributes z.real and z.imag.
Numeric exceptions
TypeError
raised on application of arithmetic operation to non-number
OverflowError
numeric bounds exceeded
ZeroDivisionError
raised when zero second argument of div or modulo op
FloatingPointError
raised when a floating point operation fails
Operations on all sequence types (lists, tuples, strings)
Operations on all sequence types
Operation Result Notes
x in s True if an item of s is equal to x, else False
x not in s False if an item of s is equal to x, else True
for x in s: loops over the sequence
s + t the concatenation of s and t
s * n, n*s n copies of s concatenated
s[i] i'th item of s, origin 0 (1)
s[i:j] slice of s from i (included) to j (excluded) (1), (2)
len(s) length of s
min(s) smallest item of s
max(s) largest item of (s)
iter(s) returns an iterator over s. iterators define __iter__ and next()
Notes :
(1) if i or j is negative, the index is relative to the end of the string,
ie len(s)+ i or len(s)+j is
substituted. But note that -0 is still 0.
(2) The slice of s from i to j is defined as the sequence of items with
index k such that i <= k < j.
If i or j is greater than len(s), use len(s). If i is omitted, use
len(s). If i is greater than or
equal to j, the slice is empty.
Operations on mutable (=modifiable) sequences (lists)
Operations on mutable sequences
Operation Result Notes
s[i] =x item i of s is replaced by x
s[i:j] = t slice of s from i to j is replaced by t
del s[i:j] same as s[i:j] = []
s.append(x) same as s[len(s) : len(s)] = [x]
s.count(x) return number of i's for which s[i] == x
s.extend(x) same as s[len(s):len(s)]= x
s.index(x) return smallest i such that s[i] == x (1)
s.insert(i, x) same as s[i:i] = [x] if i >= 0
s.pop([i]) same as x = s[i]; del s[i]; return x (4)
s.remove(x) same as del s[s.index(x)] (1)
s.reverse() reverse the items of s in place (3)
s.sort([cmpFct]) sort the items of s in place (2), (3)
Notes :
(1) raise a ValueError exception when x is not found in s (i.e. out of
range).
(2) The sort() method takes an optional argument specifying a comparison
fct of 2 arguments (list items) which should
return -1, 0, or 1 depending on whether the 1st argument is
considered smaller than, equal to, or larger than the 2nd
argument. Note that this slows the sorting process down considerably.
(3) The sort() and reverse() methods modify the list in place for economy
of space when sorting or reversing a large list.
They don't return the sorted or reversed list to remind you of this
side effect.
(4) [New 1.5.2] The optional argument i defaults to -1, so that by default the last
item is removed and returned.
Operations on mappings (dictionaries)
Operations on mappings
Operation Result Notes
len(d) the number of items in d
d[k] the item of d with key k (1)
d[k] = x set d[k] to x
del d[k] remove d[k] from d (1)
d.clear() remove all items from d
d.copy() a shallow copy of d
d.get(k,defaultval) the item of d with key k (4)
d.has_key(k) True if d has key k, else False
d.items() a copy of d's list of (key, item) pairs (2)
d.iteritems() an iterator over (key, value) pairs (7)
d.iterkeys() an iterator over the keys of d (7)
d.itervalues() an iterator over the values of d (7)
d.keys() a copy of d's list of keys (2)
d1.update(d2) for k, v in d2.items(): d1[k] = v (3)
d.values() a copy of d's list of values (2)
d.pop(k) remove d[k] and return its value
d.popitem() remove and return an arbitrary (6)
(key, item) pair
d.setdefault(k,defaultval) the item of d with key k (5)
Notes :
TypeError is raised if key is not acceptable
(1) KeyError is raised if key k is not in the map
(2) Keys and values are listed in random order
(3) d2 must be of the same type as d1
(4) Never raises an exception if k is not in the map, instead it returns
defaultVal.
defaultVal is optional, when not provided and k is not in the map,
None is returned.
(5) Never raises an exception if k is not in the map, instead it returns
defaultVal, and adds k to map with value defaultVal. defaultVal is
optional. When not provided and k is not in the map, None is returned and
added to map.
(6) Raises a KeyError if the dictionary is emtpy.
(7) While iterating over a dictionary, the values may be updated but
the keys cannot be changed.
Operations on strings
Note that these string methods largely (but not completely) supersede the
functions available in the string module.
Operations on strings
Operation Result Notes
s.capitalize() return a copy of s with only its first character
capitalized.
s.center(width) return a copy of s centered in a string of length width (1)
.
s.count(sub[ return the number of occurrences of substring sub in (2)
,start[,end]]) string s.
s.decode(([ return a decoded version of s. (3)
encoding
[,errors]])
s.encode([ return an encoded version of s. Default encoding is the
encoding current default string encoding. (3)
[,errors]])
s.endswith(suffix return true if s ends with the specified suffix, (2)
[,start[,end]]) otherwise return False.
s.expandtabs([ return a copy of s where all tab characters are (4)
tabsize]) expanded using spaces.
s.find(sub[,start return the lowest index in s where substring sub is (2)
[,end]]) found. Return -1 if sub is not found.
s.index(sub[ like find(), but raise ValueError when the substring is (2)
,start[,end]]) not found.
s.isalnum() return True if all characters in s are alphanumeric, (5)
False otherwise.
s.isalpha() return True if all characters in s are alphabetic, (5)
False otherwise.
s.isdigit() return True if all characters in s are digit (5)
characters, False otherwise.
s.islower() return True if all characters in s are lowercase, False (6)
otherwise.
s.isspace() return True if all characters in s are whitespace (5)
characters, False otherwise.
s.istitle() return True if string s is a titlecased string, False (7)
otherwise.
s.isupper() return True if all characters in s are uppercase, False (6)
otherwise.
s.join(seq) return a concatenation of the strings in the sequence
seq, seperated by 's's.
s.ljust(width) return s left justified in a string of length width. (1),
(8)
s.lower() return a copy of s converted to lowercase.
s.lstrip() return a copy of s with leading whitespace removed.
s.replace(old, return a copy of s with all occurrences of substring (9)
new[, maxsplit]) old replaced by new.
s.rfind(sub[ return the highest index in s where substring sub is (2)
,start[,end]]) found. Return -1 if sub is not found.
s.rindex(sub[ like rfind(), but raise ValueError when the substring (2)
,start[,end]]) is not found.
s.rjust(width) return s right justified in a string of length width. (1),
(8)
s.rstrip() return a copy of s with trailing whitespace removed.
s.split([sep[ return a list of the words in s, using sep as the (10)
,maxsplit]]) delimiter string.
s.splitlines([ return a list of the lines in s, breaking at line (11)
keepends]) boundaries.
s.startswith return true if s starts with the specified prefix,
(prefix[,start[ otherwise return false. (2)
,end]])
s.strip() return a copy of s with leading and trailing whitespace
removed.
s.swapcase() return a copy of s with uppercase characters converted
to lowercase and vice versa.
return a titlecased copy of s, i.e. words start with
s.title() uppercase characters, all remaining cased characters
are lowercase.
s.translate(table return a copy of s mapped through translation table (12)
[,deletechars]) table.
s.upper() return a copy of s converted to uppercase.
s.zfill(width) return a string padded with zeroes on the left side and
sliding a minus sign left if necessary. never truncates.
Notes :
(1) Padding is done using spaces.
(2) If optional argument start is supplied, substring s[start:] is
processed. If optional arguments start and end are supplied, substring s[start:
end] is processed.
(3) Optional argument errors may be given to set a different error handling
scheme. The default for errors is 'strict', meaning that encoding errors raise
a ValueError. Other possible values are 'ignore' and 'replace'.
(4) If optional argument tabsize is not given, a tab size of 8 characters
is assumed.
(5) Returns false if string s does not contain at least one character.
(6) Returns false if string s does not contain at least one cased
character.
(7) A titlecased string is a string in which uppercase characters may only
follow uncased characters and lowercase characters only cased ones.
(8) s is returned if width is less than len(s).
(9) If the optional argument maxsplit is given, only the first maxsplit
occurrences are replaced.
(10) If sep is not specified or None, any whitespace string is a separator.
If maxsplit is given, at most maxsplit splits are done.
(11) Line breaks are not included in the resulting list unless keepends is
given and true.
(12) table must be a string of length 256. All characters occurring in the
optional argument deletechars are removed prior to translation.
String formatting with the % operator
formatString % args--> evaluates to a string
* formatString uses C printf format codes : %, c, s, i, d, u, o, x, X, e, E,
f, g, G, r (details below).
* Width and precision may be a * to specify that an integer argument gives
the actual width or precision.
* The flag characters -, +, blank, # and 0 are understood. (details below)
* %s will convert any type argument to string (uses str() function)
* args may be a single arg or a tuple of args
'%s has %03d quote types.' % ('Python', 2) # => 'Python has 002 quote types.'
* Right-hand-side can also be a mapping:
a = '%(lang)s has %(c)03d quote types.' % {'c':2, 'lang':'Python}
(vars() function very handy to use on right-hand-side.)
Format codes
Conversion Meaning
d Signed integer decimal.
i Signed integer decimal.
o Unsigned octal.
u Unsigned decimal.
x Unsigned hexidecimal (lowercase).
X Unsigned hexidecimal (uppercase).
e Floating point exponential format (lowercase).
E Floating point exponential format (uppercase).
f Floating point decimal format.
F Floating point decimal format.
g Same as "e" if exponent is greater than -4 or less than precision,
"f" otherwise.
G Same as "E" if exponent is greater than -4 or less than precision,
"F" otherwise.
c Single character (accepts integer or single character string).
r String (converts any python object using repr()).
s String (converts any python object using str()).
% No argument is converted, results in a "%" character in the result.
(The complete specification is %%.)
Conversion flag characters
Flag Meaning
# The value conversion will use the ``alternate form''.
0 The conversion will be zero padded.
- The converted value is left adjusted (overrides "-").
(a space) A blank should be left before a positive number (or empty
string) produced by a signed conversion.
+ A sign character ("+" or "-") will precede the conversion (overrides a
"space" flag).
File Objects
Created with built-in function open; may be created by other modules' functions
as well.
Operators on file objects
File operations
Operation Result
f.close() Close file f.
f.fileno() Get fileno (fd) for file f.
f.flush() Flush file f's internal buffer.
f.isatty() True if file f is connected to a tty-like dev, else False.
f.read([size]) Read at most size bytes from file f and return as a string
object. If size omitted, read to EOF.
f.readline() Read one entire line from file f.
f.readlines() Read until EOF with readline() and return list of lines read.
Set file f's position, like "stdio's fseek()".
f.seek(offset[, whence == 0 then use absolute indexing.
whence=0]) whence == 1 then offset relative to current pos.
whence == 2 then offset relative to file end.
f.tell() Return file f's current position (byte offset).
f.write(str) Write string to file f.
f.writelines(list Write list of strings to file f.
)
File Exceptions
EOFError
End-of-file hit when reading (may be raised many times, e.g. if f is a
tty).
IOError
Other I/O-related I/O operation failure.
OSError
OS system call failed.
Advanced Types
-See manuals for more details -
+ Module objects
+ Class objects
+ Class instance objects
+ Type objects (see module: types)
+ File objects (see above)
+ Slice objects
+ XRange objects
+ Callable types:
o User-defined (written in Python):
# User-defined Function objects
# User-defined Method objects
o Built-in (written in C):
# Built-in Function objects
# Built-in Method objects
+ Internal Types:
o Code objects (byte-compile executable Python code: bytecode)
o Frame objects (execution frames)
o Traceback objects (stack trace of an exception)
Statements
pass -- Null statement
del name[,name]* -- Unbind name(s) from object. Object will be indirectly
(and automatically) deleted only if no longer referenced.
print [>> fileobject,] [s1 [, s2 ]* [,]
-- Writes to sys.stdout, or to fileobject if supplied.
Puts spaces between arguments. Puts newline at end
unless statement ends with comma.
Print is not required when running interactively,
simply typing an expression will print its value,
unless the value is None.
exec x [in globals [,locals]]
-- Executes x in namespaces provided. Defaults
to current namespaces. x can be a string, file
object or a function object.
callable(value,... [id=value], [*args], [**kw])
-- Call function callable with parameters. Parameters can
be passed by name or be omitted if function
defines default values. E.g. if callable is defined as
"def callable(p1=1, p2=2)"
"callable()" <=> "callable(1, 2)"
"callable(10)" <=> "callable(10, 2)"
"callable(p2=99)" <=> "callable(1, 99)"
*args is a tuple of positional arguments.
**kw is a dictionary of keyword arguments.
Assignment operators
Caption
Operator Result Notes
a = b Basic assignment - assign object b to label a (1)
a += b Roughly equivalent to a = a + b (2)
a -= b Roughly equivalent to a = a - b (2)
a *= b Roughly equivalent to a = a * b (2)
a /= b Roughly equivalent to a = a / b (2)
a %= b Roughly equivalent to a = a % b (2)
a **= b Roughly equivalent to a = a ** b (2)
a &= b Roughly equivalent to a = a & b (2)
a |= b Roughly equivalent to a = a | b (2)
a ^= b Roughly equivalent to a = a ^ b (2)
a >>= b Roughly equivalent to a = a >> b (2)
a <<= b Roughly equivalent to a = a << b (2)
Notes :
(1) Can unpack tuples, lists, and strings.
first, second = a[0:2]; [f, s] = range(2); c1,c2,c3='abc'
Tip: x,y = y,x swaps x and y.
(2) Not exactly equivalent - a is evaluated only once. Also, where
possible, operation performed in-place - a is modified rather than
replaced.
Control Flow
if condition: suite
[elif condition: suite]*
[else: suite] -- usual if/else_if/else statement
while condition: suite
[else: suite]
-- usual while statement. "else" suite is executed
after loop exits, unless the loop is exited with
"break"
for element in sequence: suite
[else: suite]
-- iterates over sequence, assigning each element to element.
Use built-in range function to iterate a number of times.
"else" suite executed at end unless loop exited
with "break"
break -- immediately exits "for" or "while" loop
continue -- immediately does next iteration of "for" or "while" loop
return [result] -- Exits from function (or method) and returns result (use a tuple to
return more than one value). If no result given, then returns None.
yield result -- Freezes the execution frame of a generator and returns the result
to the iterator's .next() method. Upon the next call to next(),
resumes execution at the frozen point with all of the local variables
still intact.
Exception Statements
assert expr[, message]
-- expr is evaluated. if false, raises exception AssertionError
with message. Inhibited if __debug__ is 0.
try: suite1
[except [exception [, value]: suite2]+
[else: suite3]
-- statements in suite1 are executed. If an exception occurs, look
in "except" clauses for matching <exception>. If matches or bare
"except" execute suite of that clause. If no exception happens
suite in "else" clause is executed after suite1.
If exception has a value, it is put in value.
exception can also be tuple of exceptions, e.g.
"except (KeyError, NameError), val: print val"
try: suite1
finally: suite2
-- statements in suite1 are executed. If no
exception, execute suite2 (even if suite1 is
exited with a "return", "break" or "continue"
statement). If exception did occur, executes
suite2 and then immediately reraises exception.
raise exception [,value [, traceback]]
-- raises exception with optional value
value. Arg traceback specifies a traceback object to
use when printing the exception's backtrace.
raise -- a raise statement without arguments re-raises
the last exception raised in the current function
An exception is either a string (object) or a class instance.
Can create a new one simply by creating a new string:
my_exception = 'You did something wrong'
try:
if bad:
raise my_exception, bad
except my_exception, value:
print 'Oops', value
Exception classes must be derived from the predefined class: Exception, e.g.:
class text_exception(Exception): pass
try:
if bad:
raise text_exception()
# This is a shorthand for the form
# "raise <class>, <instance>"
except Exception:
print 'Oops'
# This will be printed because
# text_exception is a subclass of Exception
When an error message is printed for an unhandled exception which is a
class, the class name is printed, then a colon and a space, and
finally the instance converted to a string using the built-in function
str().
All built-in exception classes derives from StandardError, itself
derived from Exception.
Name Space Statements
[1.51: On Mac & Windows, the case of module file names must now match the case
as used
in the import statement]
Packages (>1.5): a package is a name space which maps to a directory including
module(s) and the special initialization module '__init__.py'
(possibly empty). Packages/dirs can be nested. You address a
module's symbol via '[package.[package...]module.symbol's.
import module1 [as name1] [, module2]*
-- imports modules. Members of module must be
referred to by qualifying with [package.]module name:
"import sys; print sys.argv:"
"import package1.subpackage.module; package1.subpackage.module.foo()"
module1 renamed as name1, if supplied.
from module import name1 [as othername1] [, name2]*
-- imports names from module module in current namespace.
"from sys import argv; print argv"
"from package1 import module; module.foo()"
"from package1.module import foo; foo()"
name1 renamed as othername1, if supplied.
from module import *
-- imports all names in module, except those starting with "_";
*to be used sparsely, beware of name clashes* :
"from sys import *; print argv"
"from package.module import *; print x'
NB: "from package import *" only imports the symbols defined
in the package's __init__.py file, not those in the
template modules!
global name1 [, name2]*
-- names are from global scope (usually meaning from module)
rather than local (usually meaning only in function).
-- E.g. in fct without "global" statements, assuming
"a" is name that hasn't been used in fct or module
so far:
-Try to read from "a" -> NameError
-Try to write to "a" -> creates "a" local to fcn
-If "a" not defined in fct, but is in module, then
-Try to read from "a", gets value from module
-Try to write to "a", creates "a" local to fct
But note "a[0]=3" starts with search for "a",
will use to global "a" if no local "a".
Function Definition
def func_id ([param_list]): suite
-- Creates a function object & binds it to name func_id.
param_list ::= [id [, id]*]
id ::= value | id = value | *id | **id
[Args are passed by value.Thus only args representing a mutable object
can be modified (are inout parameters). Use a tuple to return more than
one value]
Example:
def test (p1, p2 = 1+1, *rest, **keywords):
-- Parameters with "=" have default value (v is
evaluated when function defined).
If list has "*id" then id is assigned a tuple of
all remaining args passed to function (like C vararg)
If list has "**id" then id is assigned a dictionary of
all extra arguments passed as keywords.
Class Definition
class <class_id> [(<super_class1> [,<super_class2>]*)]: <suite>
-- Creates a class object and assigns it name <class_id>
<suite> may contain local "defs" of class methods and
assignments to class attributes.
Example:
class my_class (class1, class_list[3]): ...
Creates a class object inheriting from both "class1" and whatever
class object "class_list[3]" evaluates to. Assigns new
class object to name "my_class".
- First arg to class methods is always instance object, called 'self'
by convention.
- Special method __init__() is called when instance is created.
- Special method __del__() called when no more reference to object.
- Create instance by "calling" class object, possibly with arg
(thus instance=apply(aClassObject, args...) creates an instance!)
- In current implementation, can't subclass off built-in
classes. But can "wrap" them, see UserDict & UserList modules,
and see __getattr__() below.
Example:
class c (c_parent):
def __init__(self, name): self.name = name
def print_name(self): print "I'm", self.name
def call_parent(self): c_parent.print_name(self)
instance = c('tom')
print instance.name
'tom'
instance.print_name()
"I'm tom"
Call parent's super class by accessing parent's method
directly and passing "self" explicitly (see "call_parent"
in example above).
Many other special methods available for implementing
arithmetic operators, sequence, mapping indexing, etc.
Documentation Strings
Modules, classes and functions may be documented by placing a string literal by
itself as the first statement in the suite. The documentation can be retrieved
by getting the '__doc__' attribute from the module, class or function.
Example:
class C:
"A description of C"
def __init__(self):
"A description of the constructor"
# etc.
Then c.__doc__ == "A description of C".
Then c.__init__.__doc__ == "A description of the constructor".
Others
lambda [param_list]: returnedExpr
-- Creates an anonymous function. returnedExpr must be
an expression, not a statement (e.g., not "if xx:...",
"print xxx", etc.) and thus can't contain newlines.
Used mostly for filter(), map(), reduce() functions, and GUI callbacks..
List comprehensions
result = [expression for item1 in sequence1 [if condition1]
[for item2 in sequence2 ... for itemN in sequenceN]
]
is equivalent to:
result = []
for item1 in sequence1:
for item2 in sequence2:
...
for itemN in sequenceN:
if (condition1) and furthur conditions:
result.append(expression)
Built-In Functions
Built-In Functions
Function Result
__import__(name[, Imports module within the given context (see lib ref for
globals[, locals[, more details)
fromlist]]])
abs(x) Return the absolute value of number x.
apply(f, args[, Calls func/method f with arguments args and optional
keywords]) keywords.
bool(x) Returns True when the argument x is true and False otherwise.
buffer(obj) Creates a buffer reference to an object.
callable(x) Returns True if x callable, else False.
chr(i) Returns one-character string whose ASCII code isinteger i
classmethod(f) Converts a function f, into a method with the class as the
first argument. Useful for creating alternative constructors.
cmp(x,y) Returns negative, 0, positive if x <, ==, > to y
coerce(x,y) Returns a tuple of the two numeric arguments converted to a
common type.
Compiles string into a code object.filename is used in
error message, can be any string. It isusually the file
compile(string, from which the code was read, or eg. '<string>'if not read
filename, kind) from file.kind can be 'eval' if string is a single stmt, or
'single' which prints the output of expression statements
thatevaluate to something else than None, or be 'exec'.
complex(real[, Builds a complex object (can also be done using J or j
image]) suffix,e.g. 1+3J)
delattr(obj, name) deletes attribute named name of object obj <=> del obj.name
If no args, returns the list of names in current
dict([items]) Create a new dictionary from the specified item list.
dir([object]) localsymbol table. With a module, class or class
instanceobject as arg, returns list of names in its attr.
dict.
divmod(a,b) Returns tuple of (a/b, a%b)
enumerate(seq) Return a iterator giving: (0, seq[0]), (1, seq[1]), ...
eval(s[, globals[, Eval string s in (optional) globals, locals contexts.s must
locals]]) have no NUL's or newlines. s can also be acode object.
Example: x = 1; incr_x = eval('x + 1')
execfile(file[, Executes a file without creating a new module, unlike
globals[, locals]]) import.
file() Synonym for open().
filter(function, Constructs a list from those elements of sequence for which
sequence) function returns true. function takes one parameter.
float(x) Converts a number or a string to floating point.
getattr(object, [<default> arg added in 1.5.2]Gets attribute called name
name[, default])) from object,e.g. getattr(x, 'f') <=> x.f). If not found,
raisesAttributeError or returns default if specified.
globals() Returns a dictionary containing current global variables.
hasattr(object, Returns true if object has attr called name.
name)
hash(object) Returns the hash value of the object (if it has one)
help(f) Display documentation on object f.
hex(x) Converts a number x to a hexadecimal string.
id(object) Returns a unique 'identity' integer for an object.
input([prompt]) Prints prompt if given. Reads input and evaluates it.
Converts a number or a string to a plain integer. Optional
int(x[, base]) base paramenter specifies base from which to convert string
values.
intern(aString) Enters aString in the table of "interned strings"
andreturns the string. Interned strings are 'immortals'.
isinstance(obj, returns true if obj is an instance of class. Ifissubclass
class) (A,B) then isinstance(x,A) => isinstance(x,B)
issubclass(class1, returns true if class1 is derived from class2
class2)
Returns the length (the number of items) of an object
iter(collection) Returns an iterator over the collection.
len(obj) (sequence, dictionary, or instance of class implementing
__len__).
list(sequence) Converts sequence into a list. If already a list,returns a
copy of it.
locals() Returns a dictionary containing current local variables.
Converts a number or a string to a long integer. Optional
long(x[, base]) base paramenter specifies base from which to convert string
values.
Applies function to every item of list and returns a listof
map(function, list, the results. If additional arguments are passed,function
...) must take that many arguments and it is givento function on
each call.
max(seq) Returns the largest item of the non-empty sequence seq.
min(seq) Returns the smallest item of a non-empty sequence seq.
oct(x) Converts a number to an octal string.
open(filename [, Returns a new file object. First two args are same asthose
mode='r', [bufsize= for C's "stdio open" function. bufsize is 0for unbuffered,
implementation 1 for line-buffered, negative forsys-default, all else, of
dependent]]) (about) given size.
ord(c) Returns integer ASCII value of c (a string of len 1). Works
with Unicode char.
object() Create a base type. Used as a superclass for new-style objects.
open(name Open a file.
[, mode
[, buffering]])
pow(x, y [, z]) Returns x to power y [modulo z]. See also ** operator.
property() Created a property with access controlled by functions.
range(start [,end Returns list of ints from >= start and < end.With 1 arg,
[, step]]) list from 0..arg-1With 2 args, list from start..end-1With 3
args, list from start up to end by step
raw_input([prompt]) Prints prompt if given, then reads string from stdinput (no
trailing \n). See also input().
reduce(f, list [, Applies the binary function f to the items oflist so as to
init]) reduce the list to a single value.If init given, it is
"prepended" to list.
Re-parses and re-initializes an already imported module.
Useful in interactive mode, if you want to reload amodule
reload(module) after fixing it. If module was syntacticallycorrect but had
an error in initialization, mustimport it one more time
before calling reload().
Returns a string containing a printable and if possible
repr(object) evaluable representation of an object. <=> `object`
(usingbackquotes). Class redefinissable (__repr__). See
also str()
round(x, n=0) Returns the floating point value x rounded to n digitsafter
the decimal point.
setattr(object, This is the counterpart of getattr().setattr(o, 'foobar',
name, value) 3) <=> o.foobar = 3Creates attribute if it doesn't exist!
slice([start,] stop Returns a slice object representing a range, with R/
[, step]) Oattributes: start, stop, step.
Returns a string containing a nicely
staticmethod() Convert a function to method with no self or class
argument. Useful for methods associated with a class that
do not need access to an object's internal state.
str(object) printablerepresentation of an object. Class overridable
(__str__).See also repr().
super(type) Create an unbound super object. Used to call cooperative
superclass methods.
sum(sequence, Add the values in the sequence and return the sum.
[start])
tuple(sequence) Creates a tuple with same elements as sequence. If already
a tuple, return itself (not a copy).
Returns a type object [see module types] representing
thetype of obj. Example: import typesif type(x) ==
type(obj) types.StringType: print 'It is a string'NB: it is
recommanded to use the following form:if isinstance(x,
types.StringType): etc...
unichr(code) code.
unicode(string[, Creates a Unicode string from a 8-bit string, using
encoding[, error thegiven encoding name and error treatment ('strict',
]]]) 'ignore',or 'replace'}.
Without arguments, returns a dictionary correspondingto the
current local symbol table. With a module,class or class
vars([object]) instance object as argumentreturns a dictionary
corresponding to the object'ssymbol table. Useful with "%"
formatting operator.
xrange(start [, end Like range(), but doesn't actually store entire listall at
[, step]]) once. Good to use in "for" loops when there is abig range
and little memory.
zip(seq1[, seq2, Returns a list of tuples where each tuple contains the nth
...]) element of each of the argument sequences.
Built-In Exceptions
Exception>
Root class for all exceptions
SystemExit
On 'sys.exit()'
StopIteration
Signal the end from iterator.next()
StandardError
Base class for all built-in exceptions; derived from Exception
root class.
ArithmeticError
Base class for OverflowError, ZeroDivisionError,
FloatingPointError
FloatingPointError
When a floating point operation fails.
OverflowError
On excessively large arithmetic operation
ZeroDivisionError
On division or modulo operation with 0 as 2nd arg
AssertionError
When an assert statement fails.
AttributeError
On attribute reference or assignment failure
EnvironmentError [new in 1.5.2]
On error outside Python; error arg tuple is (errno, errMsg...)
IOError [changed in 1.5.2]
I/O-related operation failure
OSError [new in 1.5.2]
used by the os module's os.error exception.
EOFError
Immediate end-of-file hit by input() or raw_input()
ImportError
On failure of `import' to find module or name
KeyboardInterrupt
On user entry of the interrupt key (often `Control-C')
LookupError
base class for IndexError, KeyError
IndexError
On out-of-range sequence subscript
KeyError
On reference to a non-existent mapping (dict) key
MemoryError
On recoverable memory exhaustion
NameError
On failure to find a local or global (unqualified) name
RuntimeError
Obsolete catch-all; define a suitable error instead
NotImplementedError [new in 1.5.2]
On method not implemented
SyntaxError
On parser encountering a syntax error
IndentationError
On parser encountering an indentation syntax error
TabError
On parser encountering an indentation syntax error
SystemError
On non-fatal interpreter error - bug - report it
TypeError
On passing inappropriate type to built-in op or func
ValueError
On arg error not covered by TypeError or more precise
Warning
UserWarning
DeprecationWarning
PendingDeprecationWarning
SyntaxWarning
RuntimeWarning
FutureWarning
Standard methods & operators redefinition in classes
Standard methods & operators map to special '__methods__' and thus may be
redefined (mostly in in user-defined classes), e.g.:
class x:
def __init__(self, v): self.value = v
def __add__(self, r): return self.value + r
a = x(3) # sort of like calling x.__init__(a, 3)
a + 4 # is equivalent to a.__add__(4)
Special methods for any class
(s: self, o: other)
__init__(s, args) instance initialization (on construction)
__del__(s) called on object demise (refcount becomes 0)
__repr__(s) repr() and `...` conversions
__str__(s) str() and 'print' statement
__cmp__(s, o) Compares s to o and returns <0, 0, or >0.
Implements >, <, == etc...
__hash__(s) Compute a 32 bit hash code; hash() and dictionary ops
__nonzero__(s) Returns False or True for truth value testing
__getattr__(s, name) called when attr lookup doesn't find <name>
__setattr__(s, name, val) called when setting an attr
(inside, don't use "self.name = value"
use "self.__dict__[name] = val")
__delattr__(s, name) called to delete attr <name>
__call__(self, *args) called when an instance is called as function.
Operators
See list in the operator module. Operator function names are provided with
2 variants, with or without
ading & trailing '__' (eg. __add__ or add).
Numeric operations special methods
(s: self, o: other)
s+o = __add__(s,o) s-o = __sub__(s,o)
s*o = __mul__(s,o) s/o = __div__(s,o)
s%o = __mod__(s,o) divmod(s,o) = __divmod__(s,o)
s**o = __pow__(s,o)
s&o = __and__(s,o)
s^o = __xor__(s,o) s|o = __or__(s,o)
s<<o = __lshift__(s,o) s>>o = __rshift__(s,o)
nonzero(s) = __nonzero__(s) (used in boolean testing)
-s = __neg__(s) +s = __pos__(s)
abs(s) = __abs__(s) ~s = __invert__(s) (bitwise)
s+=o = __iadd__(s,o) s-=o = __isub__(s,o)
s*=o = __imul__(s,o) s/=o = __idiv__(s,o)
s%=o = __imod__(s,o)
s**=o = __ipow__(s,o)
s&=o = __iand__(s,o)
s^=o = __ixor__(s,o) s|=o = __ior__(s,o)
s<<=o = __ilshift__(s,o) s>>=o = __irshift__(s,o)
Conversions
int(s) = __int__(s) long(s) = __long__(s)
float(s) = __float__(s) complex(s) = __complex__(s)
oct(s) = __oct__(s) hex(s) = __hex__(s)
coerce(s,o) = __coerce__(s,o)
Right-hand-side equivalents for all binary operators exist;
are called when class instance is on r-h-s of operator:
a + 3 calls __add__(a, 3)
3 + a calls __radd__(a, 3)
All seqs and maps, general operations plus:
(s: self, i: index or key)
len(s) = __len__(s) length of object, >= 0. Length 0 == false
s[i] = __getitem__(s,i) Element at index/key i, origin 0
Sequences, general methods, plus:
s[i]=v = __setitem__(s,i,v)
del s[i] = __delitem__(s,i)
s[i:j] = __getslice__(s,i,j)
s[i:j]=seq = __setslice__(s,i,j,seq)
del s[i:j] = __delslice__(s,i,j) == s[i:j] = []
seq * n = __repeat__(seq, n)
s1 + s2 = __concat__(s1, s2)
i in s = __contains__(s, i)
Mappings, general methods, plus
hash(s) = __hash__(s) - hash value for dictionary references
s[k]=v = __setitem__(s,k,v)
del s[k] = __delitem__(s,k)
Special informative state attributes for some types:
Modules:
__doc__ (string/None, R/O): doc string (<=> __dict__['__doc__'])
__name__(string, R/O): module name (also in __dict__['__name__'])
__dict__ (dict, R/O): module's name space
__file__(string/undefined, R/O): pathname of .pyc, .pyo or .pyd (undef for
modules statically linked to the interpreter)
Classes: [in bold: writable since 1.5.2]
__doc__ (string/None, R/W): doc string (<=> __dict__['__doc__'])
__module__ is the module name in which the class was defined
__name__(string, R/W): class name (also in __dict__['__name__'])
__bases__ (tuple, R/W): parent classes
__dict__ (dict, R/W): attributes (class name space)
Instances:
__class__ (class, R/W): instance's class
__dict__ (dict, R/W): attributes
User-defined functions: [bold: writable since 1.5.2]
__doc__ (string/None, R/W): doc string
__name__(string, R/O): function name
func_doc (R/W): same as __doc__
func_name (R/O): same as __name__
func_defaults (tuple/None, R/W): default args values if any
func_code (code, R/W): code object representing the compiled function body
func_globals (dict, R/O): ref to dictionary of func global variables
func_dict (dict, R/W): same as __dict__ contains the namespace supporting
arbitrary function attributes
func_closure (R/O): None or a tuple of cells that contain bindings
for the function's free variables.
User-defined Methods:
__doc__ (string/None, R/O): doc string
__name__(string, R/O): method name (same as im_func.__name__)
im_class (class, R/O): class defining the method (may be a base class)
im_self (instance/None, R/O): target instance object (None if unbound)
im_func (function, R/O): function object
Built-in Functions & methods:
__doc__ (string/None, R/O): doc string
__name__ (string, R/O): function name
__self__ : [methods only] target object
Codes:
co_name (string, R/O): function name
co_argcount (int, R/0): number of positional args
co_nlocals (int, R/O): number of local vars (including args)
co_varnames (tuple, R/O): names of local vars (starting with args)
co_cellvars (tuple, R/O)) the names of local variables referenced by
nested functions
co_freevars (tuple, R/O)) names of free variables
co_code (string, R/O): sequence of bytecode instructions
co_consts (tuple, R/O): litterals used by the bytecode, 1st one is
fct doc (or None)
co_names (tuple, R/O): names used by the bytecode
co_filename (string, R/O): filename from which the code was compiled
co_firstlineno (int, R/O): first line number of the function
co_lnotab (string, R/O): string encoding bytecode offsets to line numbers.
co_stacksize (int, R/O): required stack size (including local vars)
co_flags (int, R/O): flags for the interpreter
bit 2 set if fct uses "*arg" syntax
bit 3 set if fct uses '**keywords' syntax
Frames:
f_back (frame/None, R/O): previous stack frame (toward the caller)
f_code (code, R/O): code object being executed in this frame
f_locals (dict, R/O): local vars
f_globals (dict, R/O): global vars
f_builtins (dict, R/O): built-in (intrinsic) names
f_restricted (int, R/O): flag indicating whether fct is executed in
restricted mode
f_lineno (int, R/O): current line number
f_lasti (int, R/O): precise instruction (index into bytecode)
f_trace (function/None, R/W): debug hook called at start of each source line
f_exc_type (Type/None, R/W): Most recent exception type
f_exc_value (any, R/W): Most recent exception value
f_exc_traceback (traceback/None, R/W): Most recent exception traceback
Tracebacks:
tb_next (frame/None, R/O): next level in stack trace (toward the frame where
the exception occurred)
tb_frame (frame, R/O): execution frame of the current level
tb_lineno (int, R/O): line number where the exception occurred
tb_lasti (int, R/O): precise instruction (index into bytecode)
Slices:
start (any/None, R/O): lowerbound
stop (any/None, R/O): upperbound
step (any/None, R/O): step value
Complex numbers:
real (float, R/O): real part
imag (float, R/O): imaginary part
Important Modules
sys
Some sys variables
Variable Content
argv The list of command line arguments passed to aPython
script. sys.argv[0] is the script name.
builtin_module_names A list of strings giving the names of all moduleswritten
in C that are linked into this interpreter.
check_interval How often to check for thread switches or signals(measured
in number of virtual machine instructions)
exc_type, exc_value, Deprecated since release 1.5. Use exc_info() instead.
exc_traceback
exitfunc User can set to a parameterless fcn. It will getcalled
before interpreter exits.
last_type, Set only when an exception not handled andinterpreter
last_value, prints an error. Used by debuggers.
last_traceback
maxint maximum positive value for integers
modules Dictionary of modules that have already been loaded.
path Search path for external modules. Can be modifiedby
program. sys.path[0] == dir of script executing
platform The current platform, e.g. "sunos5", "win32"
ps1, ps2 prompts to use in interactive mode.
File objects used for I/O. One can redirect byassigning a
stdin, stdout, new file object to them (or any object:.with a method
stderr write(string) for stdout/stderr,.with a method readline()
for stdin)
version string containing version info about Python interpreter.
(and also: copyright, dllhandle, exec_prefix, prefix)
version_info tuple containing Python version info - (major, minor,
micro, level, serial).
Some sys functions
Function Result
exit(n) Exits with status n. Raises SystemExit exception.(Hence can
be caught and ignored by program)
getrefcount(object Returns the reference count of the object. Generally one
) higher than you might expect, because of object arg temp
reference.
setcheckinterval( Sets the interpreter's thread switching interval (in number
interval) of virtual code instructions, default:100).
settrace(func) Sets a trace function: called before each line ofcode is
exited.
setprofile(func) Sets a profile function for performance profiling.
Info on exception currently being handled; this is atuple
(exc_type, exc_value, exc_traceback).Warning: assigning the
exc_info() traceback return value to a loca variable in a
function handling an exception will cause a circular
reference.
setdefaultencoding Change default Unicode encoding - defaults to 7-bit ASCII.
(encoding)
getrecursionlimit Retrieve maximum recursion depth.
()
setrecursionlimit Set maximum recursion depth. (Defaults to 1000.)
()
os
"synonym" for whatever O/S-specific module is proper for current environment.
this module uses posix whenever possible.
(see also M.A. Lemburg's utility http://www.lemburg.com/files/python/
platform.py)
Some os variables
Variable Meaning
name name of O/S-specific module (e.g. "posix", "mac", "nt")
path O/S-specific module for path manipulations.
On Unix, os.path.split() <=> posixpath.split()
curdir string used to represent current directory ('.')
pardir string used to represent parent directory ('..')
sep string used to separate directories ('/' or '\'). Tip: use
os.path.join() to build portable paths.
altsep Alternate sep
if applicable (None
otherwise)
pathsep character used to separate search path components (as in
$PATH), eg. ';' for windows.
linesep line separator as used in binary files, ie '\n' on Unix, '\
r\n' on Dos/Win, '\r'
Some os functions
Function Result
makedirs(path[, Recursive directory creation (create required intermediary
mode=0777]) dirs); os.error if fails.
removedirs(path) Recursive directory delete (delete intermediary empty
dirs); if fails.
renames(old, new) Recursive directory or file renaming; os.error if fails.
posix
don't import this module directly, import os instead !
(see also module: shutil for file copy & remove fcts)
posix Variables
Variable Meaning
environ dictionary of environment variables, e.g.posix.environ['HOME'].
error exception raised on POSIX-related error.
Corresponding value is tuple of errno code and perror() string.
Some posix functions
Function Result
chdir(path) Changes current directory to path.
chmod(path, Changes the mode of path to the numeric mode
mode)
close(fd) Closes file descriptor fd opened with posix.open.
_exit(n) Immediate exit, with no cleanups, no SystemExit,etc. Should use
this to exit a child process.
execv(p, args) "Become" executable p with args args
getcwd() Returns a string representing the current working directory
getpid() Returns the current process id
fork() Like C's fork(). Returns 0 to child, child pid to parent.[Not
on Windows]
kill(pid, Like C's kill [Not on Windows]
signal)
listdir(path) Lists (base)names of entries in directory path, excluding '.'
and '..'
lseek(fd, pos, Sets current position in file fd to position pos, expressedas
how) an offset relative to beginning of file (how=0), tocurrent
position (how=1), or to end of file (how=2)
mkdir(path[, Creates a directory named path with numeric mode (default 0777)
mode])
open(file, Like C's open(). Returns file descriptor. Use file object
flags, mode) fctsrather than this low level ones.
pipe() Creates a pipe. Returns pair of file descriptors (r, w) [Not on
Windows].
popen(command, Opens a pipe to or from command. Result is a file object to
mode='r', read to orwrite from, as indicated by mode being 'r' or 'w'.
bufSize=0) Use it to catch acommand output ('r' mode) or to feed it ('w'
mode).
remove(path) See unlink.
rename(src, dst Renames/moves the file or directory src to dst. [error iftarget
) name already exists]
rmdir(path) Removes the empty directory path
read(fd, n) Reads n bytes from file descriptor fd and return as string.
Returns st_mode, st_ino, st_dev, st_nlink, st_uid,st_gid,
stat(path) st_size, st_atime, st_mtime, st_ctime.[st_ino, st_uid, st_gid
are dummy on Windows]
system(command) Executes string command in a subshell. Returns exitstatus of
subshell (usually 0 means OK).
Returns accumulated CPU times in sec (user, system, children's
times() user,children's sys, elapsed real time). [3 last not on
Windows]
unlink(path) Unlinks ("deletes") the file (not dir!) path. same as: remove
utime(path, ( Sets the access & modified time of the file to the given tuple
aTime, mTime)) of values.
wait() Waits for child process completion. Returns tuple ofpid,
exit_status [Not on Windows]
waitpid(pid, Waits for process pid to complete. Returns tuple ofpid,
options) exit_status [Not on Windows]
write(fd, str) Writes str to file fd. Returns nb of bytes written.
posixpath
Do not import this module directly, import os instead and refer to this module
as os.path. (e.g. os.path.exists(p)) !
Some posixpath functions
Function Result
abspath(p) Returns absolute path for path p, taking current working dir in
account.
dirname/
basename(p directory and name parts of the path p. See also split.
)
exists(p) True if string p is an existing path (file or directory)
expanduser Returns string that is (a copy of) p with "~" expansion done.
(p)
expandvars Returns string that is (a copy of) p with environment vars expanded.
(p) [Windows: case significant; must use Unix: $var notation, not %var%]
getsize( return the size in bytes of filename. raise os.error.
filename)
getmtime( return last modification time of filename (integer nb of seconds
filename) since epoch).
getatime( return last access time of filename (integer nb of seconds since
filename) epoch).
isabs(p) True if string p is an absolute path.
isdir(p) True if string p is a directory.
islink(p) True if string p is a symbolic link.
ismount(p) True if string p is a mount point [true for all dirs on Windows].
join(p[,q Joins one or more path components intelligently.
[,...]])
Splits p into (head, tail) where tail is lastpathname component and
split(p) <head> is everything leadingup to that. <=> (dirname(p), basename
(p))
splitdrive Splits path p in a pair ('drive:', tail) [Windows]
(p)
splitext(p Splits into (root, ext) where last comp of root contains no periods
) and ext is empty or startswith a period.
Calls the function visit with arguments(arg, dirname, names) for
each directory recursively inthe directory tree rooted at p
walk(p, (including p itself if it's a dir)The argument dirname specifies the
visit, arg visited directory, the argumentnames lists the files in the
) directory. The visit function maymodify names to influence the set
of directories visited belowdirname, e.g., to avoid visiting certain
parts of the tree.
shutil
high-level file operations (copying, deleting).
Main shutil functions
Function Result
copy(src, dst) Copies the contents of file src to file dst, retaining file
permissions.
copytree(src, dst Recursively copies an entire directory tree rooted at src
[, symlinks]) into dst (which should not already exist). If symlinks is
true, links insrc are kept as such in dst.
rmtree(path[, Deletes an entire directory tree, ignoring errors if
ignore_errors[, ignore_errors true,or calling onerror(func, path,
onerror]]) sys.exc_info()) if supplied with
(and also: copyfile, copymode, copystat, copy2)
time
Variables
Variable Meaning
altzone signed offset of local DST timezone in sec west of the 0th meridian.
daylight nonzero if a DST timezone is specified
Functions
Function Result
time() return a float representing UTC time in seconds since the epoch.
gmtime(secs), return a tuple representing time : (year aaaa, month(1-12),day
localtime( (1-31), hour(0-23), minute(0-59), second(0-59), weekday(0-6, 0 is
secs) monday), Julian day(1-366), daylight flag(-1,0 or 1))
asctime(
timeTuple),
strftime(
format, return a formated string representing time.
timeTuple)
mktime(tuple) inverse of localtime(). Return a float.
strptime( parse a formated string representing time, return tuple as in
string[, gmtime().
format])
sleep(secs) Suspend execution for <secs> seconds. <secs> can be a float.
and also: clock, ctime.
string
As of Python 2.0, much (though not all) of the functionality provided by the
string module have been superseded by built-in string methods - see Operations
on strings for details.
Some string variables
Variable Meaning
digits The string '0123456789'
hexdigits, octdigits legal hexadecimal & octal digits
letters, uppercase, lowercase, Strings containing the appropriate
whitespace characters
index_error Exception raised by index() if substr not
found.
Some string functions
Function Result
expandtabs(s, returns a copy of string <s> with tabs expanded.
tabSize)
find/rfind(s, sub Return the lowest/highest index in <s> where the substring
[, start=0[, end= <sub> is found such that <sub> is wholly contained ins
0]) [start:end]. Return -1 if <sub> not found.
ljust/rjust/center Return a copy of string <s> left/right justified/centerd in
(s, width) afield of given width, padded with spaces. <s> is
nevertruncated.
lower/upper(s) Return a string that is (a copy of) <s> in lowercase/
uppercase
split(s[, sep= Return a list containing the words of the string <s>,using
whitespace[, the string <sep> as a separator.
maxsplit=0]])
join(words[, sep=' Concatenate a list or tuple of words with
']) interveningseparators; inverse of split.
replace(s, old, Returns a copy of string <s> with all occurrences of
new[, maxsplit=0] substring<old> replaced by <new>. Limits to <maxsplit>
firstsubstitutions if specified.
strip(s) Return a string that is (a copy of) <s> without leadingand
trailing whitespace. see also lstrip, rstrip.
re (sre)
Handles Unicode strings. Implemented in new module sre, re now a mere front-end
for compatibility.
Patterns are specified as strings. Tip: Use raw strings (e.g. r'\w*') to
litteralize backslashes.
Regular expression syntax
Form Description
. matches any character (including newline if DOTALL flag specified)
^ matches start of the string (of every line in MULTILINE mode)
$ matches end of the string (of every line in MULTILINE mode)
* 0 or more of preceding regular expression (as many as possible)
+ 1 or more of preceding regular expression (as many as possible)
? 0 or 1 occurrence of preceding regular expression
*?, +?, ?? Same as *, + and ? but matches as few characters as possible
{m,n} matches from m to n repetitions of preceding RE
{m,n}? idem, attempting to match as few repetitions as possible
[ ] defines character set: e.g. '[a-zA-Z]' to match all letters(see also
\w \S)
[^ ] defines complemented character set: matches if char is NOT in set
escapes special chars '*?+&$|()' and introduces special sequences
\ (see below). Due to Python string rules, write as '\\' orr'\' in the
pattern string.
\\ matches a litteral '\'; due to Python string rules, write as '\\\\
'in pattern string, or better using raw string: r'\\'.
| specifies alternative: 'foo|bar' matches 'foo' or 'bar'
(...) matches any RE inside (), and delimits a group.
(?:...) idem but doesn't delimit a group.
matches if ... matches next, but doesn't consume any of the string
(?=...) e.g. 'Isaac (?=Asimov)' matches 'Isaac' only if followed by
'Asimov'.
(?!...) matches if ... doesn't match next. Negative of (?=...)
(?P<name matches any RE inside (), and delimits a named group. (e.g. r'(?P
>...) <id>[a-zA-Z_]\w*)' defines a group named id)
(?P=name) matches whatever text was matched by the earlier group named name.
(?#...) A comment; ignored.
(?letter) letter is one of 'i','L', 'm', 's', 'x'. Set the corresponding flags
(re.I, re.L, re.M, re.S, re.X) for the entire RE.
Special sequences
Sequence Description
number matches content of the group of the same number; groups are numbered
starting from 1
\A matches only at the start of the string
\b empty str at beg or end of word: '\bis\b' matches 'is', but not 'his'
\B empty str NOT at beginning or end of word
\d any decimal digit (<=> [0-9])
\D any non-decimal digit char (<=> [^O-9])
\s any whitespace char (<=> [ \t\n\r\f\v])
\S any non-whitespace char (<=> [^ \t\n\r\f\v])
\w any alphaNumeric char (depends on LOCALE flag)
\W any non-alphaNumeric char (depends on LOCALE flag)
\Z matches only at the end of the string
Variables
Variable Meaning
error Exception when pattern string isn't a valid regexp.
Functions
Function Result
Compile a RE pattern string into a regular expression object.
Flags (combinable by |):
I or IGNORECASE or (?i)
case insensitive matching
compile( L or LOCALE or (?L)
pattern[, make \w, \W, \b, \B dependent on thecurrent locale
flags=0]) M or MULTILINE or (?m)
matches every new line and not onlystart/end of the whole
string
S or DOTALL or (?s)
'.' matches ALL chars, including newline
X or VERBOSE or (?x)
Ignores whitespace outside character sets
escape(string) return (a copy of) string with all non-alphanumerics
backslashed.
match(pattern, if 0 or more chars at beginning of <string> match the RE pattern
string[, flags string,return a corresponding MatchObject instance, or None if
]) no match.
search(pattern scan thru <string> for a location matching <pattern>, return
, string[, acorresponding MatchObject instance, or None if no match.
flags])
split(pattern, split <string> by occurrences of <pattern>. If capturing () are
string[, used inpattern, then occurrences of patterns or subpatterns are
maxsplit=0]) also returned.
findall( return a list of non-overlapping matches in <pattern>, either a
pattern, list ofgroups or a list of tuples if the pattern has more than 1
string) group.
return string obtained by replacing the (<count> first) lefmost
sub(pattern, non-overlapping occurrences of <pattern> (a string or a RE
repl, string[, object) in <string>by <repl>; <repl> can be a string or a fct
count=0]) called with a single MatchObj arg, which must return the
replacement string.
subn(pattern,
repl, string[, same as sub(), but returns a tuple (newString, numberOfSubsMade)
count=0])
Regular Expression Objects
(RE objects are returned by the compile fct)
re object attributes
Attribute Descrition
flags flags arg used when RE obj was compiled, or 0 if none provided
groupindex dictionary of {group name: group number} in pattern
pattern pattern string from which RE obj was compiled
re object methods
Method Result
If zero or more characters at the beginning of string match this
regular expression, return a corresponding MatchObject instance.
Return None if the string does not match the pattern; note that
this is different from a zero-length match.
The optional second parameter pos gives an index in the string
match( where the search is to start; it defaults to 0. This is not
string[, completely equivalent to slicing the string; the '' pattern
pos][, character matches at the real beginning of the string and at
endpos]) positions just after a newline, but not necessarily at the index
where the search is to start.
The optional parameter endpos limits how far the string will be
searched; it will be as if the string is endpos characters long, so
only the characters from pos to endpos will be searched for a
match.
Scan through string looking for a location where this regular
search( expression produces a match, and return a corresponding MatchObject
string[, instance. Return None if no position in the string matches the
pos][, pattern; note that this is different from finding a zero-length
endpos]) match at some point in the string.
The optional pos and endpos parameters have the same meaning as for
the match() method.
split(
string[, Identical to the split() function, using the compiled pattern.
maxsplit=
0])
findall( Identical to the findall() function, using the compiled pattern.
string)
sub(repl,
string[, Identical to the sub() function, using the compiled pattern.
count=0])
subn(repl,
string[, Identical to the subn() function, using the compiled pattern.
count=0])
Match Objects
(Match objects are returned by the match & search functions)
Match object attributes
Attribute Description
pos value of pos passed to search or match functions; index intostring at
which RE engine started search.
endpos value of endpos passed to search or match functions; index intostring
beyond which RE engine won't go.
re RE object whose match or search fct produced this MatchObj instance
string string passed to match() or search()
Match object functions
Function Result
returns one or more groups of the match. If one arg, result is a
group([g1 string;if multiple args, result is a tuple with one item per arg. If
, g2, gi is 0,return value is entire matching string; if 1 <= gi <= 99,
...]) returnstring matching group #gi (or None if no such group); gi may
also bea group name.
returns a tuple of all groups of the match; groups not
groups() participatingto the match have a value of None. Returns a string
instead of tupleif len(tuple)=1
start(
group), returns indices of start & end of substring matched by group (or
end(group Noneif group exists but doesn't contribute to the match)
)
span( returns the 2-tuple (start(group), end(group)); can be (None, None)if
group) group didn't contibute to the match.
math
Variables:
pi
e
Functions (see ordinary C man pages for info):
acos(x)
asin(x)
atan(x)
atan2(x, y)
ceil(x)
cos(x)
cosh(x)
degrees(x)
exp(x)
fabs(x)
floor(x)
fmod(x, y)
frexp(x) -- Unlike C: (float, int) = frexp(float)
ldexp(x, y)
log(x [,base])
log10(x)
modf(x) -- Unlike C: (float, float) = modf(float)
pow(x, y)
radians(x)
sin(x)
sinh(x)
sqrt(x)
tan(x)
tanh(x)
getopt
Functions:
getopt(list, optstr) -- Similar to C. <optstr> is option
letters to look for. Put ':' after letter
if option takes arg. E.g.
# invocation was "python test.py -c hi -a arg1 arg2"
opts, args = getopt.getopt(sys.argv[1:], 'ab:c:')
# opts would be
[('-c', 'hi'), ('-a', '')]
# args would be
['arg1', 'arg2']
List of modules and packages in base distribution
(built-ins and content of python Lib directory)
(Python NT distribution, may be slightly different in other distributions)
Standard library modules
Operation Result
aifc Stuff to parse AIFF-C and AIFF files.
anydbm Generic interface to all dbm clones. (dbhash, gdbm,
dbm,dumbdbm)
asynchat Support for 'chat' style protocols
asyncore Asynchronous File I/O (in select style)
atexit Register functions to be called at exit of Python interpreter.
audiodev Audio support for a few platforms.
base64 Conversions to/from base64 RFC-MIME transport encoding .
BaseHTTPServer Base class forhttp services.
Bastion "Bastionification" utility (control access to instance vars)
bdb A generic Python debugger base class.
binhex Macintosh binhex compression/decompression.
bisect List bisection algorithms.
bz2 Support for bz2 compression/decompression.
calendar Calendar printing functions.
cgi Wraps the WWW Forms Common Gateway Interface (CGI).
cgitb Utility for handling CGI tracebacks.
CGIHTTPServer CGI http services.
cmd A generic class to build line-oriented command interpreters.
datetime Basic date and time types.
code Utilities needed to emulate Python's interactive interpreter
codecs Lookup existing Unicode encodings and register new ones.
colorsys Conversion functions between RGB and other color systems.
commands Tools for executing UNIX commands .
compileall Force "compilation" of all .py files in a directory.
ConfigParser Configuration file parser (much like windows .ini files)
copy Generic shallow and deep copying operations.
copy_reg Helper to provide extensibility for pickle/cPickle.
csv Read and write files with comma separated values.
dbhash (g)dbm-compatible interface to bsdhash.hashopen.
dircache Sorted list of files in a dir, using a cache.
[DEL:dircmp:DEL] [DEL:Defines a class to build directory diff tools on.:DEL]
difflib Tool for creating delta between sequences.
dis Bytecode disassembler.
distutils Package installation system.
doctest Tool for running and verifying tests inside doc strings.
dospath Common operations on DOS pathnames.
dumbdbm A dumb and slow but simple dbm clone.
[DEL:dump:DEL] [DEL:Print python code that reconstructs a variable.:DEL]
email Comprehensive support for internet email.
exceptions Class based built-in exception hierarchy.
filecmp File comparison.
fileinput Helper class to quickly write a loop over all standard input
files.
[DEL:find:DEL] [DEL:Find files directory hierarchy matching a pattern.:DEL]
fnmatch Filename matching with shell patterns.
formatter A test formatter.
fpformat General floating point formatting functions.
ftplib An FTP client class. Based on RFC 959.
gc Perform garbacge collection, obtain GC debug stats, and tune
GC parameters.
getopt Standard command line processing. See also ftp://
www.pauahtun.org/pub/getargspy.zip
getpass Utilities to get a password and/or the current user name.
glob filename globbing.
gopherlib Gopher protocol client interface.
[DEL:grep:DEL] [DEL:'grep' utilities.:DEL]
gzip Read & write gzipped files.
heapq Priority queue implemented using lists organized as heaps.
HMAC Keyed-Hashing for Message Authentication -- RFC 2104.
htmlentitydefs Proposed entity definitions for HTML.
htmllib HTML parsing utilities.
HTMLParser A parser for HTML and XHTML.
httplib HTTP client class.
ihooks Hooks into the "import" mechanism.
imaplib IMAP4 client.Based on RFC 2060.
imghdr Recognizing image files based on their first few bytes.
imputil Privides a way of writing customised import hooks.
inspect Tool for probing live Python objects.
keyword List of Python keywords.
knee A Python re-implementation of hierarchical module import.
linecache Cache lines from files.
linuxaudiodev Lunix /dev/audio support.
locale Support for number formatting using the current locale
settings.
logging Python logging facility.
macpath Pathname (or related) operations for the Macintosh.
macurl2path Mac specific module for conversion between pathnames and URLs.
mailbox A class to handle a unix-style or mmdf-style mailbox.
mailcap Mailcap file handling (RFC 1524).
mhlib MH (mailbox) interface.
mimetools Various tools used by MIME-reading or MIME-writing programs.
mimetypes Guess the MIME type of a file.
MimeWriter Generic MIME writer.
mimify Mimification and unmimification of mail messages.
mmap Interface to memory-mapped files - they behave like mutable
strings./font>
multifile Class to make multi-file messages easier to handle.
mutex Mutual exclusion -- for use with module sched.
netrc
nntplib An NNTP client class. Based on RFC 977.
ntpath Common operations on DOS pathnames.
nturl2path Mac specific module for conversion between pathnames and URLs.
optparse A comprehensive tool for processing command line options.
os Either mac, dos or posix depending system.
[DEL:packmail: [DEL:Create a self-unpacking shell archive.:DEL]
DEL]
pdb A Python debugger.
pickle Pickling (save and restore) of Python objects (a faster
Cimplementation exists in built-in module: cPickle).
pipes Conversion pipeline templates.
pkgunil Utilities for working with Python packages.
popen2 variations on pipe open.
poplib A POP3 client class. Based on the J. Myers POP3 draft.
posixfile Extended (posix) file operations.
posixpath Common operations on POSIX pathnames.
pprint Support to pretty-print lists, tuples, & dictionaries
recursively.
profile Class for profiling python code.
pstats Class for printing reports on profiled python code.
pydoc Utility for generating documentation from source files.
pty Pseudo terminal utilities.
pyexpat Interface to the Expay XML parser.
py_compile Routine to "compile" a .py file to a .pyc file.
pyclbr Parse a Python file and retrieve classes and methods.
Queue A multi-producer, multi-consumer queue.
quopri Conversions to/from quoted-printable transport encoding.
rand Don't use unless you want compatibility with C's rand().
random Random variable generators
re Regular Expressions.
repr Redo repr() but with limits on most sizes.
rexec Restricted execution facilities ("safe" exec, eval, etc).
rfc822 RFC-822 message manipulation class.
rlcompleter Word completion for GNU readline 2.0.
robotparser Parse robots.txt files, useful for web spiders.
sched A generally useful event scheduler class.
sets Module for a set datatype.
sgmllib A parser for SGML.
shelve Manage shelves of pickled objects.
shlex Lexical analyzer class for simple shell-like syntaxes.
shutil Utility functions usable in a shell-like program.
SimpleHTTPServer Simple extension to base http class
site Append module search paths for third-party packages to
sys.path.
smtplib SMTP Client class (RFC 821)
sndhdr Several routines that help recognizing sound.
SocketServer Generic socket server classes.
stat Constants and functions for interpreting stat/lstat struct.
statcache Maintain a cache of file stats.
statvfs Constants for interpreting statvfs struct as returned by
os.statvfs()and os.fstatvfs() (if they exist).
string A collection of string operations.
StringIO File-like objects that read/write a string buffer (a fasterC
implementation exists in built-in module: cStringIO).
sunau Stuff to parse Sun and NeXT audio files.
sunaudio Interpret sun audio headers.
symbol Non-terminal symbols of Python grammar (from "graminit.h").
tabnanny,/font> Check Python source for ambiguous indentation.
tarfile Facility for reading and writing to the *nix tarfile format.
telnetlib TELNET client class. Based on RFC 854.
tempfile Temporary file name allocation.
textwrap Object for wrapping and filling text.
threading Proposed new higher-level threading interfaces
threading_api (doc of the threading module)
toaiff Convert "arbitrary" sound files to AIFF files .
token Tokens (from "token.h").
tokenize Compiles a regular expression that recognizes Python tokens.
traceback Format and print Python stack traces.
tty Terminal utilities.
turtle LogoMation-like turtle graphics
types Define names for all type symbols in the std interpreter.
tzparse Parse a timezone specification.
unicodedata Interface to unicode properties.
urllib Open an arbitrary URL.
urlparse Parse URLs according to latest draft of standard.
user Hook to allow user-specified customization code to run.
UserDict A wrapper to allow subclassing of built-in dict class.
UserList A wrapper to allow subclassing of built-in list class.
UserString A wrapper to allow subclassing of built-in string class.
[DEL:util:DEL] [DEL:some useful functions that don't fit elsewhere !!:DEL]
uu UUencode/UUdecode.
unittest Utilities for implementing unit testing.
wave Stuff to parse WAVE files.
weakref Tools for creating and managing weakly referenced objects.
webbrowser Platform independent URL launcher.
[DEL:whatsound: [DEL:Several routines that help recognizing sound files.:DEL]
DEL]
whichdb Guess which db package to use to open a db file.
xdrlib Implements (a subset of) Sun XDR (eXternal Data
Representation)
xmllib A parser for XML, using the derived class as static DTD.
xml.dom Classes for processing XML using the Document Object Model.
xml.sax Classes for processing XML using the SAX API.
xmlrpclib Support for remote procedure calls using XML.
zipfile Read & write PK zipped files.
[DEL:zmod:DEL] [DEL:Demonstration of abstruse mathematical concepts.:DEL]
* Built-ins *
sys Interpreter state vars and functions
__built-in__ Access to all built-in python identifiers
__main__ Scope of the interpreters main program, script or stdin
array Obj efficiently representing arrays of basic values
math Math functions of C standard
time Time-related functions (also the newer datetime module)
marshal Read and write some python values in binary format
struct Convert between python values and C structs
* Standard *
getopt Parse cmd line args in sys.argv. A la UNIX 'getopt'.
os A more portable interface to OS dependent functionality
re Functions useful for working with regular expressions
string Useful string and characters functions and exceptions
random Mersenne Twister pseudo-random number generator
thread Low-level primitives for working with process threads
threading idem, new recommanded interface.
* Unix/Posix *
dbm Interface to Unix ndbm database library
grp Interface to Unix group database
posix OS functionality standardized by C and POSIX standards
posixpath POSIX pathname functions
pwd Access to the Unix password database
select Access to Unix select multiplex file synchronization
socket Access to BSD socket interface
* Tk User-interface Toolkit *
tkinter Main interface to Tk
* Multimedia *
audioop Useful operations on sound fragments
imageop Useful operations on images
jpeg Access to jpeg image compressor and decompressor
rgbimg Access SGI imglib image files
* Cryptographic Extensions *
md5 Interface to RSA's MD5 message digest algorithm
sha Interface to the SHA message digest algorithm
HMAC Keyed-Hashing for Message Authentication -- RFC 2104.
* SGI IRIX * (4 & 5)
al SGI audio facilities
AL al constants
fl Interface to FORMS library
FL fl constants
flp Functions for form designer
fm Access to font manager library
gl Access to graphics library
GL Constants for gl
DEVICE More constants for gl
imgfile Imglib image file interface
* Suns *
sunaudiodev Access to sun audio interface
Workspace exploration and idiom hints
dir(<module>) list functions, variables in <module>
dir() get object keys, defaults to local name space
if __name__ == '__main__': main() invoke main if running as script
map(None, lst1, lst2, ...) merge lists
b = a[:] create copy of seq structure
_ in interactive mode, is last value printed
Python Mode for Emacs
(Not revised, possibly not up to date)
Type C-c ? when in python-mode for extensive help.
INDENTATION
Primarily for entering new code:
TAB indent line appropriately
LFD insert newline, then indent
DEL reduce indentation, or delete single character
Primarily for reindenting existing code:
C-c : guess py-indent-offset from file content; change locally
C-u C-c : ditto, but change globally
C-c TAB reindent region to match its context
C-c < shift region left by py-indent-offset
C-c > shift region right by py-indent-offset
MARKING & MANIPULATING REGIONS OF CODE
C-c C-b mark block of lines
M-C-h mark smallest enclosing def
C-u M-C-h mark smallest enclosing class
C-c # comment out region of code
C-u C-c # uncomment region of code
MOVING POINT
C-c C-p move to statement preceding point
C-c C-n move to statement following point
C-c C-u move up to start of current block
M-C-a move to start of def
C-u M-C-a move to start of class
M-C-e move to end of def
C-u M-C-e move to end of class
EXECUTING PYTHON CODE
C-c C-c sends the entire buffer to the Python interpreter
C-c | sends the current region
C-c ! starts a Python interpreter window; this will be used by
subsequent C-c C-c or C-c | commands
C-c C-w runs PyChecker
VARIABLES
py-indent-offset indentation increment
py-block-comment-prefix comment string used by py-comment-region
py-python-command shell command to invoke Python interpreter
py-scroll-process-buffer t means always scroll Python process buffer
py-temp-directory directory used for temp files (if needed)
py-beep-if-tab-change ring the bell if tab-width is changed
The Python Debugger
(Not revised, possibly not up to date, see 1.5.2 Library Ref section 9.1; in 1.5.2, you may also use debugger integrated in IDLE)
Accessing
import pdb (it's a module written in Python)
-- defines functions :
run(statement[,globals[, locals]])
-- execute statement string under debugger control, with optional
global & local environment.
runeval(expression[,globals[, locals]])
-- same as run, but evaluate expression and return value.
runcall(function[, argument, ...])
-- run function object with given arg(s)
pm() -- run postmortem on last exception (like debugging a core file)
post_mortem(t)
-- run postmortem on traceback object <t>
-- defines class Pdb :
use Pdb to create reusable debugger objects. Object
preserves state (i.e. break points) between calls.
runs until a breakpoint hit, exception, or end of program
If exception, variable '__exception__' holds (exception,value).
Commands
h, help
brief reminder of commands
b, break [<arg>]
if <arg> numeric, break at line <arg> in current file
if <arg> is function object, break on entry to fcn <arg>
if no arg, list breakpoints
cl, clear [<arg>]
if <arg> numeric, clear breakpoint at <arg> in current file
if no arg, clear all breakpoints after confirmation
w, where
print current call stack
u, up
move up one stack frame (to top-level caller)
d, down
move down one stack frame
s, step
advance one line in the program, stepping into calls
n, next
advance one line, stepping over calls
r, return
continue execution until current function returns
(return value is saved in variable "__return__", which
can be printed or manipulated from debugger)
c, continue
continue until next breakpoint
j, jump lineno
Set the next line that will be executed
a, args
print args to current function
rv, retval
prints return value from last function that returned
p, print <arg>
prints value of <arg> in current stack frame
l, list [<first> [, <last>]]
List source code for the current file.
Without arguments, list 11 lines around the current line
or continue the previous listing.
With one argument, list 11 lines starting at that line.
With two arguments, list the given range;
if the second argument is less than the first, it is a count.
whatis <arg>
prints type of <arg>
!
executes rest of line as a Python statement in the current stack frame
q quit
immediately stop execution and leave debugger
<return>
executes last command again
Any input debugger doesn't recognize as a command is assumed to be a
Python statement to execute in the current stack frame, the same way
the exclamation mark ("!") command does.
Example
(1394) python
Python 1.0.3 (Sep 26 1994)
Copyright 1991-1994 Stichting Mathematisch Centrum, Amsterdam
>>> import rm
>>> rm.run()
Traceback (innermost last):
File "<stdin>", line 1
File "./rm.py", line 7
x = div(3)
File "./rm.py", line 2
return a / r
ZeroDivisionError: integer division or modulo
>>> import pdb
>>> pdb.pm()
> ./rm.py(2)div: return a / r
(Pdb) list
1 def div(a):
2 -> return a / r
3
4 def run():
5 global r
6 r = 0
7 x = div(3)
8 print x
[EOF]
(Pdb) print r
0
(Pdb) q
>>> pdb.runcall(rm.run)
etc.
Quirks
Breakpoints are stored as filename, line number tuples. If a module is reloaded
after editing, any remembered breakpoints are likely to be wrong.
Always single-steps through top-most stack frame. That is, "c" acts like "n".
|