We discuss code optimization from the point of view of the memory hierarchy. You are probably aware of the disk/RAM hierarchy and the tradeoff between memory and speed. With certain classes of algorithms you can easily take advantage of the cache/RAM hierarchy just by being careful on how you process data. I show a very simple example involving matrix manipulation where you can easily get speedups of up to 40%. And I am pretty sure there are plenty situations where you can do better.
Most language compilers and interpreters create Abstract Syntax Trees (ASTs) to represent your code before translation or execution. Python makes these trees accessible to the developer via the ast module. This allows for pretty powerful program analysis and even transformation. This article is a primer on Python's ast module. After reading this you will understand the basics of automated code traversing and manipulation by exploring a illustrative example dealing with converting Python 2 to 3 code.