In this article, we would focus on how to use Jupyter Notebook for writing Python programs. Jupyter Notebook provides us a REPL (read-evaluate-print-loop) shell. The shell opens inside a web browser. Furthermore, it supports numerous programming languages. Notebook server is one of three main components of Jupyter Notebook. The other two are – browser and kernel. We can edit, save and load notebook files with the help of Notebook server. If you want to read about how to install and setup Jupyter Notebook for Python in Ubuntu, then read the article here.
Use Jupyter Notebook for writing Python programs
To run Jupyter Notebook –
Either open a terminal and issue the following,
or, you can open a web browser and visit the following link
To list all the running notebook files, click on Running tab and click on Shutdown to close the running notebooks.
In the right-hand corner, you would see a drop-down button New. Click on New and then select Python 3. It will open a new notebook tab with title – untitled.ipynb.
In new tab, save the notebook file by clicking File menu and then choose Save As – <file-name>.
Working with Jupyter notebook
In [ ] represents an empty cell. In is short for Input.
We write code in cells of notebook. Click Run button (or, Shift+Enter) to get the output of the written code.
For instance, type 4*3 in an empty cell and it would return with the output 12 in next cell.
Furthermore, for all the keyboard shortcuts available – click Help –> Keyboard Shortcuts.
Next, we can save/export our notebook in different file formats – click File –> Download as
A notebook file can be saved in file formats of – AsciiDoc, HTML, LaTex, Markdown, Notebook, PDF, reST, Python & Reveal .js slides.
Note: If you try to open a file that’s not available in current working directory then, you would move to the directory where file is stored. Also, current working directory is the directory where all your work is stored. You could navigate between directory structure from the Jupyter Notebooks’ dashboard itself.
For instance, all the files are saved in /home/<user-name> directory. If you would want to save/load files to/from Downloads directory then click on Downloads. Alternately, you can also Upload files through Upload button. But, uploading a file in will make a copy of the file in current working directory.
In conclusion, we have discussed how to work with Jupyter Notebook for writing Python programs.