Python Program Demo 1: Using the Jupyter Notebook

Hello everybody,

It’s Michael, and here’s my last post for 2019. Today’s post will be a Python program demo on how to use the Jupyter Notebook in Python. I figured a Python program demo would be appropriate since I didn’t do one during my series of Python posts back in August, plus the Jupyter notebook is another great Python IDE (to refresh your memory, IDE stands for Integrated Development Environment, which is the tool you use to write and run code).

  • The word Jupyter is a portmanteau of the words Julia, Python, and R, which are the three programming languages that the Jupyter notebook was originally intended for; Jupyter notebooks now support many more languages. Just a little fun fact for you guys.
    • Also, I didn’t know there was a programming language called Julia. Apparently it’s another data science-oriented language.

Now, if you installed Anaconda, here’s where you would find the Jupyter notebook:

Screen Shot 2019-12-21 at 12.22.08 PM

If you want to launch the Jupyter notebook, you would first click on the icon that says Jupyter-not Jupyter Lab-which can be found on the Anaconda home page (which is the page that appears on this screenshot).

However, keep in mind that you won’t see the notebook right away when you click the launch button. Rather, the command prompt will open; this is because the Jupyter notebook isn’t automatically included with Anaconda, so you will need to install the Jupyter notebook via the command prompt using three simple words-pip install jupyter. You can swap pip for conda if your Anaconda installation came with an Anaconda command prompt (mine didn’t, but that could be because I’m using a Mac).

Once the installation is complete, type this URL to your browser-http://localhost:8888/tree. Once you’ve done so, you should see the Jupyter notebook’s landing page:

Screen Shot 2019-12-22 at 1.57.46 PM

  • Keep in mind that after you first install the Jupyter notebook, you will need to open the Jupyter notebook from the Anaconda home page whenever you want to use a Jupyter notebook in the future. You could also copy and paste this URL-http://localhost:8888/tree-in your browser, but you must have Anaconda open for this to work.
  • Another interesting tidbit about the Jupyter notebook is that even though it runs in a web browser, you can use the Jupyter notebook without Wi-Fi. This would come in handy if you want to work on some Python code if the internet is out at your place.

The home page for the Jupyter notebook simply shows all of your folders; your home page will look different depending on what folders you have in your computer.

Now, to create a new Jupyter notebook you would first click on the New button, then click on the Python 3 option in the dropdown. You could click any of the other 3 options (Text File, Folder, or Terminal) if you didn’t want a Python file, but for now, I’m going to focus on Python files.

  • Python 3 is the current version of Python (as of December 22, 2019), but depending on when you are reading this post, Python 3 could be long-deprecated by then.

Now, here’s what an empty Python file looks like:

Screen Shot 2019-12-22 at 2.11.04 PM

The blank text box is called a cell; this is the place where you would write and run your code. To run your code, click the run button at the top of the page.

  • If you have an error in your code, an error message would be displayed.
  • You can change the Untitled header to a name of your choice.

Now, here’s what a Python file looks like with some code:

Screen Shot 2019-12-22 at 2.23.58 PM

As you can see, I have some simple test Python code here. Everything ran just fine, but you might be wondering why the text Here is some test code isn’t in a cell. This is because I used a setting called Markdown, which you can see in the dropdown in the toolbar. There are four settings in the dropdown box-Code, Markdown, Raw NBConvert  and Heading-but the two I will use the most are Code and Markdown. Code formats the cells as regular, runnable Python code, while Markdown formats the cells as regular text. Markdown is good if you want to write notes with your code (OK, you can also use the hashtag/pound sign besides lines of code to denote comments, but Markdown makes for readable notes).

Now here’s what the Jupyter notebook homepage looks like with this test file:

Screen Shot 2019-12-22 at 5.36.26 PM

Regarding this picture, here are two important things to know:

  • The green icon means that the notebook is currently running. To shut the notebook down, click on the checkbox to the left of the icon, then click the Shutdown button that appears after you click the checkbox.
    • For the most part, the Jupyter notebook will save your code automatically, but just in case, click the floppy disk icon before you exit the Python file to ensure that your code will be saved (after all, technology isn’t perfect)
  • Test File is saved with a .ipynb extension, which is the extension that all Jupyter notebook files use. One thing to note is that even though .ipynb files will show up amongst all of your other files, you can only use the Jupyter notebook to open files with this extension.

Personally, I think the Jupyter notebook is a neat tool, but I also like Spyder. Jupyter notebooks will work especially well if you’re doing data analytics with Python (Python data analytics series coming in 2020). However, I’d stick to Spyder if I’m trying to make something like an app or a game.

Thank you all for reading and following this blog in 2019. Hope you all have a great holiday season, and I can’t wait to give you more amazing programming/data analytics content in 2020 (yes, just 9 days left in this decade. Crazy stuff).

Happy holidays and see you all in 2020,

Michael

 

 

 

Leave a Reply