Python Programming/Tips and Tricks

There are many tips and tricks you can learn in Python:

Strings
(but don't worry about this unless your resulting string is more than 500-1000 characters long)
 * Triple quotes are an easy way to define a string with both single and double quotes.
 * String concatenation is expensive. Use percent formatting and str.join for concatenation:

Optimized C modules
Several modules have optimized versions written in C, which provide an almost-identical interface and are frequently much faster or more memory-efficient than the pure Python implementations. Module behavior generally does differ in some respects, often minor, and thus C versions are frequently used.

This is primarily a Python 2.x feature, which has been largely removed in Python 3, with modules automatically using optimized implementations if available. However, the /  pair still exists (as of Python 3.4).

importing
The C version of a module named  or   is called , and frequently imported using   to strip off the prefix, as: For compatibility, one can try to import the C version and fall back to the Python version if the C version is not available; in this case using  is required, so the code does not depend on which module was imported:

Examples
Notable examples include:
 * (Python 2.x)  for , up to 1000× faster.
 * (Python 2.x)  for , replaced by   in Python 3
 * for  – the Python   adds significant overhead, and thus   is recommended for most use.
 * (not needed in Python 3.3+)  for , 15–20 times faster and uses 2–5 times less memory; not needed in Python 3.3+, which automatically uses a fast implementation if possible.

List comprehension and generators

 * List comprehension and generator expressions are very useful for working with small, compact loops. Additionally, it is faster than a normal for-loop.
 * List comprehension and generator expression can be used to work with two (or more) lists with zip or itertools.izip

Data type choice
Choosing the correct data type can be critical to the performance of an application. For example, say you have 2 lists: and you want to find the entries common to both lists. You could iterate over one list, checking for common items in the other: For such small lists, this will work fine, but for larger lists, for example if each contains thousands of entries, the following will be more efficient, and produces the same result: Sets are optimized for speed in such functions. Dictionaries themselves cannot be used as members of a set as they are mutable, but tuples can. If one needs to do set operations on a list of dictionaries, one can convert the items to tuples and the list to a set, perform the operation, then convert back. This is often much faster than trying to replicate set operations using string functions.

Other

 * Decorators can be used for handling common concerns like logging, db access, etc.
 * While Python has no built-in function to flatten a list you can use a recursive function to do the job quickly.
 * To stop a Python script from closing right after you launch one independently, add this code:
 * Python already has a GUI built in: Tkinter, based on Tcl's Tk. More are available, such as PyQt4, pygtk3, and wxPython.


 * Ternary Operators:


 * Booleans as indexes: