If you're playing with performance-sensitive code, always ensure that loops don't execute in the module scope (for example, while writing half-assed benchmarks).
Doing so results in a potentially sizeable slowdown to your loop. This is because Python cannot compile to LOAD_FAST/STORE_FAST operations, since all assignments in the module scope must be reflected in the module's dict.
During the execution of a normal Python function, the local scope is represented as an array instead of a dict, with each local variable discovered by the parser assigned a numeric index in the array.
>>> def x(a):
... b = 2
... return a + b
Note this difference in behaviour almost never has a discernible impact on performance, except usually to explain discrepencies in the results of throwaway benchmarks.