What is IPython?

If you've previously downloaded Python 3, you probably started the Python 3 interpreter by typing the following command into your computer's terminal:

python3

Starting the Python interpreter allows you to run Python code within your terminal. Although this is helpful, the Python interpreter has its limitations. For example, the Python interpreter does not provide syntax highlighting, tab completion, proper indentation, and several other important features that a code editor would provide.

IPython is an alternative Python interpreter. It is an interactive shell used for computing in Python. It provides many more useful features over the more popular default Python interpreter. In this article, you'll learn how to get started with IPython3 and use some of its features.

Just like there are previous versions of Python, there is also a previous version of IPython. Make sure that you install and use IPython.

Where Can I Get IPython?

There are two ways to install IPython on your computer. If you already have Python 3 installed, you can use pip3 (Python's package manager) to install IPython using the following command:

pip3 install ipython

We suggest installing IPython using the Python Anaconda distribution. When you install Python via Anaconda, IPython will automatically be installed. If you install Python via Miniconda (a smaller version of Anaconda), you'll have to install IPython using conda (Anaconda's package manager for data science packages) with the following command:

conda install ipython

If you'd like to learn more about Python installation, please refer to the following Codecademy resources:

How Do I Use IPython?

Great! At this point, you should have IPython installed on your computer. Using IPython is not much different from using the default Python shell. To use it, type the following command in your computer's terminal:

ipython

After typing this command, your terminal will provide you with a few details about IPython, like the version, a description of IPython, and some commands you can enter to help you get started (?, %quickref, help, object?).

That's it! You've loaded the IPython interpreter! Let's take a look at some of its features.

What Are Some Features of IPython?

Now that you've installed IPython, you might be wondering: "How is this interpreter an improvement over the default interpreter?" In this section, we'll discuss a handful of features that make IPython much easier to use over its default counterpart.

  • Run Native Shell Commands

When you run an interpreter, the interpreter typically has its own built-in commands. These built-in commands often collide with native shell commands.

For example, if you were to start the Python interpreter (using python) and then type cd after the interpreter loads, an error would appear in your terminal. This error occurs because the Python interpreter does not recognize the command. The command cd is native to your computer's terminal, but not to the Python interpreter. IPython includes support for native shell commands like cd, ls, command history, and more.

  • Syntax Highlighting

One of the first things you'll notice about IPython is that it provides syntax highlighting, meaning it uses color to differentiate parts of Python code. Type x = 10 into the terminal and notice how IPython highlights the code with different colors. This syntax highlighting is an improvement over the default Python interpreter and makes code much easier to read in the terminal.

syntax highlighting in ipython

  • Proper Indentation

If you've used Python before, you know that it cares a lot about whitespace and indentation. IPython recognizes this and automatically provides you with proper indentation when you type Python code in the interpreter. Let's take a look at an example.

Start the IPython interpreter by typing ipython in your terminal.

Next, type the following code and press "Enter" (or "Return" on your keyboard):

for x in range(10):

Notice that after you press "Enter" (or "Return") on your keyboard, IPython automatically provides the required indentation (four spaces) on the next line of code.

On the next line, type the following code:

print (x)

To run the code, press "Enter" (or "Return") twice . (The second "Enter" informs IPython that you are typing code and instructs IPython to execute the code.)

Voila! You should see the following output:

0 1 2 3 4 5 6 7 8 9
  • Tab Completion

IPython also provides tab completion. Let's take a look at an example using Python's str module. As a refresher, recall that the str module provides some useful methods you can use on strings.

Start IPython in your terminal. Type the following code and then press "Tab" on your keyboard:

str.

After pressing tab, you should see a list of methods supported by the str module.

tab completion for the str module

You can use the up and down arrow keys on your keyboard to navigate through the methods and select the one you'd like to use, or you can begin typing the name of the method you want to use and complete it by using "Tab". This a vast improvement over the default interpreter!

  • Documentation

Tab autocompletion is useful because it provides you with the list of all possible methods a specific module contains. With such a vast array of options, however, you might wonder: "What does this method do?" Once again, IPython to the rescue!

Type the following command into IPython and press "Enter" (or "Return") on your keyboard:

str.capitalize?

You should see IPython provide you with the documentation for that method.

documentation for the string method

You can use ? at the end of nearly any command in IPython if you want more information on the command (or method, variable, etc.).

  • Pasting Blocks of Code

Last, but certainly not least, another useful feature of IPython is the ability to paste in large amounts of Python code directly into the interpreter. You can grab any block of Python code, paste it into IPython, and the result should be properly indented code in the IPython interpreter. Neat!

These are only a handful of features that IPython provides. If you'd like to learn more about IPython, we recommend the following resource:

What Is Jupyter?

IPython also provides you with the Jupyter Notebook. The Jupyter Notebook is a web application that allows you to create documents that contain executable code, formulas and equations, data visualizations, and more.

If you installed Python using Anaconda, you already have the Jupyter Notebook installed. Run the following command to start the notebook web application:

jupyter notebook

If you installed Python using Miniconda, you'll need to install the Jupyter Notebook using conda first, which you can do by typing the following command:

conda install jupyter

Next, start the Jupyter Notebook by running the following command (as previously described):

jupyter notebook

When you start the notebook, you'll notice that a tab will open if you have a web browser open. It will run the Jupyter Notebook on a local port, such as http://localhost:8888 (or some other port). You'll notice that the notebook will list out the contents of your computer in a directory format. You can create new "notebooks" by clicking "New" and then selecting "Python 2" (or the version of Python you have installed).

We won't explain all the details of the Jupyter Notebook in this article. But it is worth noting that with Jupyter, you can create shareable files that can support live code, charts, graphs, math, different forms of markup (Markdown, LaTeX, etc.), and much more.

To learn more about Jupyter Notebooks, visit the official Project Jupyter website:

Conclusion

IPython is an alternative Python interpreter that provides improvements over the default Python interpreter. These improvements include syntax highlighting, proper indentation, documentation, and much more. With IPython, you can also use Jupyter notebooks to create reports that contain live code, charts, and more. If you're serious about using Python for computing and data analysis, IPython is a phenomenal tool for you to use.

If you have any other questions about IPython, look at the following documentation. Good luck!