Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Getting Started with Jupyter Notebook

Jun 6, 2019 • 11 Minute Read

Foundation

Writing applications in Python sometimes requires quick code experiments. Whether it is a Data Science problem, a Machine Learning problem, or any other Python project, there are many instances when we have second thoughts about if our logic will work. At that moment you can sense a little quarrel in your head, “Should I create a new python file to test this or should I just place it in my current code and test it?” It takes a lot to write efficient code and, hence, testing a segment of code is very important before merging it with your application.

Jupyter Notebook (IPython Notebooks) is a great platform for doing a quick code experiment and also to record and share your analysis of code snippets with others. Jupyter Notebook is a web-based interactive computational environment for Programming. Being an open source project (currently under Google), Jupyter is language agnostic and supports execution environment in dozens of languages among which are R, Ruby, Python, and Haskel. In this guide we will learn about Jupyter Notebook, how to make it running on our system, examples showing the power of Jupyter notebook and lastly some tips to use Jupyter notebook effectively.

Like our native hard notebooks, which we used to share with others, a Jupyter notebook can be shared over the web. It is a document, following a versioned schema, and containing an ordered list of input/output cells which can contain code, text, headings, mathematics, plots, and rich media. A Jupyter notebook takes “.ipynb” as a file extension. Can you think of the format that Jupyter notebook is using to transfer “ipynb” document over the web? If “JSON” is the word that pops up in your head then you are right. JSON is a widely known data-interchange format which is used to transfer data from one end to another on the Web.

Jupyter Notebooks possess the ability to connect to many kernels, allowing programming in many languages. By default, Jupyter Notebook comes with the IPython kernel. It runs on web browsers and, hence, we can say it must be a server-client application. The application can run on a PC/Laptop without Internet access, or it can be installed on a remote server, where you can access it through the Internet.

Every application comes with two main components: User Interface and an Engine which runs under the hood and does all the operations and processing. In Jupyter, these are the kernels and a dashboard.

Yes, I meant ”Kernels”, as I mentioned earlier Jupyter facilitates multiple programming languages and for each programming language, there will be a different Kernel. Now the question arises what is a Kernel? A Kernel is a program that runs and introspects the user’s code.

The dashboard of the application not only shows you the notebook that you have made, and can reopen, but can also be used to manage the kernels. You’ll know which ones are running and can shut them down if necessary.

The popularity of Jupyter notebook is increasing drastically and has now become one of the favorite user interfaces for data scientists, programmers, and cloud computing. Major cloud providers have adopted the Jupyter Notebook, or derivative tools, as a frontend interface for cloud users. Google, Microsoft Azure, Amazon, IBM, etc.

Setting Up

Jupyter Notebook works on top of Python language, so you need to install Python on your system. If you are using Linux/Mac, python is already installed; if you are a windows user, then you can to install from here.

Now that you have python in your system, let's install Jupyter notebook. The obvious way to install Jupyter notebook is using pip.

      pip3 install jupyter
    

Use pip for python2

      pip install jupyter
    

Alternatively, You can install Jupyter notebook using Anaconda, which is a most popular python distribution software. Anaconda comes with tons of scientific python packages that you may need, and if you need to install more packages then you can install by Anaconda’s own installer tool called conda.

Note - If you don’t want to install Notebook and just want to use it online, click here to start online version and select ”Try in your browser”; and you are set.

Getting Started

Starting Notebook (regardless of the operating system) begins by starting the terminal/command prompt and typing:

      jupyter notebook <PATH>
    

Optional parameter is the home directory for Jupyter notebook. It can be left blank. You will be presented with a web-page where you can code and execute your code.

Executing the Code

You have three options for executing the code.

  1. Shift-Enter - run cell, select below
  2. Ctrl-Enter - run selected cells
  3. Alt-Enter - run cell and insert below

Code in Cells

      x = "Hello world !"
print(x)
    

As soon as you execute the cell, you get the output.

Output Window

You must have noticed that output got printed just below the code cell; one of the beautiful features of Jupyter notebook is that there's no separate console, rather it lives along with the Code.

Example 1

      import time, sys

for i in range(5):
print(i)
time.sleep(1)
    

Execute the code to see the output.

Output- You will notice that the output is displayed asynchronously, meaning you will see the output one piece at a time, not all at the end as I am making it sleep for one second.

Example 2

      for i in range(1000):
	print(5\*\*i)
    

Try executing the above code and observe the output. You might be wondering, ”How will Jupyter Notebook render the output if it is too long in width or height?” Let me remind you that it is running on Web, which means it automatically resizes the output area and you just have to scroll to view the output.

Basics of Markdown

