#----------------------------------------------------------------------------- #----------------------------------------------------------------------------- Basic Variable Stuff: del V Delets V. V=None Nullifies V. id(V) Prints id number for V. type(V) Prints type of variable (string, etc.) repr(V) Returns string representation of V. #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- Lists: L = [a, b, c] A list. Mutable. L[2] = 7 OK L.sort() Sort the list in place. Returns None. sorted(L) Returns a sorted list. L2 = L1 "shallow" copy of a list. Since a list is mutable, L1 and L2 point to the same memory. Therefore changing L2 will change L1 and vice versa. L2 = L1[:] One level "deep" copy of a list. Now changing L2 will not change L1 on the first level. L.append(x) Add an item to the end of the list; equivalent to a[len(a):] = [x]. L.extend(L) Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L. L.insert(i, x) Insert an item at a given position. The first argument is the index of the element before which to insert. Thus a.insert(0, x) inserts at the front of the list, a.insert(len(a), x) is equivalent to a.append(x). L.remove(x) Remove the first item from the list whose value is x. It is an error if there is no such item. L.pop([i]) Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the L. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.) L.index(x) Return the index in the list of the first item whose value is x. It is an error if there is no such item. L.count(x) Return the number of times x appears in the list. L.sort() Sort the items of the list, in place. L.reverse() Reverse the elements of the list, in place. #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- Tuple: T = (a, b, c) A tuple. Immutable #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- Dictionary: D = {"a": "aaa", "b": 500} A Dictionary D["b"] Lookup #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- String methods: `num` Turn a number into a string S1 in S2 Return true if S2.find(S1) == -1. false otherwise. S.capitalize( ) Return a copy of the string with only its first character capitalized. S.center(width[, fillchar]) Return centered in a string of length width. Padding is done using the specified fillchar (default is a space). S.count(sub[, start[, end]]) Return the number of occurrences of substring sub in string S[start:end]. Optional arguments start and end are interpreted as in slice notation. S.decode([encoding[, errors]]) Decodes the string using the codec registered for encoding. S.encode([encoding[,errors]]) Return an encoded version of the string. S.endswith(suffix[, start[, end]]) Return True if the string ends with the specified suffix, otherwise return False. Suffix can also be a tuple of suffixes to look for. With optional start, test beginning at that position. With optional end, stop comparing at that position. S.expandtabs([tabsize]) Return a copy of the string where all tab characters are expanded using spaces. If tabsize is not given, a tab size of 8 characters is assumed. S.find(sub[, start[, end]]) Return the lowest index in the string where substring sub is found, such that sub is contained in the range [start, end]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found. S.index(sub[, start[, end]]) Like S.find(), but raise ValueError when the substring is not found. S.isalnum() Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise. S.isalpha() Return true if all characters in the string are alphabetic and there is at least one character, false otherwise. S.isdigit() Return true if all characters in the string are digits and there is at least one character, false otherwise. S.islower() Return true if all cased characters in the string are lowercase and there is at least one cased character, false otherwise. S.isspace() Return true if there are only whitespace characters in the string and there is at least one character, false otherwise. S.istitle() Return true if the string is a titlecased string and there is at least one character. For example, uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return false otherwise. S.isupper() Return true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise. S.join(seq) Return a string which is the concatenation of the strings in the sequence seq. The separator between elements is the string providing this method. S.ljust(width[, fillchar]) Return the string left justified in a string of length width. Padding is done using the specified fillchar (default is a space). The original string is returned if width is less than len(s). S.lower() Return a copy of the string converted to lowercase. S.lstrip([chars]) Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. S.partition(sep) Split the string at the first occurrence of sep. Return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings. S.replace(old, new[, count]) Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced. S.rfind(sub [,start [,end]]) Return the highest index in the string where substring sub is found, such that sub is contained within s[start,end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure. S.rindex(sub[, start[, end]]) Like S.rfind() but raises ValueError when the substring sub is not found. S.rjust(width[, fillchar]) Return the string right justified in a string of length width. Padding is done using the specified fillchar (default is a space). The original string is returned if width is less than len(s). S.rpartition(sep) Split the string at the last occurrence of sep. Return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself. S.rsplit([sep [,maxsplit]]) Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done, the rightmost ones. If sep is not specified or None, any whitespace string is a separator. Except for splitting from the right, S.rsplit() behaves like S.split(). S.rstrip([chars]) Return a copy of the string with trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace and end-of-line chars. The chars argument is not a suffix; rather, all combinations of its values are stripped: S.split([sep [,maxsplit]]) Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. Thus, the list will have at most maxsplit+1 elements. Consecutive delimiters are not grouped together and are deemed to delimit empty strings. The sep argument may consist of multiple characters. For example, "'1, 2, 3'.split(', ')" returns "['1', '2', '3']"). Splitting an empty string with a specified separator returns "['']". If sep is not specified or is None, a different splitting algorithm is applied. First, whitespace characters (spaces, tabs, newlines, returns, and formfeeds) are stripped from both ends. Then, words are separated by arbitrary length strings of whitespace characters. Consecutive whitespace delimiters are treated as a single delimiter. For example, ("'1 2 3'.split()" returns "['1', '2', '3']"). Splitting an empty string or a string consisting of just whitespace returns an empty list. S.splitlines([keepends]) Return a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true. S.startswith(prefix[, start[, end]]) Return True if string starts with the prefix, otherwise return False. Prefix can also be a tuple of prefixes to look for. With optional start, test string beginning at that position. With optional end, stop comparing string at that position. S.strip([chars]) Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. S.swapcase() Return a copy of the string with uppercase characters converted to lowercase and vice versa. S.title() Return a titlecased version of the string: Words start with uppercase characters, all remaining cased characters are lowercase. S.translate(table[, deletechars]) Return a copy of the string where all characters occurring in the optional argument deletechars are removed, and the remaining characters have been mapped through the given translation table, which must be a string of length 256. You can use the maketrans() helper function in the string module to create a translation table. S.upper() Return a copy of the string converted to uppercase. S.zfill(width) Return the numeric string left filled with zeros in a string of length width. The original string is returned if width is less than len(s). #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- Loops and Conditionals: while : ... for in : ... break Exit the loop continue Cycle to next iteration if : ... elif : ... else: ... A is B Test if points to same memory point. #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- Modules/Doc: import import as from import * from import , , ... __init__.py If this is in a directory then "import ." will import .py form the directory. _blabla Private. Not imported with "from module import *" __blabla Very private. Not visiable with dir(object). __blabla__ System defined. dir(M) Give list of names in module M. dir() Give list of names (except built-in ones) currently defined dir(__builtin__) Gives names of built-in functions reload(M) Reload module M. The following can be use to execute code in a module when "phthon " is typed on the shell command line [note that this code is not executed when the module is loaded using "import " within python]: if __name__ == "__main__": bla bla bla... #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- Classes: hasattr(V, "attr") Returns True if V.attr exists callable(V.attr) Returns True if V.attr can be called. #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- String Formatting: A conversion specifier contains two or more characters and has the following components, which must occur in this order: 1. The '%' character, which marks the start of the specifier. 2. Mapping key (optional), consisting of a parenthesised sequence of characters (for example, (somename)). 3. Conversion flags (optional), which affect the result of some conversion types. 4. Minimum field width (optional). If specified as an '*' (asterisk), the actual width is read from the next element of the tuple in values, and the object to convert comes after the minimum field width and optional precision. 5. Precision (optional), given as a '.' (dot) followed by the precision. If specified as '*' (an asterisk), the actual width is read from the next element of the tuple in values, and the value to convert comes after the precision. 6. Length modifier (optional). 7. Conversion type. When the right argument is a dictionary (or other mapping type), then the formats in the string must include a parenthesised mapping key into that dictionary inserted immediately after the '%' character. The mapping key selects the value to be formatted from the mapping. For example: print '%(language)s has %(number)03d quote types.' % {"language": "Python", "number": 2} Python has 002 quote types. In this case no * specifiers may occur in a format (since they require a sequential parameter list). The conversion flag characters are: Flag Meaning '#' The value conversion will use the "alternate form" (where defined below). '0' The conversion will be zero padded for numeric values. '-' The converted value is left adjusted (overrides the '0' conversion if both are given). ' ' (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). A length modifier (h, l, or L) may be present, but is ignored as it is not necessary for Python For example: %ld is identical to %d. The conversion types are: Conversion Meaning Notes 'd' Signed integer decimal. 'i' Signed integer decimal. 'o' Signed octal value. 'u' Obsolete type. It is identical to 'd'. 'x' Signed hexadecimal (lowercase). 'X' Signed hexadecimal (uppercase). 'e' Floating point exponential format (lowercase). 'E' Floating point exponential format (uppercase). 'f' Floating point decimal format. 'F' Floating point decimal format. 'g' Floating point format. Uses lowercase exponential format 'G' Floating point format. Uses uppercase exponential format '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. #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- Files: handle_name = open(file_name, mode = m) m = 'r' Open for reading. The file must exist. (default) 'w' Open for writing. Erase an existing file first. 'a' Open for writing. Append input. 'r+' Open for reading and writing. The file must exist. Start at beginning of file. 'w+' Open for reading and writing. Erase an existing file first. 'a+' Open for reading and writing. Start at end of file. Append 'b' to the mode for binary files. File list with wildcard example: import glob glob.glob('*.py') Write to a file: out_file = open(file_name, 'w') out_file.write(line) out_file.close() #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- Regular Expressions Anchors: ^ Start of line \A Start of string $ End of line \Z End of string \b Word boundary \B Not word boundary \< Start of word \> End of word #------------------- Sample Patterns ([A-Za-z0-9-]+) Letters, numbers and hyphens (\d{1,2}\/\d{1,2}\/\d{4}) Date (e.g. 21/3/2006) ([^\s]+(?=\.(jpg|gif|png))\.\2) jpg, gif or png image (^[1-9]{1}$|^[1-4]{1}[0-9]{1}$|^50$) Any number from 1 to 50 inclusive (#?([A-Fa-f0-9]){3}(([A-Fa-f0-9]){3})?) Valid hexadecimal colour code ((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,15}) 8 to 15 character string with at least one upper case letter, one lower case letter, and one digit. (\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6}) Email addresses (\<(/?[^\>]+)\>) HTML Tags #------------------- Character Classes \c Control character \s White space \S Not white space \d Digit \D Not digit \w Word \W Not word \xhh Hexadecimal character hh \Oxxx Octal character xxx #------------------- POSIX Character Classes [:upper:] Upper case letters [:lower:] Lower case letters [:alpha:] All letters [:alnum:] Digits and letters [:digit:] Digits [:xdigit:] Hexadecimal digits [:punct:] Punctuation [:blank:] Space and tab [:space:] Blank characters [:cntrl:] Control characters [:graph:] Printed characters [:print:] Printed characters andspaces [:word:] Digits, letters andunderscore #------------------- Assertions ?= Lookahead assertion + ?! Negative lookahead + ?<= Lookbehind assertion + ?!= or ? Once-only Subexpression ?() Condition [if then] ?()| Condition [if then else] ?# Comment #------------------- Quantifiers * 0 or more + *? 0 or more, ungreedy + + 1 or more + +? 1 or more, ungreedy + ? 0 or 1 + ?? 0 or 1, ungreedy + {3} Exactly 3 + {3,} 3 or more + {3,5} 3, 4 or 5 + {3,5}? 3, 4 or 5, ungreedy + #------------------- Special Characters \ Escape Character + \n New line + \r Carriage return + \t Tab + \v Vertical tab + \f Form feed + \a Alarm [\b] Backspace \e Escape \N{name} Named Character #------------------- String Replacement (Backreference) $n nth non-passive group $2 "xyz" in /^(abc(xyz))$/ $1 "xyz" in /^(?:abc)(xyz)$/ $` Before matched string $' After matched string ' $+ Last matched string $& Entire matched string $_ Entire input string $$ Literal "$" #------------------- Ranges (Note: Ranges are inclusive) . Any character exceptnew line (\n) + (a|b) a or b + (...) Group + (?:...) Passive Group + [abc] Range (a or b or c) + [^abc] Not a or b or c + [a-q] Letter between a and q + [A-Q] Upper case letter between A and Q + [0-7] Digit between 0 and 7 + \n nth group/subpattern + #------------------- Patter Modifiers g Global match i Case-insensitive m Multiple lines s Treat string as single line x Allow comments and white space in pattern e Evaluate replacement U Ungreedy pattern #------------------- Metacharacters (Must be escaped) ^ [ . $ { * ( \ + ) | ? < > #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- Command line: import sys for arg in sys.argv[1:]: