Skip to content

Jupyter Notebook Cheat Sheets

Anaconda



Jupyter Notebook





Objects

Find, list, search, reset.

a = 10
b = 20.1
c = "test"
d = [1, 2, 3]
who
1
a    b   c   d
whos
1
2
3
4
5
6
Variable   Type     Data/Info
-----------------------------
a          int      10
b          float    20.1
c          str      test
d          list     n=3
reset
1
Once deleted, variables cannot be recovered. Proceed (y/[n])? y
who
1
Interactive namespace is empty.
a = 10
b = 20
c = "test"
d = [1, 2, 3]
who
1
a    b   c   d

Logging

Start, state, on, off.

logstate
1
Logging has not been activated.
logstart
1
2
3
4
5
6
7
Activating auto-logging. Current session state plus future input saved.
Filename       : ipython_log.py
Mode           : rotate
Output logging : False
Raw input log  : False
Timestamping   : False
State          : active
logstate
1
2
3
4
5
6
Filename       : ipython_log.py
Mode           : rotate
Output logging : False
Raw input log  : False
Timestamping   : False
State          : active
logoff
1
Switching logging OFF
logstate
1
2
3
4
5
6
Filename       : ipython_log.py
Mode           : rotate
Output logging : False
Raw input log  : False
Timestamping   : False
State          : temporarily suspended
logon
1
Switching logging ON
logstate
1
2
3
4
5
6
Filename       : ipython_log.py
Mode           : rotate
Output logging : False
Raw input log  : False
Timestamping   : False
State          : active

Magic & Help

List, help.

lsmagic
1
2
3
4
5
6
7
Available line magics:
%alias  %alias_magic  %autocall  %automagic  %autosave  %bookmark  %cat  %cd  %clear  %colors  %config  %connect_info  %cp  %debug  %dhist  %dirs  %doctest_mode  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %lf  %lk  %ll  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %lx  %macro  %magic  %man  %matplotlib  %mkdir  %more  %mv  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %popd  %pprint  %precision  %profile  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %rep  %rerun  %reset  %reset_selective  %rm  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.

Open a window about magic commands.

magic

Open a window about the command (minimum, maximum help).

%alias?
%alias??

Function

docstrings, info, source.

def print_number(nb):

    """
    The docstring. print_number prints Hi!
    """
    print("Hi!" * nb)
print_number(1)
print_number(5)
1
2
Hi!
Hi!Hi!Hi!Hi!Hi!
pdef print_number
1
 print_number(nb)

Open a window about the docstrings.

pdoc print_number

Open a window about the function.

pinfo print_number

Bookmark, history.

bookmark -l
1
2
3
Current bookmarks:
name     -> path
untitled -> /home/user

Add a bookmark.

bookmark aaa path
bookmark -l
1
2
3
4
Current bookmarks:
aaa      -> path
name     -> path
untitled -> /home/user

Delete a bookmark.

bookmark -d aaa
bookmark -l
1
2
3
Current bookmarks:
name     -> path
untitled -> /home/user

Print the directory navigation history.

dhist
1
2
Directory history (kept in _dh)
0: /home/user

External

Bash, script.

The following is an example of one bash command.

!pwd
1
/home/user
abc = !pwd
abc
1
['/home/user']
alias
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
Total number of aliases: 12

[('cat', 'cat'),
 ('cp', 'cp'),
 ('ldir', 'ls -F -o --color %l | grep /$'),
 ('lf', 'ls -F -o --color %l | grep ^-'),
 ('lk', 'ls -F -o --color %l | grep ^l'),
 ('ll', 'ls -F -o --color'),
 ('ls', 'ls -F --color'),
 ('lx', 'ls -F -o --color %l | grep ^-..x'),
 ('mkdir', 'mkdir'),
 ('mv', 'mv'),
 ('rm', 'rm'),
 ('rmdir', 'rmdir')]

Invoke an external script into IPython.

run print_text.py
1
Hi!

Invoke an external script into IPython and open a window about the script’s profile.

run -p print_text.py
1
Hi!

Load the external script into IPython (load print_text.py turns into # %load print_text.py).

# %load print_text.py
def print_text():

    """
    The docstring. print_text prints Hi!
    """
    print("Hi!")

print_text()