{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Data Analysis with Jupyter Notebooks.\n", "\n", "# Tutorial 2 — Simple Calculations\n", "\n", "Benjamin J. Morgan, University of Bath." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Contents\n", "\n", "- [Simple Calculations](#simple_calculations)\n", "- [Comments and Markdown cells](#comments)\n", "- [Mathematical Functions and Modules](#functions_and_modules)\n", "- [Variables](#variables)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Simple calculations\n", "\n", "One of the simplest forms of “code” that can be run in code cells is mathematical expressions:\n", "\n", ">```python\n", "1+2+3+4\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">```python\n", "4*5/2\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">```python\n", "2**4 - 2\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`**` is the “power” operator. This code calculates $2^4 - 2$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Perform the following calculations in the three cells below: \n", " $1+1+2+3+5+8$ \n", " $1*2*3*4*5*6$ \n", " $2^{10}$\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Running the cell below will check your results for these three calculations. \n", "If any of these give errors, you can go back to edit your code, and re-run to check your changes." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This cell tests your answers from the three previous code cells.\n", "# You do not need to edit it\n", "assert ___ == 20\n", "assert __ == 720\n", "assert _ == 1024" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mathematical functions and modules\n", "\n", "In mathematics, a **function** converts one number to another number; $y=f(x)$.\n", "\n", "In programming, a **function** is more general than this, and converts an input into an output. For example, if we want to calculate a square root, we can use the `sqrt()` function.\n", "\n", ">```python\n", "sqrt(4)\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This has given us an error: \n", "\n", "NameError: name 'sqrt' is not defined. \n", "\n", "Python has a *lot* of built in commands (functions). Solving any particular problem will only require a small subset of these. To keep Python code efficient, only a minimal set of the available tools are available \"out of the box\". Other commands (such as mathematical functions) are collected in **modules** that we can load, to make these available in our notebook.\n", "\n", "`sqrt` lives in the `math` module. We can load it like this:\n", "\n", ">```python\n", "from math import sqrt\n", "sqrt(4)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Edit the last code cell to import the math function and re-run it. \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or we can import the entire math module:\n", "\n", ">```python\n", "import math\n", "math.sqrt(4)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Edit the previous code cell again to calculate $\\sqrt 4$ by importing the entire math module.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can think of `math.sqrt()` as instructing the computer to “use the `sqrt()` function provided inside the `math` module”." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `math` module contains a [large set of common mathematical functions](https://docs.python.org/2/library/math.html), and the constants $\\pi$ and $\\mathrm{e}$ (natural logarithm).\n", "\n", ">```python\n", "from math import pi, sin, e, log\n", "```\n", "\n", "The `log` function calculates the natural logarithm, i.e. $\\ln x$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">```python\n", "pi\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">```python\n", "sin( pi/2 )\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">```python\n", "e # natural logarithm\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">```python\n", "log(e) # the natural logarithm of e\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">```python\n", "e**2\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Calculate the following:

\n", "\n", " \n", "$\\cos(2\\pi)$

\n", "$\\ln(2\\mathrm{e})-\\ln(2)$

\n", "$\\log_{10}(10)$

\n", " \n", " \n", "The math function for $\\log_{10}$ is log10().\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This cell tests your answers from the three previous code cells.\n", "# You do not need to edit it\n", "from math import fabs\n", "assert fabs( ___ )- 1.0 < 1e-10\n", "assert fabs( __ ) - 1.0 < 1e-10\n", "assert fabs( _ ) - 1.0 < 1e-10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Variables\n", "\n", "Many of the code examples we have already seen produce some information. When each code cell is executed, if that code **returns** a result, this is printed directly underneath the corresponding code cell, next to Out[ ]:.\n", "\n", ">```python\n", "72/4\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">```python\n", "print(\"Nothing is returned here!\")\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "print() is another function, like `math.sqrt()` or `math.log()`. Instead of performing a mathematical calculation, print() just prints whatever is inside the brackets. In this example, this is exactly what happened. The text is printed to the screen, but the print() function does not return a value.\n", "\n", "print() can print more than one variable if these are separated by commas:\n", "\n", ">```python\n", "print(\"72/4 =\",72/4)\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import statements are another example of code that does not return anything.\n", "\n", ">```python\n", "from math import sqrt\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you run an code cell and nothing appears underneath, the code ran okay (and hopefully did what you expected). Any output under a cell will either be the **returned** result, or an error." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One aspect that makes programmatic data analysis useful comes from the ability to write complex procedures with many steps, that are then performed *identically* every time the code is run against new data sets. To build up more sophisticated data analysis workflows, we often want to keep a result of one step, to use in a later step. Storing results in computer memory is called **assigning** **variables**. A variable is just a name; a sequence of letters and numbers; that labels the stored result. Then, to access the value stored in the variable, we can use the label (the variable name) to refer to the original result.\n", "\n", ">```python\n", "# calculate 2 + 3\n", "2 + 3\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">```python\n", "# calculate 2 + 3 and store the result in the variable `my_result`\n", "my_result = 2 + 3\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice there is no return value printed to Out[ ]:. \n", "Instead a variable `my_result` is created, and the value returned by the calculation is stored here. \n", "\n", "Variable names can be nearly anything, as long as that name is not already used for some part of Python (e.g. `print`). Two limitations are they cannot begin with a number (but can contain numbers), and they cannot contain spaces. Underscores are commonly used instead of spaces to keep the code readable.\n", "\n", ">```python\n", "1st_result = 3 + 4\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">```python\n", "this result = 5 + 6\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Fix the two previous code cells to use first_result and this_result so that they will run without errors.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To check the value stored in a variable we can just type the variable name, which returns the stored value.\n", "\n", ">```python\n", "my_result\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that we only get the **last** value returned if we have multiple lines of code.\n", "\n", "\n", ">```python\n", "my_result\n", "this_result\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can get round this by using the `print` function to print out the value stored in one or more variables\n", ">```python\n", "print( my_result )\n", "print( this_result )\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Variables can be used to store raw numbers, and can then be used for calculations.\n", "\n", ">```python\n", "the_number_six = 6\n", "my_result + the_number_six\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Any code that uses variables may itself return a further result, which can be assigned to a new variable, and used later (and so on).\n", "\n", ">```python\n", "yet_another_variable = my_result + the_number_six\n", "print( yet_another_variable )\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you refer to a variable that has not yet been created you will get an error.\n", "\n", ">```python\n", "print( bananas )\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Edit the code cell above so to print \"bananas\" (the quotes are part of the new code), and re-run the cell.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Create three variables, $x$, $y$, and $z$, and use them to store the numbers $5,6,7$. \n", "\n", "Using these variables, calculate: \n", "$5+6+7$, \n", "and \n", "$(5+6)\\times7$.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create your variables and store the numbers 5, 6, 7\n", "◽◽◽\n", "◽◽◽\n", "◽◽◽" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# use the variables to calculate 5+6+7\n", "◽◽◽" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# use the variables to calculate (5+6)×7\n", "◽◽◽" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This cell tests your answers from the three previous code cells.\n", "# You do not need to edit it\n", "assert (z,y,x) == (7,6,5)\n", "assert __ == 18\n", "assert _ == 77" ] } ], "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 }