What optimisations are done that this code completes quickly?
<p>I was solving a problem I came across, what is the sum of powers of 3 from 0 to 2009 mod 8.</p>

<p>I got an answer using pen and paper, and tried to verify it with some simple python</p>

<pre><code>print(sum(3**k for k in range(2010)) % 8)
</code></pre>

<p>I was surprised by how quickly it returned an answer. My question is what optimisations or tricks are used by the interpreter to get the answer so quickly?</p>

<p>No fancy optimizations are responsible for the fast response you observed. Computers are just a lot faster in absolute terms than you expected.</p>

python python-3.x optimization