Commit 5ca4d9f2 authored by Arucas Chacon's avatar Arucas Chacon

Completed notebooks

parent 2820c217
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Jupyter Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![jupyter](images/jupyter-logo-2.png)\n",
"\n",
"![IPython](images/ipython_logo-s.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## How can I run Python programs?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* **.py** extension, plain-text Python programs. \n",
"* **.ipynb** extension to distinguish them from plain-text Python programs. We'll be able to mix: **code, documentation and graphics** in the same file. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Jupyter Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The <a href=\"https://docs.anaconda.com/anaconda/install/\" target=\"_blank\">Anaconda package manager</a> is an automated way to install the Jupyter notebook.\n",
"* See the <a href=\"https://swcarpentry.github.io/python-novice-gapminder/setup/\"> setup instructions</a> for Anaconda installation instructions."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can start the notebook server from the command line (Terminal on Mac/Linux, CMD prompt on Windows) by running the following command: \n",
"\n",
" <div class=\"alert alert-info\" role=\"alert\">jupyter-notebook</div> \n",
"\n",
"This will print some information about the notebook server in your terminal, including the URL of the web application (by default, `http://127.0.0.1:8888`). It will then open your default web browser to this URL.\n",
"\n",
"When the notebook opens, you will see the **notebook dashboard**, which will show a list of the notebooks, files, and subdirectories in the directory where the notebook server was started. This will be your work directory."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Open a new notebook from the dropdown menu (that says ‘New’) in the top right corner of the file browser page.\n",
"* Each notebook contains one or more cells that contain code, text, or images."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Modal editor"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Jupyter Notebooks have a modal user interface. This means that the keyboard does different things depending on which mode the Notebook is in. There are two modes: *edit* mode and *command* mode."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* If you press “ESC” and “ENTER” alternately, the outer border of your code cell will change from grey/blue to green.\n",
"* These are the <span class=\"label label-info\">command (grey/blue)</span> and <span class=\"label label-success\">edit (green)</span>\n",
" modes of your notebook.\n",
"\n",
"* Command mode allows you to edit notebook-level features, and edit mode changes the content of cells.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Edit mode\n",
"\n",
"Edit mode is indicated by a green cell border and a prompt showing in the editor area:\n",
"\n",
"<img src=\"images/edit_mode.png\">\n",
"\n",
"When a cell is in edit mode, you can type into the cell, like a normal text editor.\n",
"\n",
"<div class=\"alert alert-success\">\n",
"Enter edit mode by pressing `ENTER` or using the mouse to click on a cell's editor area.\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a = 10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Command mode\n",
"\n",
"Command mode is indicated by a grey cell border with a blue left margin:\n",
"\n",
"<img src=\"images/command_mode.png\">\n",
"\n",
"When you are in command mode, you are able to edit the notebook as a whole, but not type into individual cells.\n",
"\n",
"* In <span class=\"label label-info\">command mode</span>, pressing the “H” key will provide a list of all the shortcut keys.\n",
"\n",
"* When in <span class=\"label label-info\">command mode</span> (ESC/grey):\n",
" * The “B” key will make a new cell below the currently selected cell.\n",
" * The “A” key will make one above.\n",
" * The “X” key will delete the current cell.\n",
" * The “Z” key will undo your last cell deletion.\n",
" * The “M” key will will set the current cell as a Markdown cell.\n",
" * The “Y” key will will set the current cell as a Code cell.\n",
"* All actions can be done using the menus, but there are lots of keyboard shortcuts to speed things up.\n",
"\n",
"<div class=\"alert alert-danger\">\n",
"Don't try to type into a cell in command mode; unexpected things will happen!\n",
"</div>\n",
"\n",
"<div class=\"alert alert-success\">\n",
"Enter command mode by pressing `ESC` or using the mouse to click *outside* a cell's editor area.\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"panel panel-warning\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">Exercise - (2 mins)</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
" - In the Jupyter notebook, are you currently in command or edit mode? <br>\n",
" - Switch between the modes. Use the shortcuts to generate a new cell. Use the shortcuts to delete a cell\n",
" </div>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Cell types"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We have different types of cells. The type of a cell is selected from the dropdown menu or with a keyboard shortcut in command mode. We'll see `code` cells and `Markdown` cells. \n",
"\n",
"This is a **Markdown** cell itself and is used to add formatted text to your notebooks. You can use both [Markdown](https://en.wikipedia.org/wiki/Markdown) syntax and [LaTeX](https://en.wikipedia.org/wiki/LaTeX) syntax.\n",
"\n",
"You can include mathematical expressions both inline: $e^{i\\pi} + 1 = 0$ and displayed:\n",
"\n",
"$$e^x=\\sum_{i=0}^\\infty \\frac{1}{i!}x^i$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Code cells** on the other hand allow us to input Python code which will be later run by the kernel. \n",
"\n",
"Run a code cell using `Shift-Enter` or pressing the <button class='btn btn-default btn-xs'><i class=\"icon-play fa fa-play\"></i></button> button in the toolbar above. Once the cell is run (see below how), the corresponding output, if any, apperas below the cell."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This is a code cell. It runs code :-)\n",
"a = [1., 4., 3.14]\n",
"print (a)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# And another code cell\n",
"a = 10\n",
"b= 2\n",
"a/b"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# a third one\n",
"a+b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"$e^x$\n",
"\n",
"$$e^x$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Running cells\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The keyboard shortcuts for running a cell, both in edit and command mode, are:\n",
"\n",
"* `Shift-Enter` runs the current cell and moves to the one below.\n",
"* `Alt-Enter` runs the current cell and inserts a new one below.\n",
"* `Ctrl-Enter` run the current cell and enters command mode in current cell.\n",
"\n",
"<div class=\"alert alert-success\">\n",
"Press `h` anytime in command mode for a keyboard shotcut list.\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tab completion & help (edit mode)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Tab completion**, especially for attributes, is a convenient way to explore the structure of any object you’re dealing with. Simply type in a code cell `object_name.<TAB>` to view the object’s attributes. Besides Python objects and keywords, tab completion also works on file and directory names.\n",
"\n",
"For getting an object **help**, type `object_name.<SHIFT+TAB>` and a tooltip with the object short help will open. Pressing `<TAB>` twice (`<SHIFT+TAB+TAB>`) the full object help will open. Doing it four times and the full object help will go into a new frame."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"\n",
"\n",
"\n",
"<div class=\"panel panel-primary\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">Key points</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
" <ul>\n",
" <li><span class=\"label label-info\">command (grey/blue)</span> (for the whole notebook, press `ESC`) and <span class=\"label label-success\">edit (green)</span> (for a cell, `ENTER`) modes.</li>\n",
" <li>If you remember the “ESC” and “h” shortcut, you will be able to find out all the rest.</li>\n",
" <li>A Jupyter **notebook** is composed by **cells**.</li>\n",
" <li>Cells can be **Markdown** (text cells accepting Markdown and LaTeX syntax), and **code** cells (for executing code).</li>\n",
" <li>Use tab completion (`object_name.TAB`) and inline help (`object_name.SHIFT+TAB`) extensively.</li>\n",
" </ul>\n",
"</div>\n",
"</div>\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## References \n",
"* [IAMC's github](https://github.com/iamc/2017-06-28-cfmehu-ScientificPython/blob/master/jupyter-quickstart.ipynb).\n",
"\n",
"* This tutorial was essentially based in [IPython's notebook-based documentation](http://nbviewer.ipython.org/github/ipython/ipython/blob/3.x/examples/Index.ipynb). Take a look at it for a more extensive introduction to IPython.\n",
"\n",
"* Official [Jupyter notebook begginer guide](http://jupyter-notebook-beginner-guide.readthedocs.org/en/latest/index.html#).\n",
"\n",
"* Official [Jupyter user manual](https://jupyter.readthedocs.io/en/latest/index.html)\n",
"\n",
"* [A gallery of interesting IPython Notebooks](https://github.com/ipython/ipython/wiki/A-gallery-of-interesting-IPython-Notebooks).\n",
"\n",
"* [IPython project page](http://ipython.org/)\n",
"\n",
"* [Jupyter project page](http://jupyter.org/)"
]
}
],
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Variables and assignment"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use variables to store values"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Variables are names for values.\n",
"* In Python the = symbol assigns the value on the right to the name on the left.\n",
"* The variable is created when a value is assigned to it.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"weight_kg = 55"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Variable names:\n",
" * can’t start with a digit\n",
" * can’t contain spaces, quotation marks, or other punctuation\n",
" * may contain an underscore (typically used to separate words in long variable names)\n",
"* Underscores at the start like __alistairs_real_age have a special meaning so we won’t do that until we understand the convention."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use print to display values"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Python has a built-in function called <span class=\"label label-default\">print</span> that prints things as text.\n",
"* To add a string to the printout, wrap the string in single quotations."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(weight_kg)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('weight in kg:', weight_kg)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Variables must be created before they are used."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(last_name)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-success\" role=\"alert\">\n",
"Variables defined in one cell exist in all other cells once executed, so the relative location of cells in the notebook does\n",
"not matter (i.e., cells lower down can still affect those above). Remember: Notebook cells are just a way to organize a program: as far as Python is concerned, all of the source code is one long set of instructions.\n",
"</div>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Variables can be used in calculations."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('weight in pounds:', 2.2 * weight_kg)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"weight_lb = 2.2 * weight_kg\n",
"print('weight in kilograms:', weight_kg, 'and in pounds:', weight_lb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use an index to get a single character from a string.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Strings are lists of characters.\n",
"* <span class=\"label label-success\">Indices start numbered from 0</span>.\n",
"* Use the position’s index in square brackets to get the character at that position."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"workshop_name = 'Software carpentry'\n",
"print(workshop_name)\n",
"print('the first character is',workshop_name[0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use a slice to get a substring.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* A part of a string is called a substring. A substring can be as short as a single character.\n",
"* We take a slice of a string by using <span class=\"label label-default\">[start:stop]</span>, where start is replaced with the index of the first element we want and stop is replaced with the index of the element just after the last element we want.\n",
"* Mathematically, you might say that a slice selects [start:stop), from start to stop-1\n",
"* Taking a slice does not change the contents of the original string. Instead, the slice is a copy of part of the original string.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(workshop_name[0:6])\n",
"print(workshop_name[2:10])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use the built-in function len to find the length of a string.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(len(workshop_name))\n",
"print(len(workshop_name[1:6]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Python is case-sensitive."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Python thinks that upper- and lower-case letters are different, so <span class=\"label label-default\">Name</span> and <span class=\"label label-default\">name</span> are different variables.\n",
"* There are conventions for using upper-case letters at the start of variable names so we will use lower-case letters for now."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use meaningful variable names."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"qwert = 55\n",
"poiu = \"Arucas'\"\n",
"print (poiu, 'weight is', qwert)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"panel panel-warning\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">Exercise - Swapping Values</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
" Fill the table showing the values of the variables in this program after each statement is executed.\n",
" </div>\n",
"</div>"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"# Command # Value of x # Value of y # Value of swap #\n",
"x = 1.0 # # # #\n",
"y = 3.0 # # # #\n",
"swap = x # # # #\n",
"x = y # # # #\n",
"y = swap # # # #"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"panel panel-warning\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">Exercise - Predicting Values</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
"What is the final value of <span class=\"label label-default\">position</span> in the program below? (Try to predict the value without running the program, then check your prediction.)\n",
"\n",
" </div>\n",
"</div>"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"initial = \"left\"\n",
"position = initial\n",
"initial = \"right\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"panel panel-warning\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">Exercise - Choosing a name</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
"Which is a better variable name, <span class=\"label label-default\">m</span>, <span class=\"label label-default\">min</span>, or <span class=\"label label-default\">minutes</span>? Why? Hint: think about which code you would rather inherit from someone who is leaving the lab:\n",
"\n",
" </div>\n",
"</div>\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"ts = m * 60 + s\n",
"tot_sec = min * 60 + sec\n",
"total_seconds = minutes * 60 + seconds"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"panel panel-primary\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">Key Points</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
" <ul>\n",
" <li>Use variables to store values.</li>\n",
" <li>Use print to display values.</li>\n",
" <li>Variables persist between cells.</li>\n",
" <li>Variables must be created before they are used.</li>\n",
" <li>Variables can be used in calculations.</li>\n",
" <li>Use an index to get a single character from a string.</li>\n",
" <li>Use a slice to get a substring.</li>\n",
" <li>Use the built-in function len to find the length of a string.</li>\n",
" <li>Python is case-sensitive.</li>\n",
" <li>Use meaningful variable names.</li> \n",
" </ul> \n",
"</div>\n",
"</div>"
]
}
],
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Data Types and Type Conversion\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Every value has a type."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Every value in a program has a specific type.\n",
"* For instance: integer,float, string\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"scrolled": true
},
"outputs": [],
"source": [
"age = 32\n",
"pi = 3.14\n",
"first_name = 'Arucas'\n",
"print(age)\n",
"print(pi)\n",
"print(first_name)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use the built-in function type to find the type of a value.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print(type(age))\n",
"print(type(pi))\n",
"print(type(first_name))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Types control what operations (or methods) can be performed on a given value.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [