{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# A brief introduction to Jupyter Notebooks for Python 3." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a Jupyter notebook. **Python** is a programming language, and a **Jupyter notebook** is an interactive document which contains text (like what you're reading now) and Python code. Jupyter notebooks will be used throughout the tutorial, and this is a brief introduction to the interface.\n", "\n", "## Table of Contents\n", "\n", "1. [Notebook cells](#cells)\n", "2. [Markdown syntax](#markdown)\n", "3. [Saving, loading, printing](#files)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Notebook cells" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Click inside the cell below, in the box to the right of the `In []:` prompt. A border should appear around the cell, with a green bar at the left, indicating that it's active. Then press *shift-Enter* (hold down the shift key while pressing the Enter key) on the keyboard to **evaluate** the cell. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2+2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If an output message like `Out[1]: 4` appeared, then everything is working correctly!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Jupyter notebooks are built out of **cells**. There are two primary types of cells: **Markdown cells** and **Code cells**. Textual content will be contained in Markdown cells like this one. Programming content will be contained in Code cells, like the `2+2` cell above.\n", "\n", "If a cell has a green border, that means that you're currently editing it. If it has a blue border, that means that the cell is selected (and can be cut and pasted, etc.) but not in edit-mode. Otherwise it is not active.\n", "\n", "To edit a Code cell, click anywhere within its evaluation box. For example, you can click inside the `2+2` cell, change the numbers to `2+3` and re-evaluate with *shift-Enter*. \n", "\n", "To edit a Markdown cell like this one, double-click anywhere within its body. You can write Markdown just like any text document, for the most part. When you're done editing a Markdown cell, press *shift-Enter*.\n", "\n", "To change a selected cell from Code to Markdown or back again, you should see a dropdown menu with \"Markdown\" or \"Code\" visible, just above your notebook interface. Just drop down to select the type of the cell. For practice, select the cell below to a Markdown cell, and write a sentence or two. Complete it with a *shift-Enter*." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Change this cell to a Markdown cell, and write something." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, change the cell below to a Code cell, and use it to compute `999 * 999`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(I'm a markdown cell. Change me to a Code cell and compute `999*999`)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Often you'll want to **insert a new cell** above or below a given cell. To do this, you need to select the cell in one of the following ways. Select a Code cell with a single click **outside** of its evaluation box, e.g., click where it says `In [ ]:`. Select a Markdown cell with a single-click anywhere in its body.\n", "\n", "If you have selected the cell correctly, a blue border should be visible around it. Once the blue border is visible, you can add a new cell **above** it by pressing the **a** key on the keyboard. You can add a new cell **below** it by pressing the **b** key on the keyboard. Try to add a new cell above and below the Markdown cell below. The newly created cells are probably Code cells by default.\n", "\n", "Instead of **a** or **b**, you can also press **x**. That will delete the cell, so be careful and save often." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(I'm a markdown cell. Add a new cell above and below me.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A quick note: there are a few **invisible cells** in this notebook, and in other notebooks in the tutorial. In fact, one of these sneaky cells is just below this one! It's a Markdown cell, as you will see if you double-click it. Its content is ``, which is a snippet of HTML code. This is a little placeholder, which enables the links in the table of contents. Don't change those Markdown cells, unless you want to break the links in the table of contents." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Markdown for Jupyter notebooks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You will learn more about Python code in the tutorial. As you are learning, you should experiment in code cells, and write answers to questions and other notes in markdown cells. Markdown is a neat text-editing language, a sort of shorthand that extends HTML. Here is a brief guide to typing in Markdown." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Most of the time, you can type Markdown just like any old text document. One little difference is that to start a new line, you have to insert an extra empty line in the Markdown. Double-click this cell to see how the new paragraph was started below. \n", "\n", "Markdown has many more features that you'll notice in this document: there are different typefaces, lists, headings, links, and more. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Typefaces\n", "\n", "To change from plain text to *italics* or to **boldface**, you surround the text by asterisks or double-asterisks. Double-click inside this Markdown cell to see how it was done! Then insert a new Markdown cell below this one to experiment. Strictly speaking, the single-asterisks are used to convert to the HTML `` tag, which places emphasis on text. The double-asterisks convert to the HTML `` tag, which is often boldfaced. But different web-browsers behave differently." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lists\n", "\n", "Often it is useful to make lists. The two kinds of lists we'll use are numbered lists and unnumbered lists. These are easy in Markdown. For a numbered list, just start each new line with a number. Double-click this cell to see how it was done.\n", "\n", "1. This is the first line.\n", "2. This is the second line.\n", "3. This is the third line." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For an unnumbered (bulleted) list, start each new line with a dash `-` (a few other symbols work too). Double-click this cell to see how it was done.\n", "\n", "- This is the first line.\n", "- This is the second line.\n", "- This is the third line.\n", "\n", "Now I'm back to ordinary text again." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can nest your lists. But people get too attached to nested hierarchies in their writing and presentations. Lists have their place, but paragraphs should be more common. If you must nest a list, double-click this cell to see how it was done, with a bit of indentation. \n", "\n", "- This is the first line.\n", " - This is the first subline within the first line.\n", " - This is the second subline within the first line.\n", "- This is the second line.\n", "- This is the third line.\n", "\n", "Now I'm back to ordinary text again." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Headings\n", "\n", "There are different levels of headings, i.e., headings, subheadings, etc., which typically render as boldface text of different sizes. To make a top-level heading, a single hashtag `#` is used. The next level heading is created with a double hashtag `##`, and so on. Double-click this cell to see how it was done.\n", "\n", "# A top-level heading.\n", "## The next-level heading.\n", "### A third-level heading.\n", "#### A fourth-level heading. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Links\n", "\n", "Finally, we discuss how to insert web-links in your Markdown code. For example, here is a [link to the webpage to buy An Illustrated Theory of Numbers](http://bookstore.ams.org/mbk-105). The general syntax is that the displayed text goes in brackets followed by the URL (web address) in parentheses. Double-click this cell to see how it was done." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Conclusion\n", "\n", "There is much much more you can do with Markdown. In fact, Markdown is a superset of HTML, so you can put just about any basic web content in a Markdown cell. A quick google search will turn up lots of Markdown guides, and it's best to search for Markdown and Jupyter to find a guide for Jupyter's particular flavor of Markdown (which differs a bit from WordPress markdown, for example). " ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Saving, loading, and printing notebooks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can save, load, and print Jupyter notebooks. In fact, it's probably a good time to save this notebook since you've edited the markdown so far. Go to \"File...Save and Checkpoint\" (the File menu just under \"Jupyter\") to save your notebook so far.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The standard notebook file format is `.ipynb` which stands for \"iPython Notebook\", since Jupyter used to be called iPython. If you go to \"File...Download as...\", then \"IPython Notebook\" will be the top choice. You will always be able to reload your notebook and edit it, if you save it as an iPython Notebook.\n", "\n", "You may also want to save a copy as an `.html` file. HTML is the code used to render webpages, and so it's good for sharing with anyone with a web browser. Also, your web browser can probably print the HTML file nicely, so it's a good format for printing. You can go directly to \"File...Print preview\" to see what the notebook will look like when printed.\n", "\n", "To open a Python notebook, you can go to \"File...Open\" or you can return to your original Jupyter tab (if it's still there) and browse directories to find notebook files. But Jupyter must be started to load python notebooks on your computer.\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One more tool to learn about is [GitHub](https://github.com/). GitHub is a website which allows users to share and develop software of all sorts. It's used for collaborative software development, and has many desirable features like \"versioning\" (keeping track of versions along the way as various parties make edits). You can freely use GitHub to store your Python notebooks. GitHub takes a bit of practice, but it's worth the effort in the long run. \n", "\n", "The tutorial notebooks are stored on GitHub for a few reasons. First, they can be easily edited and improved. Second, they become freely and publicly available. Go to [my GitHub page](https://github.com/MartyWeissman/Python-for-number-theory) to see them! Third, even though they are stored in `.ipynb` format, anyone can view Python notebooks on the web using the [Jupyter Notebook Viewer](https://nbviewer.jupyter.org/). For example, you can use the Jupyter Notebook Viewer to see [this notebook online](https://nbviewer.jupyter.org/github/MartyWeissman/Python-for-number-theory/blob/master/My%20First%20Python%20Notebook.ipynb)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now you know more than enough to begin the Python tutorials. Download the notebooks and **happy programming**!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 1 }