Python is the native language. Invoke interpreter with "python". Collections: ARRAY: ['one', 'two'] ar[1] TUPLE: ('one', 'two'] tup[1] SET: {'one', 'two'} or set(list) x in set. But N.b. {} is empty dict! so initialize set with a = set(). set(string) makes a set of all unique letters in string. set.add(el); set.update(el6, el7); # 2nd adds multiple DICT: {'key': 'val'} di['key'] type(var) is list or type(const) is dict isinstance(obj, ClassName) (boolean) TIPS, plus table of String formatters: http://kogs-www.informatik.uni-hamburg.de/~meine/python_tricks Comment just with #. No /* or // allowed. I've seen 2 sites saying multi-line comments are supported with """....""" or '''...''', but when I try it I find that these just instantiate string contants. ??? Variable __name__ is set to the CURRENT module name, defaulting to the string "__main__". Therefore, to test for top-level program, test: __name__ == "__main__" ** power operator * prefix operator: "*paramname" in the def param list is a var-length tuple param. "*seq_expression" ONLY in a function call: Flattens the tuple by 1 level. Looks to me like the following usage in ogredotscene.py is useless: "*[[a, b], [cd]]" since it == "[a, b], [c d]" EXTREMELY USEFUL MODULES TO USE platform sys incl.: path os.path incl.: abspath, isfile, join, esp. 'path' codecs incl.: open BeautifulSoup, XML and HTML parsing table pretty-print: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/267662 Referencing a module in a package executes the package (i.e., executes the __init__.py). Modules in a package do not have the package name space by default. They must import from their own package. REFERENCE Reference for tuple type suck. Have to drill down on Mutable, etc. See the "operator" module listing for generally available stuff. Where it lists ofX(a, b), you can use a.x(b). Python Tutorial: http://docs.python.org/tut/tut.html Reference: http://docs.python.org/reference/ Really good tips: http://selinap.com/category/python/ Excellent stuff: http://www.diveintopython.org/object_oriented_framework/index.html Somehow, undeclared variables are shared above current block scope (but not above function/method/module/classes). Looks like there is no way at all to require declarations. You do not need to terminate commands with ";" except to delimit multiple commands on a single line. Naming convention __systemthing__ Don't name your stuff like this __privateVisibility Enforce ONLY IN CLASSES. Should not use these __names other than in classes. _privacyRequest Only enforced effect is that "from... import *" does not import _* attributes and fuctions. By convention though, use to indicate private vars. () seem to be necessary only for precedence and to invoke a function or method. Logical expressions generally do not need (): if 1 == 1: print "It's true" bool constants: False, True None constant = absence-of-a-value. Basically, use like Java null. (evaluates to False) N.b. 0 != None, but 0 is False. Strings. Immutable. String methods: http://docs.python.org/library/stdtypes.html#string-methods To tell if a substring is present use str.find(). Just giving a String expression will echo the string single-quoted ONLY IF RUN PROGRAM INPUT FROM A TTY!!! Must use a function like "print" to dereference quotes, char constants, etc. Raw and """ string constants don't work as documented. ' == ". Input escape sequences are converted. Raw SHOULD make string exactly what you give it (but I think you can't continue lines without \, which will still be retained :(). No need with ''' and """, and can also use literal concat + \-continuation. I think that """ and ''' work just like Bourne shell "" (except for $vars). concatenation, literals only: Just place side-by-side like 'one' "two" runtime concatenation: + string repeat operator: string * int characters are simply strings of length 1. str[3] is like Java str.charAt() str[1:2] means substring from before index 1 to before index 2. len(str) str.strip() == Java str.trim() % operator does sprintf but being deprecated, like: print "Hello, %s !" % name OR print "Hello, %s %s % ('me', 'you') # tuple .find() is same as Java .indexOf(). .index() is a throwing alternate of .find(). print v1, v2... [terminate with "," to skip default final newline].??don't work I think need to: import sys; sys.stdout.write("unterminated") (But the Python program output insists on ending with \n?) repr(numerical) converts to string. (printable "repr"esentation) == ` operator repr() is more of a diagnostic/developer output than str(x) provides. See formatstring.format() section below for new style of sprintf-like formatting. Note that print() automatically str()'s its first parameter. You only need to explicitly str if you are concatenating strings and non-strings. Lists. Mutable ['alpha', 100] len(alist); NO x[y] ASSIGNMENT! Must use methods like alist.append('newelement') Equivalent to push: alist.insert(0, 'newelement') ("x[y] =" is a HASH operation!) Logical expr: key [not] in list index iterator: for i in range(len(l)): print "Next " + str(l[i]) list("string") == ['s', 't', 'r', 'i', 'n', 'g'] sort() does IN PLACE sort and returns nothing. Tuples seem to be immutable lists, but use parentheses (which are optional upon input). alpha, beta = 'good', 'bad' # tuple? I think not, no parens Blocks All lines in a block must be indented the same amount. They're preceded with : at end of preceding line. Can generally instead do inline commands after :. If have things on a line after :, then that entire line is the ONLY block! You can not extend that block underneath, but another part of a compound command may continue underneath, like if x: statement1; statement 2 else: statement 3 for x in [list] (a "foreach"). range(10) = 0 1 2 3 4 5 6 7 8 9 To iterate over the indices of a list: for i in range(len(alist)): function definition def fname(arg): "Optional docstring" Variable assignments are LOCAL unless use global statement makes them module-level. Default params, like def fname(arg='aval') You can alternatively invoke like: x = fname(p1=13, p2='heya') The command "pass" is a no-op because empty blocks are prohibited. Functions are variables and can be displayed and reassigned like regular vars. The comment string right after the def can be displayed by users by typing help(funcname). N.b. the comment must be indented exactly as a member of the def block. Execute a script from inside: execfile("script.py") Whether in a module or not, execfile imports exactly as if the content was in the current file, even at top level inside a "class X...:", EXCEPT when inside of any def! N.b. Python vs. replaced with: exec(open("file/path").read()) sets Just like Java sets. set(alist) or set(astring) (A set of the characters in astring) Built in math operators are overloaded to set operations, for intersections, etc. x in y (like Java y.contains(x)) dictionaries Dictionaries must be initialized before assigning to them! Hash maps {'key0': 0, 'key1': 1 } dict(list-of-tuples) -> dict([('alpha', 1), ('beta', 2)]) keys must be immutable "[not] in" operator works just like for sets; h[x] = y (after initialization) delete like: del h[key] dict.has_key(kval) (Looks equivalent to newish "[not] in" operator) dict.keys() To convert key list + val list to dictionay: dict(zip(list1, list2)) To convert single list to dictionary, see my sample.py file. Looping through name+vals: for n, v in a.getAllChannelIpos().iteritems(): (Similarly for iterkeys() and itervalues()). Keys are ordered alphabetically, similar to Java TreeMaps. MODULES __name__ = CURRENT module name, defaulting to the string "__main__". Global variables are nice in that they are module-specific by default. Both scripts and modules are named *.py. Nice auto-module naming!! Module search path is sys.path, defaulting to script-containing-dir (n.b. NOT pwd!!) + $PYTHONPATH). ("python -c 'from sys import path; print(path)'") Relative paths/subdirectories in PYTHONPATH work just as you would want them to. Import names "a.b" translate to "a/b.py". Cannot "from X import *" from inside a def. This appears to be the only import placement limitation. Use module members like import modname # modname must be a package or module modname.member OR import modname.member modname.member Or from modname import member [Can do import *] member The crucial difference between "import..." and "from..." is that with "import..." you still give absolute specifier to imported items; with "from..." you use name as if it were defined in your own module. CRITICAL: One is not a superset of the other. One gives visibility to absolute names; the other imports names into your local namespace. (For a single-level "import...", you effectively import the single module name into your local namespace though). You can use both: Put the "import..." first. ** It is generally unnecessary to do both. The "from..." generally gives access to whatever is needed to succed. I have not yet determined when and why it does not. ** dir() lists available symbols. (It returns a string, so unless you run it interactively, you need to str() it). Modules are executed when imported for the first time. They should only execute setup code (and define function)s. "from x import *". This imports ALL non-_* symbols in that namespace to yours. If module or package x does "from z import y", then you will also be importing y. For this reason, here is preferred import idiom when coding modules, to prevent namespace pollution: from ext.module import item as _intname COMPILATION When you "import" a module, it is (portable) byte compiled into a *.pyc file (.pyo for crappy optimized). Can't compile a top level module/script. PACKAGES There is no end-user way, that I know of, to differentiate a module name from a package name. Just like Java packages, except do not identify the package in the module itself; but do put a file __init__.py in each package directory. There are some idiosyncracies to importing from a package. One form is required to not require absolute package name usage. Extra work to import all SUBMODULES from a package. Imports to top-level package modules automatically look in current package before looking from root. (Stupidly, it does not work from subpackages). NAMING CONVENTION Packages + modules capitalization is reversed from Java Packages + classes. Pkg1.Pkg2.module1 pyodbc: Need python-devel (+ probabyl unixODBC-devel) installed to build. Follow very easy build instructions. Defining stuff Global vars do not need to be defined before their usage. Just remember to "global" them. classes and funcs need to be defined before their usage inside the global context, but not inside of functions CLASSES I am completely skipping "classic classes" and describing new classes. Must be defined physically BEFORE usage. Must extend (perhaps indirectly) object. I don't see any private variables or functions. Built-in Types: dict, long, str, tuple, int, float, unicode, list, object Good idea to implement __repr__() and __str__() Constructors and all instance methods must have first param "self". class newclassname(parentclassname[, otherparentclassname...]): __slots__ = ['x', 'y'] // Optionally list instance vars (== attrs) // If not, then inst. vars available in __dict__ dictionary classAttr = 13 // Constructors: def __init__(self, default=None): parentclassname.__init__(self) self.x = y self.__class__.classAttr... # IMPORTANT: MAKE SURE THAT EVERY SLOT ELEMENT INITIALIZED IN # INIT if you reference it in any method before # assigning. It's a runtime error to ref an unset var. # Usually want to init them to None if you have no val. ... def smethnam(...): newclassname.classAttr += 3 ... def __repr__(self): // == toString() smethnam = staticmethod(smethnam) // Do this magic for each static fn. Class methods are exactly like static methods except use "classmethod" instead of "staticmethod", and add auto-parameter for the runtime invoking class name. Instantiation: inst = classname(...) There's also an optional, (implicitly) static __new__ method which must return a new instance. To access class vars in instance methods (incl. __init__), use self.__class__.*. DESCRIPTORS An attribute with any of these accessor methods: __get__, __set__, __delete__ To make a RO descriptor, define __set__() to raise an AttributeError. (So can just add attr field + 1-line raise __set__() method?) I don't see the big deal. python-wx .OnInit() must return True Binary file I/O: http://linux.byexamples.com/archives/478/python-writing-binary-file/ Basically, write: bfr = [] bfr.append("{0:c}".format(10))... f = open("test.bin", "wb") f.write(''.join(bfr)) f.close() read: f = open("test.bin", "rb") i = ord(f.read(1))... print("({0:d}) ({1:d}) ({2:d})\n".format(i, j, k)) f.close() OR while True: c = f.read(1) if len(c) < 1: break print("({0:d})".format(ord(c))) formatstring string.format() str.format(val) OR str.format(multi, ple) OR string.format(a="v1",b=37) http://docs.python.org/library/stdtypes.html#string-formatting {FIELD NAME} {0} are replaced with following positional parameters (0-based); {name1} are replace with assigment params like: name1='value'; {1[key]} are replace by the specified value of the indexed hash parameter LHS !CONVERSION !r or !s to run repr() or str() LHS :FORMAT SPEC} Can precede the "}" with :format-options. Examples: :10 Left justified in 10 character space :10d Right justified decimal integer LHS ALL: {FIELDNAME[!{r|s}][:FORMATSPEC]} UNICODE: http://evanjones.ca/python-utf8.html constant: u"string" When you split a unicode string, you get a list of unicode strings. A regular string is a "byte string": characters by default locale -> unicode unicode(s[, encoding]) == s.decode(encoding) usl. best: unicode(s, "utf-8") Seems to be no alternative for string.encode(). -> string unicodeStr.encode() string.split works exactly as you would want it to, with fixed delimiters. To split with a regexp, use re.split. join is reversed from what you would expect: "delim".join(list) It captures 0-length segments where they should be. string.strip is a nice leading+trailing white-space stripper (trim) Retrieving Exception source code location: http://ubuntuforums.org/showthread.php?t=246502 is: instance equalify operator For immutables (incl. integers at lease), different variables for the integer are instance equal. Determining Python version: Use the platform module. No. Do: import sys; print(sys.version) List element initialization zeros = [0]*5 results in [0, 0, 0, 0, 0] Env vars are in the hash os.environ. CGI scripts: #!/usr/bin/python print "Content-type: text/html\n" import cgi; import cgitb cgitb.enable() # or cgitb.enable(display=0, logdir="/tmp") form = cgi.FieldStorage() # Call this just once!!! # Optional keep_blank_values param aval = form.getfirst("paramname") # returns None if not sent alist = form.getlist("paramname") # returns () if not sent cgi.*(...) # Useful functions for displaying env, partsing, uuencoding, etc. Looks like the real way to do Python through a web server is by using FLUP. I believe that python has no stderr piping (without explicitly opening descriptors). exit(1) # N.b. parens Has no switch operator Ternary operator (like traditional ? :) in v. 2.5: truVal if expr else falVal Python v3. print has changed from a statement to a function, so now needs parentheses. Exceptions: raise Exception [or subclass] == Java throw except [Exception as x]...: [or subclass] == Java catch (use "," instead of "as" before Python 2.6; for some damned reason x.args is a character tuple, so must "".join(x.args) try: and finally: same as Java Also has else: # negative-except Custom exceptions should extend Exception: class MyE(Exception): pass OR class MyE(Exception): def __init__(self, arg): self.args = arg # Must be on its own line Regexp: v2 only: The middle part between a look-behind and a look-ahead doesn't work if it evaluates to empty string. import re matcher = re.match(r"regexp", str[, opts]) Matcher has implicit ^. if m: m.group(... Also has re.search, re.sub, re.split, re.compile(pattern).split Slurp text file: with open(sys.argv[1]) as f: inStr = f.read()