Yeah it's a fluke. If you run the timeits a few times in my case it settles out with g being faster than h by a little most of the time. I found another minor improvement, there may be more also.
ETA: actually, I've run it a few times now and h does come out faster pretty often. I suppose they're pretty close.
Code:
def f():
n = []
for i in range(101):
if (i%5 == 0) == True:
n.append(i)
def g():
n = []
for i in range(101):
if i%5 == 0:
n.append(i)
def h():
n = []
for i in range(101):
if i%5 == 0 is True:
n.append(i)
def i():
n = [x for x in range(101) if not x % 5]
def j():
n = range(0, 101, 5)
Code:
time of f()
15.9861290455
time of g()
12.2365651131
time of h()
11.1904981136
time of i()
9.35484814644
time of j()
0.545165061951
I threw in "j" to try to get an idea of how much room for improvement there was.
I think it's interesting to note that we can get almost a 2x speedup just by being careful with how we construct a list. A general rule of thumb in python is that if you can do something in a single statement, it'll be faster than the equivalent in multiple statements.