{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to Jupyter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "This chapter authored by [Todd M. Gureckis](http://gureckislab.org/~gureckis) is released under the [license](/LICENSE.html) for the book. Several of the sections on how to use the Jupyter Interface were developed and shared by Jessica Hamrick as part of a course developed at UC Berkeley.\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Video Lecture\n", "\n", "This video provides an complementary overview of this chapter. There are things mentioned in the chapter not mentioned in the video and vice versa. Together they give an overview of this unit so please read and watch.\n", "\n", "
Ctrl-Enter
(which will keep the cell selected), or Shift-Enter
(which will select the next cell).\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Try running the following cell and see what it prints out:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print(\"Printing cumulative sum from 1-10:\")\n",
"total = 0\n",
"for i in range(1, 11):\n",
" total += i\n",
" print(\"Sum of 1 to \" + str(i) + \" is: \" + str(total))\n",
"print(\"Done printing numbers.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You'll notice that the output beneath the cell corresponds to the `print` statements in the code. Here is another example, which only prints out the final sum:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"total = 0\n",
"for i in range(1, 11):\n",
" total += i\n",
"print(total)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Another way to print something out is to have that thing be the last line in the cell. For example, we could rewrite our example above to be:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"total = 0\n",
"for i in range(1, 11):\n",
" total += i\n",
"total"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However, this *will not work* unless the thing to be displayed is on the last line. For example, if we wanted to print the total sum and then a message after that, this will not do what we want (it will only print \"Done computing total.\", and not the total sum itself)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"total = 0\n",
"for i in range(1, 11):\n",
" total += i\n",
"total\n",
"print(\"Done computing total.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you are accustomed to Python 2, note that the parentheses are obligatory for the `print` function in Python 3."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The IPython kernel"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When you first start a notebook, you are also starting what is called a *kernel*. This is a special program that runs in the background and executes code (by default, this is Python, but it could be other languages too, like R!). Whenever you run a code cell, you are telling the kernel to execute the code that is in the cell, and to print the output (if any).\n",
"\n",
"Just like if you were typing code at the Python interpreter, you need to make sure your variables are declared before you can use them. What will happen when you run the following cell? Try it and see:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The issue is that the variable `a` does not exist. Modify the cell above so that `a` is declared first (for example, you could set the value of `a` to 1 -- or pick whatever value you want). Once you have modified the above cell, you should be able to run the following cell (if you haven't modified the above cell, you'll get the same error!):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print(\"The value of 'a' is: \" + str(a))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Running the above cell should work, because `a` has now been declared. To see what variables have been declared, you can use the `%whos` command:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%whos"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you ran the summing examples from the previous section, you'll notice that `total` and `i` are listed under the `%whos` command. That is because when we ran the code for those examples, they also modified the kernel state."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(Note that commands beginning with a percent (%) or double percent (%%) are special IPython commands called *magics*. They will **only** work in IPython.)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Restarting the kernel"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is generally a good idea to periodically restart the kernel and start fresh, because you may be using some variables that you declared at some point, but at a later point deleted that declaration. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```{warning}\n",
"Your code should always be able to work if you run every cell in the notebook, in order, starting from a new kernel.\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To test that your code can do this, first restart the kernel by clicking the restart button:\n",
"\n",
"\n",
"```{figure} ./images/restart-kernel-button.png\n",
"---\n",
"height: 40px\n",
"name: kernel-restart\n",
"---\n",
"The button to restart the kernel\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then, run all cells in the notebook in order by choosing **Cell$\\rightarrow$Run All** from the menu above."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```{admonition} Keyboard Shortcuts\n",
":class: tip\n",
"There are many keyboard shortcuts for the notebook. To see a full list of these, go to Help$\\rightarrow$Keyboard Shortcuts.\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```{admonition} User Interface Tour\n",
":class: tip\n",
"To learn a little more about what things are what in the IPython Notebook, check out the user interface tour, which you can access by going to Help$\\rightarrow$User Interface Tour.\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Markdown and LaTeX Cheatsheet"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As mentioned above, Markdown is a special way of writing text in order to specify formatting, like whether text should be bold, italicized, etc.\n",
"In a homework you will get some practice with this. However, you can always come back and use the following website as a reference: [https://help.github.com/articles/markdown-basics](https://help.github.com/articles/markdown-basics)\n",
"\n",
"- Hint #1: after editing the Markdown, you will need to run the cell so that the formatting appears.\n",
"- Hint #2: try selecting *this* cell so you can see what the Markdown looks like when you're editing it. Remember to run the cell again to see what it looks like when it is formatted.\n",
"\n",
"Markdown is a useful tool in Jupyter for making things look nice but you don't have to be a Markdown expert to succeed at this class."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Additional Resources\n",
"\n",
"This chapter gives you a basic introduction to the Jupyter Notebook system we will be using more or less daily in this class. It is something you will get more familiar with through experience. However, if you want additional resources here are some useful guides and videos introducing Jupyter:\n",
"\n",
"- [What is Jupyter Notebook? (youtube)](https://www.youtube.com/watch?v=q_BzsPxwLOE)\n",
"- [Jupyter Notebook: An Introduction (Mike Driscoll, RealPython.com)](https://realpython.com/jupyter-notebook-introduction/)\n",
"- [Project Jupyter homepage](https://jupyter.org)\n",
"- [List of other programming languages/kernels you can use with Jupyter](https://github.com/jupyter/jupyter/wiki/Jupyter-kernels)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## References\n",
"\n",
"```{bibliography} ../../references.bib\n",
":filter: docname in docnames\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernel_info": {
"name": "python3"
},
"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.7.3"
},
"nteract": {
"version": "0.15.0"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 1
}