pythonjupyter-notebooktimeitmagic-command

Why does %%timeit work fine in Jupyter Notebook but throws SyntaxError in Python Shell IDLE?


%%timeit
d = deque()
for i in range(40000):
    d.appendleft(i)

The above code prints out the time of execution in Jupyter Notebook as: 3.39 ms ± 168 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) However, when I run this code in Python IDLE Shell, it indicates SyntaxError at % in %%timeit. How is that so?


Solution

  • Because %% is Jupyter's "cell magic": https://ipython.readthedocs.io/en/stable/interactive/magics.html#cell-magics

    It's not native Python syntax.