IPython complete tutorial in a nutshell

ipython

What is IPython?

IPython project began in 2001 by Mr. Fernando Perez’s to make a better interactive Python interpreter. Like the regular Python shell, IPython is a much advanced Python Interpreter which is one of the most important tools in the modern scientific computing.  IPython has a execute-explore workflow instead of edit-compile-run workflow of any other programming languages. IPython also provide a very tight integration with the operating system’s shell and file system.

IPython also features an interactive GUI console with inline plotting for data science projects and also a web-based interactive notebook for programming. IPython features a light-weight and fast parallel computing engine. In IPython when typing just a variable, it renders a string representation of the object. Many kinds of Python objects are formatted to be more readable, or pretty-printed.

Important features of IPython:

Tab Completion

In IPython while we type-in the expressions in the shell, pressing <Tab> will search the namespace for any variables matching the characters which have been typed so far. Pressing <Tab> also shows you the methods and attributes on any objects or modules.

Object Introspection

Typing a ? mark before or after a variable will display some general information about the object.

Notably if object is a function or instance method, the docstring, if defined, will also be shown.

We can also use wildcard * to get the list of all names matching the wildcard expression using object introspection method. Suppose we want to get the list of functions associated with datetime module of python we could use the following expressions in IPython.

The %run Magic Command

The %run magic command will run in the IPython shell any external python file. This is very helpful as you can store the complete set of python code in a separate text editor and during running or checking the output you could use %run magic commands in IPython to execute these codes.

In the above snapshot the test_code.py contains the following codes

1
2
3
4
a = 2
b = 3
sum = a + b
print(sum)

It is to be noted that while using %run magic commands to run external python files, all of the variables defined in the file will then be accessible in the IPython shell

Code execution from Clipboard

Suppose we want to test a small bit of code in between the actual program we could use the %paste and %cpaste magic functions. Both %paste and %cpaste magic functions have similar in nature with only one difference that while using %cpaste “:” prompt is used for pasting the code into.

Using %cpaste magic commands has an advantage that you can paste as much code as you like before actually executing the pasted code. It also helps you to re-look the pasted code before actually executing the same. If you accidentally pasted the wrong code, you can break out of the %cpaste prompt by pressing <Ctrl-C>

Magic Commands

IPython has a various list of special commands which are designed specially to facilitate common tasks and enables you to easily control the behavior of the IPython system. We have introduced you with many of the magic commands like %run, %paste and %cpaste. A list of magic commands could be found by running %magic or %quickref magic command sin the IPython shells

Qt-based Rich GUI Console

The IPython team has developed a Qt framework based GUI console having several features like embedded image, multiline editing and syntax highlighter. For running the Qt framework based GUI console you need to install Qt framework first.

With Qt framework based GUI console for IPython one can launch multiple IPython processes in tabs, enabling one to switch between tasks.

Type in the command $ipython qtconsole to open and run the Qt based GUI console for IPython.

IPython HTML Notebook

IPython team had introduced a web based interactive coding platform that is commonly known as jupyter notebook. Earlier it was called IPython Notebook, but due to huge popularity of this notebook in other languages as well this was maintained separately by the name jupyter notebook.

Jupyter notebook has grown into a wonderful tool for scientific computing and research. It has JSON based .ipynb document format for easy sharing among the users.

IPython Notebook could be run by typing the command “$ipython notebook”. Once running this command your primary web browser will automatically open up to the notebook dashboard.

I have tried to introduce only the basic features of IPython, there are many advanced features of IPython interactive console which makes IPython as one of the best Python Interpreter. For further learning or exploring more about the IPython you can follow the official tutorial of IPython.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *