# Python Quick Reference Pytho - Cheat Sheet

• No tags were found...

Python Quick Reference Pytho - Cheat Sheet

Python Quick ReferenceOPERATOR PRECEDENCE IN EXPRESSIONSOperator Description A`expr,...` String conversion NA{key:expr,...} Dictionary creation NA[expr,...] List creation NA(expr,...) Tuple creation or simple parentheses NAf(expr,...) Function call Lx[index:index] Slicing Lx[index] Indexing Lx.attr Attribute reference Lx**y Exponentiation (x to yth power) R~x Bitwise NOT NA+x, -x Unary plus and minus NAx*y, x/y, x//y, x%y Multiplication, division, remainder Lx+y, x-y Addition, subtraction Lxy Left-shift, right-shift Lx&y Bitwise AND Lx^y Bitwise XOR Lx|y Bitwise OR Lx=y Comparisons Cxy, x!=y, x==y Equality/inequality tests* Cx is y, x is not y Identity tests Cx in y, x not in y Membership tests Cnot x Boolean NOT NAx and y Boolean AND Lx or y Boolean OR Llambda arg,...: expr Anonymous simple function NA* x!=y and xy are the same inequality test (!= is the preferred form, obsolete)A – Associativity L – Left R – Right C – Chaining NA – Not associativeLIST OBJECT METHODSOperator DescriptionL.count(x) Returns the number of occurrences of x in LL.index(x) Returns the index of the first occurrence of x in L or raises anexception if L has no such itemL.append(x) Appends x to the end of LL.extend(l) Appends all the items of list l to the end of LL.insert(i,x) Inserts x at index i in LL.remove(x) Removes the first occurrence ofx from LL.pop(i=-1) Returns the value of the item atindex i and removes it from LL.reverse( ) Reverses, in-place, the items of LL.sort(f=cmp) Sorts, in-place, the items of L,comparing items by fExcerpted from Python in a Nutshellwww.oreilly.com

Python Quick ReferenceCOMMON FILE OPERATIONSOperationInterpretationoutput = open('/tmp/spam', 'w') Create output file ('w' means write).input = open('data', 'r') Create input file ('r' means read).S = input.read( )Read entire file into a single string.S = input.read(N)Read N bytes (1 or more).S = input.readline( )Read next line (through end-line marker).L = input.readlines( )Read entire file into list of line strings.output.write(S)Write string S into file.output.writelines(L)Write all line strings in list L into file.output.close( )Manual close (done for you when filecollected).COMMON DICTIONARY LITERALS AND OPERATIONSOperationInterpretationD1 = { }Empty dictionaryD2 = {'spam': 2, 'eggs': 3}Two-item dictionaryD2['eggs']Indexing by keyD2.has_key('eggs'), 'eggs' in D2 membership testD2.keys( ), D2.values( ), D2.items( ) lists of keys, values, itemsD2.copy( ), D2.update(D1)shallow copy, dict mergingD2.get(key, default=None)"indexing" w/default valuelen(D1)Length (number stored entries)D2[key] = 42Adding/changing,del D2[key]deletingD4 = dict(zip(keyslist, valslist)) ConstructionCOMMON TUPLE LITERALS AND OPERATIONSOperationInterpretation( ) An empty tupleT1 = (0,)A one-item tuple (not an expression)T2 = (0, 'Ni', 1.2, 3)A four-item tupleT2 = 0, 'Ni', 1.2, 3Another four-item tuple (same as prior line)T1[i]IndexingT1[i:j]slicinglen(t1)length (number of items)T1 + T2ConcatenationT2 * 3repetitionfor x in T2Iteration3 in T2 membershiptestExcerpted from LearningPython, 2nd Editionwww.oreilly.com©2004 O’Reilly Media, Inc. O’Reilly logo is a registered trademark ofO’Reilly Media Inc. All other trademarks are property of their respective owners. #40055

More magazines by this user
Similar magazines