# Python Programming/Tips and Tricks

| Previous: Metaclasses | Index | Next: Namespace |
There are many tips and tricks you can learn in Python:

## Strings

* 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:
(but don't worry about this unless your resulting string is more than 500-1000 characters long)

## 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 cProfile/profile pair still exists (as of Python 3.4).

### importing

The C version of a module named module or Module is called cModule, and frequently imported using import...as 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 import...as is required, so the code does not depend on which module was imported:

### Examples

Notable examples include:
* (Python 2.x) cPickle for pickle, up to 1000× faster.
* (Python 2.x) cStringIO for StringIO, replaced by io.StringIO in Python 3
* cProfile for profile – the Python profile adds significant overhead, and thus cProfile is recommended for most use.
* (not needed in Python 3.3+) cElementTree for ElementTree, 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:

## References

* ↑ "'concat vs join - followup' on 'Python Rocks! and other rants 27.8.2004 Weblog of Kent S Johnson'". August 27, 2004. Retrieved 2008-08-29.
* ↑ What’s New In Python 3.0, Guido van Rossum
* ↑ "The cElementTree Module", January 30, 2005, Fredrik Lundh
| Previous: Metaclasses | Index | Next: Namespace |