This is a notebook: a document that can contain rich text elements—headings, paragraph text, hyperlinks, mathematical symbols and embedded figures—and interactive code elements. The document uses "cells" to divide up the text and code elements: text is formatted using markdown, and code can be executed using the IPython kernel.
Markdown is an easy way to format text to be displayed on a browser. You can format headings, bold face, italics, add a horizonal line, hyperlink some text, add bulleted or numbered lists. It's so easy, you can learn it in minutes! The "Daring Fireball" (by John Gruber) markdown syntax page is a nice starting point or reference.
If you came to view this notebook following the link on the tutorial description, "The world of Jupyter" (itself, a markdown file), then you are seeing it rendered as a static webpage on the free nbviewer service. It is the simplest way to share a notebook with anyone: just host the notebook file (.ipynb
extension) online and enter the public URL to the file on the nbviewer Go! box. The notebook will be rendered like a static webpage: visitors can read everything, but they cannot interact with the code.
To interact with a notebook, you need the Jupyter Notebook App, either locally installed on your computer or on some cloud service.
If you want to see what the Jupyter Notebook App looks like, you can try it right now on this free service: https://try.jupyter.org. You should see the Dashboard, which shows a list of available files, like this:
Click on the New button on the top right, and choose "Python 3" from the pull-down options to get a notebook that is connected to the IPython kernel. You should get an empty notebook with a single empty code cell labeled In[ ]
, with a flashing cursor inside. This is a code cell. You can start by trying any simple mathematical operation (like a calculator), and type [shift] + [enter]
to execute it.
You can try any of the arithmetic operators in Python:
+ - * / ** % //
The last three operators above are exponent (raise to the power of), modulo (divide and return remainder) and floor division. Be careful that the division operator behaves differently in legacy Python 2: it rounds to an integer when the operands are integers. We use Python 3 and don't worry about this, because it gives the expected result, e.g., 1/2
gives 0.5
(not zero!).
Here's a simple example calculation in a code cell:
2 * 12
24
Typing [shift] + [enter]
will execute the cell and give you the output in a new line, labeled Out[1]
(the numbering increases each time you execute a cell). If you're at the end of the notebook, you will also get a new blank cell, where you can try more arithmetic operations, if you want. Or, you can change the type of cell from Code to Markdown with the button on the menu bar. See if you find that now, and then type some text into a markdwon cell. Try some markdown syntax!
In a code cell, you can enter any valid Python code, and type [shift] + [enter]
to execute it. For example, the simplest code example just prints the message Hello World!, like this:
print("Hello World!")
Hello World!
You can also define a variable (say x
) and assign it a value (say, 2), then use that variable in some code:
x = 2
print(x * "Hello World!")
Hello World!Hello World!
x * 12
24
If you followed the instructions above, you launched the Jupyter Notebook App on a cloud service called tmpnb
. As the prefix tmp
indicates, it gives you a temporary demo: as soon as you close the browser tab (or after a few minutes of inactivity), the kernel dies and the content you wrote is not saved anywhere. This is a free service, sponsored by the company Rackspace, just for trying something out.
You can work on notebooks and save your work using other cloud services. A long-running cloud offering is SageMathCloud. And most recently (in June 2016), Microsoft announced notebooks hosted on Azure cloud. In both cases, you need to create a free account to be able to save your work.
You learned about:
[shift] + [enter]
to execute,tmpnb
, andCloud notebook services (especially free ones!) have their limitations. As you start using notebooks more, you'll want to install Jupyter on your personal computer. For that, we recommend the free Anaconda distribution, which includes all the Python packages you may need (more than 700 packages are included!).
If you prefer a light installation (e.g., if your internet is slow), we can recommend Miniconda. This will include only conda
(the package manager) and its dependencies, and Python. You will have to install Jupyter separately, and also other basic Python libraries for numerical mathematics and data analysis, using the conda install
command.
We also recommend that you start with Python 3 right away—Python 2.7 is the legacy Python, and you should only need to use it if you have a lot of code written in the past that you need to work with.
(c) 2016 Lorena A. Barba. Free to use under Creative Commons Attribution CC-BY 4.0 License. This notebook was written for the tutorial "The world of Jupyter" at the Huazhong University of Science and Technology (HUST), Wuhan, China.