DEV Community

Aniket Satbhai
Aniket Satbhai

Posted on

Some useful ipython magic commands

In [1]: %precision 2
Out[1]: '%.2f'

In [2]: import math

In [3]: math.sqrt(2)
Out[3]: 1.41
Enter fullscreen mode Exit fullscreen mode
In [1]: %time sum(range(10000))
CPU times: user 91 µs, sys: 6 µs, total: 97 µs
Wall time: 98.2 µs
Out[1]: 49995000
Enter fullscreen mode Exit fullscreen mode
In [1]: %timeit sum(range(10000))
114 µs ± 8.23 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Enter fullscreen mode Exit fullscreen mode
In [1]: %load example.py

In [2]: # %load -r 1:3 example.py
   ...: import numpy as np
   ...: import pandas as pd
Enter fullscreen mode Exit fullscreen mode
In [1]: %save sample.py 1
The following commands were written to file `sample.py`:
get_ipython().run_line_magic('load', 'example.py')
Enter fullscreen mode Exit fullscreen mode

Ref.: https://ipython.readthedocs.io/en/stable/interactive/magics.html

Top comments (0)