As we discussed previously, you can make notes in Jupyter Notebook by using various stylings like Bold and Italic text, Tables, Lists, Heading, Media, Equations, etc. To use texts in Jupyter, we use the cell as a Markdown cell. To make life easier use shortcut “esc” and then “m” key.

Headings

Remember the

tag from HTML? Well, we have that tag in Jupyter as well. Try out below code as a Markdown cell. # Heading 1 ## Heading 2 ### Heading 3 #### Heading 4 Output

Equations

You can include mathematical expressions in your notebook.

A simpler one: Make markdown of following

      $$c = \sqrt{a^2 + b^2}$$
    

Output

Cool, huh?

Let's try a complex one: Run with no markdown

      from IPython.display import display, Math, Latex
display(Math(r&#39;F(k) = \int\_{-\infty}^{\infty} f(x) e^{2\pi i k} dx&#39;))
    

Output

Media

Everyone loves pictures and videos in their presentation or notes, me too. Jupyter notebook respects that and gives a feature to embed the media in our python code. Use markdown cell.

      <img src="dangerous-python.jpeg" />
    

You can put a video in as well. Try out the below code:

      <video controls src="foo.m4v">
    

Text formatting You can use various text formatting such as Bold, Italic, Underline, strikeout texts. I have listed a few examples that are most frequently used in day-to-day tasks. Try the below code using markdown cell.

      <B>Bold text</B>
<i>Italic text</i>
<u>Underline</u>
<s>Strike out</s>
<pre>
    I want this text to be displayed
            the way I am writing it.
</pre>
    

Lists When I prepare presentations for any meetings where I need to showcase the Code, Output then I often use Lists as well. Everyone loves bullet points, they are short, crisp, and easy to remember. Try the below code using markdown cell.

      **Unordered list**

* An asterisk(*) starts an unordered list
* This is another item in the list
+ or you can also use the (+) character
- or the (-) character to continue the list

**Ordered list**

1. This starts a list with numbers.
+  This will show as number "2"
*  this will show as number "3."
9. Any number, +, -, or * will keep the list going.
    * Use Tab (4 spaces) to make a sub-list
        1. keep indenting for more sub lists
    * Back to the second level
    

Tips and Tricks

I am pretty sure that you found Jupyter Notebook exciting and will use this for all your Python projects. And if you going to stick with Jupyter notebook, you might want to read this part, because now I will tell some tricks that make life very easier with Jupyter Notebook.

Keyboard Shortcuts

No matter which operating system you are using, everyone loves to handle the system with their keyboards. It saves time and you don’t have to put your other hand on the mouse each time you want to navigate. Jupyter Notebook respects that and, hence it gives you the power to customize the keyboard shortcuts in your own way. Which means you don’t have to read the manual of this application to learn about the shortcuts.

I can hear the word “Wow!”

Navigate to the Help menu and select “Edit keyboard shortcuts” and customize it the way you like.

Exporting Notebooks

When you are working with your Notebook, sometimes you need to share your work with other people, and in those situations, you can export your notebook and send it to them. Jupyter Notebook helps you to export your notebook in your different formats, you can choose your desired format.

  1. Notebook(.ipynb)
  2. Script(.txt)
  3. Markdown(.md)
  4. HTML(.html)
  5. Latex(.tex)
  6. pdf via Latex(.pdf)

So this is it. I hope you enjoyed this short guide in which we learned about Jupyter Notebook, Python kernel, Installing Jupyter in different operating systems, and working with Jupyter notebook. We saw how Notebook uses cells for different purposes like executing code, markdowns (Texts, Headings, Equations, etc.) and not to forget about asynchronous output sections along with the code. I believe that, after reading this guide, you will be motivated to use Jupyter Notebook for executing python scripts for your project. Please check out other Pluralsight guide for getting deeper into Python and Data Science and to see how Jupyter Notebook can be the most useful for you.

Gaurav Singhal

Gaurav S.

Guarav is a Data Scientist with a strong background in computer science and mathematics. He has extensive research experience in data structures, statistical data analysis, and mathematical modeling. With a solid background in Web development he works with Python, JAVA, Django, HTML, Struts, Hibernate, Vaadin, Web Scrapping, Angular, and React. His data science skills include Python, Matplotlib, Tensorflows, Pandas, Numpy, Keras, CNN, ANN, NLP, Recommenders, Predictive analysis. He has built systems that have used both basic machine learning algorithms and complex deep neural network. He has worked in many data science projects, some of them are product recommendation, user sentiments, twitter bots, information retrieval, predictive analysis, data mining, image segmentation, SVMs, RandomForest etc.

More about this author