What is Jupyter Notebook (2)

Magic

Jupyter Notebook has many special commands, that called “magic” command. They are prefixed to % or %%. % means the whole line is under “magic” command. %% means the whole area is under “magic” command. By default, % can be omitted if there is no variable conflict.

  • %magic_name? — get documentation of the magic
  • %automagic

As mentioned before, % can be omitted by default. If you want to disable this function, use %automagic.

  • %quickref — Jupyter Notebook quick reference card
  • %magic — documentation for all magic command
  • %run — run .py file immediately
  • -i: give script access toe variable in notebook
  • -d: invoke the debugger before executing any code, enter s (step) to enter the script
  • %paste — execute pre-formatted Python code from clipboard
  • %cpaste — manually paste Python code to be executed
  • %time — report the execution time of a single statement
In [11]: %time 2^25
CPU times: user 4 µs, sys: 1 µs, total: 5 µs
Wall time: 10 µs
Out[11]: 27

*%timeit — run a statement for several time, and get the average time

In [12]: %timeit 2^30
100000000 loops, best of 3: 13.3 ns per loop
  • %hist — print all or part of input history
  • %reset — clear the interactive namespace
In [14]: %reset
Once deleted, variables cannot be recovered. Proceed (y/[n])? y
  • %xdel — remove all references to a particular object
In [16]: a = 5
In [17]: b = a
In [18]: b
Out[18]: 5
In [19]: %xdel a
In [20]: b
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-20-3b5d5c371295> in <module>()
----> 1 b
NameError: name 'b' is not defined
  • %logstart, %logoff, %logon, %logstate, %logstop — log the input and out
  • %debug — enter immediately after an exception
  • %pdb — automatically invoke debugger after any exception
Show your support

Clapping shows how much you appreciated Rachel Fu’s story.