So what is a IPython Notebook all about and how is it related to me learning Python? The IPython Notebook is simply the software or the code editor that we will be using to actually learn Python. This software will allow us to write Python code in our web browser. Note that the IPython Notebook is now called the Jupyter Notebook and now supports many other languages aside from Python. There is a lot more I could mention about this particular piece of software but all you need to know is in order to gain access to this software you simply need to do visit the following web page.
Once you get the site to open then you should see a screen similar to the figure below. This is basically our welcome screen. If you have made it to this point, good job!
A quick word about Python 2 versus Python 3:
As a new comer to the Python world you will soon get thrown into a civil war between which version of Python is the best. Some will say choose Python 2 and you will hear others say their preference of choice is Python 3. There has been a very slow transition from Python 2 to Python 3 and there are probably a bunch of reasons why this is the case. Overtime everyone will eventually switch to Python 3 so I suggest you start with Python 3 when using this book.
Let’s now navigate our mouse to the top-right area of our screen. We are going to click on the New drop-down menu and select the option called Python 3 as shown below.
After you click on the Python 3 option your screen should look like the figure below.
A few things to notice about this screen. In the top-right side of your screen we see Python 3 which is good since we are working with this version of Python. We also see a bunch of menu options that we will touch up on as they become useful to us. The most important thing to notice is the bottom of the page which is the large text box highlighted in green. This is where we are going to start coding in Python. Very Exciting!
The first thing I want to do is give our Notebook a title. To do this all you have to do is click on the Untitled label at the top of the page near the Jupyter logo as shown below.
Let’s call our Notebook “Try Python”. We type this in the Rename Notebook dialog box. Now press the OK button as shown in the figure below.
As you can see from the figure below we have just renamed our Notebook. Wasn’t that easy?
Don’t get too nervous, we are going to be taking baby steps. This next set of steps will show you how to write and execute Python code. Click on the rectangular text box in the middle of your screen and type “a = 5” as shown in the figure below.
Now if you press the enter button on your keyboard, what happens? Correct, nothing! All the Enter key does is move your cursor to the next line just like you would expect in a program like Microsoft Word.
Next, type “print(a)” in the following line as shown below. Notice that when you typed the open parenthesis “(“ first, then the close parenthesis “)” was automatically created for you. Jupyter tries to be helpful at times and this can get annoying but it is there to help you be more productive.
So let’s go over the code we have written thus far and explain what it means. We first created a variable named “a” and gave it a value that equals the number “5”. We then typed the function called “print” and passed it the variable we created earlier. The print function comes with Python and it just means to print whatever you put inside the parenthesis.
To run this code we press the SHIFT+ENTER keys right after the other. So press the SHIFT key and while still holding the SHIFT key, press the ENTER key. This will tell Jupyter you want to run the two lines of code we have written. Your output should look like the one in the figure below. Notice that after we ran our code, Jupyter created another rectangular text box right below our first one.
As you have already suspected the output of the Python code you wrote should print out the number 5. You could have also pressed the right arrow symbol (run button) on the menu as shown below but that would not make you look as cool, would it?
Besides code, the Notebook lets us write text in Markdown. What is Markdown? This is basically a way to write text similarly like you write text in Microsoft Word, but in a more programmatic way. Let me show you how.
The first thing we need to do is change our cell type from Code to Markdown. By cell, I mean that rectangular text box we just typed some Python code in. Change the cell type to Markdown as shown below and make sure your cursor is on the second cell before changing from Code to Markdown.
Now click on the second cell and type “# Hello World” as shown below. As you can see, the size and color of the text looks different than our first cell. This is just to help you realize that you are not writing Python code but are writing in Markdown.
Press SHIFT+ENTER to run the code and see if you get a similar output as shown in the figure below.
As you can see we just created some bold text. But what did the “#” pound sign mean? The pound sign is just Markdown syntax for Heading. The more pound signs you put in front of your text the smaller the font will be. Let’s try it. Double-click on the Hello World text and add two more pound signs as shown below.
Run the code by pressing the SHIFT+ENTER key and you should get the same output as shown below. As you can see we were able to control the size of the text by adding or subtracting pound signs. Pretty cool! But even cooler was that we just realized we can go back and edit code cells and re-run them as many times as we want. Give it a try. Change the first cell from a five to a ten and re-run the cell.
We can also write bullets, italicize text, and add links to our markdown cells but I’ll leave that for you to explore on your own. My goal was just to make you aware of this feature.
We can also move cells around and that can be done using the Jupyter menu we have been avoiding until now. Select the second cell, the one with the Markdown code in it, and press the up arrow as shown below.
After you press the up arrow, the first cell with Python code will move down to the second spot as shown below. This feature is very useful when you want to organize your Notebook a certain way.
I’m going to show you how to add cells using the command mode. What is the command mode? This is simply a mode that lets you use shortcut keys to accomplish some tasks. How do we enter the command mode? Very good question. When we are editing code, we are essentially in Edit mode. We know this by the pencil icon as shown in the figure below. This icon is located at the top-right side of your screen.
To get into Command mode just press the Esc button on your keyboard. When you do this the pencil icon will disappear letting you know you are no longer in Edit mode. Now click on the second cell, the one with Python code and press the Esc key. You are now in Command mode. Press the “A” button on your keyboard. This should have created a new cell above your Python cell as shown in the figure below.
Similarly if you want to add a cell below the current cell, in command mode, press the B button on your keyboard. Give it a try and see for yourself.
To delete cells all you have to do is select the cell you want to delete, go to command mode by pressing the Esc button and press the D key twice, one after the other. Remember that you know you are in command mode when you do not see the pencil icon on the top-right of your screen. Try to delete the empty cell we created. Remember to press the D key twice. You should now end up with a screen that looks like the one below.
If you are having trouble with the shortcuts or if they are just confusing to you, then please use the Jupyter menu. Hover over the menu options and you will see options to delete and insert cells as shown below.
Last but not least, it is time to save your work. Jupyter does a good job by automatically saving your work every now and then. This is helpful but since we are running the Jupyter software in a webpage, if you close the webpage, you will lose your work.
A good thing to note is that the file extension of a Jupyter Notebook is .ipynb and this extension’s acronym is IPython Notebook. Jupyter allows you to also save your work as a HTML page, as a PDF file, and even as a regular Python file. In my opinion, always save your work as an .ipynb file and then if you really want to, save it in any other format you would want.
Press the File menu option, select the Download as option, and click on the IPython Notebook (.ipynb) option as shown below.
It can be very confusing saving your Jupyter Notebook as an IPython Notebook. But just realize they mean the same thing.
As you can see we did not cover every menu item and all of the features of the Jupyter Notebook. We also did very little Python programming in this book. The reason is because I did not want to overwhelm you but to give you enough information to get started. You now know about the powerful software called the Jupyter Notebook and how you can use it to code in Python. You practiced writing and executing Python code using the Jupyter Notebook. We also briefly talked about Markdown and learned some keyboard shortcuts. I hope the information was clear and at a level that even someone with a zero programming background can follow. If you have any comments or see any mistakes in the book please send me an email at firstname.lastname@example.org and I’ll be happy to respond to you.