[Python-checkins] python/dist/src/Misc cheatsheet,1.2,1.3
rhettinger@users.sourceforge.net
rhettinger@users.sourceforge.net
Sat, 25 Jan 2003 13:23:01 -0800
Update of /cvsroot/python/python/dist/src/Misc
In directory sc8-pr-cvs1:/tmp/cvs-serv16384
Modified Files:
cheatsheet
Log Message:
Part I of an update for Python 2.3.
Index: cheatsheet
===================================================================
RCS file: /cvsroot/python/python/dist/src/Misc/cheatsheet,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** cheatsheet 6 Aug 2001 17:43:49 -0000 1.2
--- cheatsheet 25 Jan 2003 21:22:52 -0000 1.3
***************
*** 1,6 ****
! Python 2.0 Quick Reference
!
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
--- 1,6 ----
! 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
***************
*** 20,49 ****
Python/
newsgroup: comp.lang.python Help desk: help@python.org
! Resources: http://starship.python.net/ and http://www.vex.net/parnassus/
Full documentation: http://www.python.org/doc/
! An excellent Python reference book: Python Essential Reference by David Beazley
! (New Riders)
!
!
! Contents
!
! * Invocation Options
! * Environment Variables
! * Lexical Entities : keywords, identifiers, strings, numbers, sequences,
! dictionaries, operators
! * Basic Types And Their Operations
! * Advanced Types
! * Statements
! * Built In Functions
! * Built In Exceptions
! * Standard methods & operators redefinition in user-created Classes
! * Special informative state attributes for some types
! * Important Modules : sys, os, posix, posixpath, shutil, time, string, re,
! math, getopt
! * List of modules In base distribution
! * Workspace Exploration And Idiom Hints
! * Python Mode for Emacs
! * The Python Debugger
!
--- 20,31 ----
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)
***************
*** 54,69 ****
Invocation Options
Option Effect
-d Outputs parser debugging information (also PYTHONDEBUG=x)
-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 (set __debug__ = 0 =>s suppresses asserts)
-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).
- -U Force Python to interpret all string literals as Unicode literals.
-v Verbose (trace import statements) (also PYTHONVERBOSE=x)
-x Skip first line of source, allowing use of non-unix Forms of #!cmd
- [DEL:-X [DEL:Disable class based built-in exceptions (for backward
- :DEL] compatibility management of exceptions):DEL]
-? Help!
-c Specify the command to execute (see next section). This terminates the
--- 36,54 ----
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
***************
*** 116,120 ****
break else global not try
class except if or while
! continue exec import pass
def finally in print
--- 101,105 ----
break else global not try
class except if or while
! continue exec import pass yield
def finally in print
***************
*** 175,181 ****
Decimal integer: 1234, 1234567890546378940L (or l)
! Octal integer: 0177, 0177777777777777777L (begin with a 0)
! Hex integer: 0xFF, 0XFFFFffffFFFFFFFFFFL (begin with 0x or 0X)
! Long integer (unlimited precision): 1234567890123456L (ends with L or l)
Float (double precision): 3.14e-10, .001, 10., 1E3
Complex: 1J, 2+3J, 4+5j (ends with J or j, + separates (float) real and
--- 160,166 ----
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
***************
*** 207,212 ****
Dictionaries (Mappings)
! Dictionary of length 0, 1, 2, etc:
! {} {1 : 'first'} {1 : 'first', 'next': 'second'}
Operators and their evaluation order
--- 192,201 ----
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
***************
*** 236,240 ****
Alternate names are defined in module operator (e.g. __add__ and add for +)
! Most operators are overridable
--- 225,232 ----
Alternate names are defined in module operator (e.g. __add__ and add for +)
! Most operators are overridable.
!
! Many of binary operators support augmented assignment:
! x += 1 # Same as x = x + 1
***************
*** 257,260 ****
--- 249,255 ----
Comparison behavior can be overridden for a given class by defining special
method __cmp__.
+ The above comparisions return True or False which are of type bool
+ (a subclass of int) and behave exactly as 1 or 0 except 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.
***************
*** 345,348 ****
--- 340,344 ----
x in s 1 if an item of s is equal to x, else 0
x not in s 0 if an item of s is equal to x, else 1
+ 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
***************
*** 352,355 ****
--- 348,352 ----
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 :
***************
*** 371,380 ****
del s[i:j] same as s[i:j] = []
s.append(x) same as s[len(s) : len(s)] = [x]
- s.extend(x) same as s[len(s):len(s)]= x (5)
s.count(x) return number of i's for which s[i] == 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.remove(x) same as del s[s.index(x)] (1)
s.pop([i]) same as x = s[i]; del s[i]; return x (4)
s.reverse() reverse the items of s in place (3)
s.sort([cmpFct]) sort the items of s in place (2), (3)
--- 368,377 ----
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)
***************
*** 392,401 ****
They don't return the sorted or reversed list to remind you of this
side effect.
! (4) [New 1.5.2] The pop() method is experimental and not supported by
! other mutable sequence types than lists.
! The optional argument i defaults to -1, so that by default the last
item is removed and returned.
- (5) [New 1.5.2] Experimental ! Raises an exception when x is not a list
- object.
--- 389,394 ----
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.
***************
*** 411,420 ****
d.clear() remove all items from d
d.copy() a shallow copy of d
d.has_key(k) 1 if d has key k, else 0
d.items() a copy of d's list of (key, item) pairs (2)
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.get(k,defaultval) the item of d with key k (4)
d.setdefault(k,defaultval) the item of d with key k (5)
--- 404,419 ----
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) 1 if d has key k, else 0
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)
***************
*** 432,435 ****
--- 431,437 ----
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
***************
*** 447,455 ****
s.count(sub[ return the number of occurrences of substring sub in (2)
,start[,end]]) string s.
s.encode([ return an encoded version of s. Default encoding is the
! encoding[,errors current default string encoding. (3)
! ]])
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.
--- 449,460 ----
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.
***************
*** 458,474 ****
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
--- 463,479 ----
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
***************
*** 504,507 ****
--- 509,514 ----
[,deletechars]) table.
s.upper() return a copy of s converted to uppercase.
+ s.zfill(width) return a string padded with zeroes on the left and
+ sliding a minus sign left if necessary. never truncates.
Notes :