| Index: Misc/cheatsheet |
| =================================================================== |
| --- Misc/cheatsheet (revision 88126) |
| +++ Misc/cheatsheet (working copy) |
| @@ -1,4 +1,4 @@ |
| - Python 2.3 Quick Reference |
| + Python 3.2 Quick Reference |
| 25 Jan 2003 upgraded by Raymond Hettinger for Python 2.3 |
| @@ -16,7 +16,7 @@ |
| 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/ |
| +bugs.python.org/ ActivePython : http://www.ActiveState.com/ASPN/ |
| Python/ |
| newsgroup: comp.lang.python Help desk: help@python.org |
| Resources: http://starship.python.net/ |
| @@ -29,82 +29,63 @@ |
| Python Pocket Reference by Mark Lutz (O'Reilly) |
| -Invocation Options |
| +Invocation |
| -python [-diOStuUvxX?] [-c command | script | - ] [args] |
| +python.exe [option] ... [-c cmd | -m mod | file | -] [arg] ... |
| +Options and arguments (and corresponding environment variables): |
| +-b : issue warnings about str(bytes_instance), str(bytearray_instance) |
| + and comparing bytes/bytearray with str. (-bb: issue errors) |
| +-B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x |
| +-c cmd : program passed in as string (terminates option list) |
| +-d : debug output from parser; also PYTHONDEBUG=x |
| +-E : ignore PYTHON* environment variables (such as PYTHONPATH) |
| +-h : print this help message and exit (also --help) |
| +-i : inspect interactively after running script; forces a prompt even |
| + if stdin does not appear to be a terminal; also PYTHONINSPECT=x |
| +-m mod : run library module as a script (terminates option list) |
| +-O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x |
| +-OO : remove doc-strings in addition to the -O optimizations |
| +-q : don't print version and copyright messages on interactive startup |
| +-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE |
| +-S : don't imply 'import site' on initialization |
| +-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x |
| + see man page for details on internal buffering relating to '-u' |
| +-v : verbose (trace import statements); also PYTHONVERBOSE=x |
| + can be supplied multiple times to increase verbosity |
| +-V : print the Python version number and exit (also --version) |
| +-W arg : warning control; arg is action:message:category:module:lineno |
| + also PYTHONWARNINGS=arg |
| +-x : skip first line of source, allowing use of non-Unix forms of #!cmd |
| +-X opt : set implementation-specific option |
| +file : program read from script file |
| +- : program read from stdin (default; interactive mode if a tty) |
| +arg ...: arguments passed to program in sys.argv[1:] |
| - 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 |
| --m mod run library module as a script (terminates option list |
| --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 |
| --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. |
| +Other environment variables: |
| +PYTHONSTARTUP: file executed on interactive startup (no default) |
| +PYTHONPATH : ':'-separated list of directories prefixed to the |
| + default module search path. The result is sys.path. |
| +PYTHONHOME : alternate <prefix> directory (or <prefix>:<exec_prefix>). |
| + The default module search path uses <prefix>/pythonX.X. |
| +PYTHONCASEOK : ignore case in 'import' statements (Windows). |
| +PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr. |
| - * 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 |
| + as |
| 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 |
| + continue exec import pass with |
| + def finally in print yield |
| * (list of keywords in std module: keyword) |
| - * Illegitimate Tokens (only valid in strings): @ $ ? |
| + * 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 |
| @@ -115,25 +96,24 @@ |
| Identifiers |
| - (letter | "_") (letter | digit | "_")* |
| - |
| + ASCII: (letter | "_") (letter | digit | "_")* |
| + |
| + * Unicode identifiers are supported. See PEP 3131. |
| * 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) |
| + * Special forms: _ident (not imported by 'from module import *' unless |
| + listed in __all__); __ident__ (system defined name); |
| + __ident (class-private name mangling) |
| -Strings |
| +Strings (Unicode) |
| "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 |
| @@ -148,8 +128,9 @@ |
| \" 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 |
| + \uHHHH BMP unicode char with hex value HHHH |
| + \UHHHHHHHH unicode char with hex value HHHHHHHH |
| + \N{CHAR NAME} unicode char with name 'CHAR NAME' |
| \AnyOtherChar is left as-is |
| * NUL byte (\000) is NOT an end-of-string marker; NULs may be embedded in |
| @@ -158,10 +139,10 @@ |
| Numbers |
| - Decimal integer: 1234, 1234567890546378940L (or l) |
| - Octal integer: 0177, 0177777777777777777 (begin with a 0) |
| + Decimal integer: 1234, 1234567890546378940 |
| + Octal integer: 0o177, 0o177777777777777777 (begin with 0o or 0O) |
| Hex integer: 0xFF, 0XFFFFffffFFFFFFFFFF (begin with 0x or 0X) |
| - Long integer (unlimited precision): 1234567890123456 |
| + Binary integer: 0b11, 0b111111110000011 (begin with 0b or 0B) |
| 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) |