Skip to content

Instantly share code, notes, and snippets.

@jjst
Last active July 17, 2021 00:13
Show Gist options
  • Save jjst/609058baba89d0613ccc9e6605e05dfe to your computer and use it in GitHub Desktop.
Save jjst/609058baba89d0613ccc9e6605e05dfe to your computer and use it in GitHub Desktop.
ipython install & cheat sheet

Getting started

Install

pip install ipython

If you also wanna use the notebook:

pip install jupyter

Launch with:

ipython

Exit with quit(), exit() or Ctrl+D (faster)

Wat is dis

Python has a built-in REPL (read-eval-print loop) that allows to type and execute python code in an interpreter. It is accessed by running the python command with no argument.

However the built-in interpreter has a number of limitations.

ipython is a python REPL "on steroids", packed with a lot more features and useful shortcuts.

ipython vs jupyter

jupyter is the new name of the umbrella project for ipython. Jupyter is language agnostic, and can use different frontends (the most common ones are the console and the notebook) and different kernels, which provide language specific implementations for python, ruby, haskell, julia... ipython refers to the combination of the python kernel and the shell mode for jupyter.

Where to start

  • Type ? in the ipython shell for a good quick intro to ipython's feature.
  • Go through the tutorial
  • When using ipython, use %quickref for quick access to a built-in cheat sheet.

Cheat sheet

Here is an opinionated cheat sheet of some of ipython's features, in order of usefulness. Most of those work in the jupyter notebook as well!

Tab completion and syntax highlighting

Use tab for tab completion. Syntax highlighting is available for ipython/jupyter 5.0+ (coming soon out of beta).

History

  • Use up/down arrows to navigate previous commands. This works across ipython sessions!
  • Use Ctrl+R to search through history (like in bash/etc)
  • _<i> can be used to refer to the <i>th result. For example:
In [37]: 24 * 3600
Out[37]: 86400

In [38]: _37 * 31
Out[38]: 2678400
  • _, __, and ___ can be used to refer to the last 3 results:
In [37]: 24 * 3600
Out[37]: 86400

In [38]: _ * 31
Out[38]: 2678400

Help

Use ? at the beginning or the end of an object or function to get help, including docstring. Example:

In [59]: range?
Docstring:
range(stop) -> list of integers
range(start, stop[, step]) -> list of integers

Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!
These are exactly the valid indices for a list of 4 elements.
Type:
builtin_function_or_method
  • Use ?? at the beginning or the end will show more info, including the source code if available.

Magic functions

Magic functions commands specific to ipython that provide useful shortcuts. They are prefixed with %. Use %magic for the magic function reference.

The % can be omitted when there's no ambiguity with a python function/keyword.

  • %timeit: measure how long a piece of code takes to run
In [6]: %timeit fac(500)
The slowest run took 5.42 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 169 µs per loop
  • %edit <i>: edit <i>th entry in default $EDITOR
  • %edit -p: reopen the previously edited script
  • %paste/%cpaste: paste code from the clipboard (escapes >>> automatically!)
  • %pastebin <i>: upload code from cell <i> to gist
  • Automatically reload a module when it gets changed on the filesystem:
%load_ext autoreload
%autoreload 2

Running shell commands

Use ! to escape the ipython shell and run raw shell commands instead:

In [32]: myfiles = !ls

In [33]: [f for f in myfiles if f.endswith('.sh')]
Out[33]: ['test.sh', 'unicorn-daemon.sh']

If there is no ambiguity with python functions/objects of the same name, ! can be omitted.

Auto-parens/autoquoting

  • Prefix a function with / to call it without parenthesis and with spaces between arguments
In [34]: range(20,30)
Out[34]: [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]

In [35]: /range 20 30
Out[35]: [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
  • Prefix a function with , or ; to automatically quote arguments: , quotes all arguments separately, ; quotes as one big argument
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment