{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Breakout: Simple Math in Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The purpose of this breakout is to help you familiarize yourself with executing Python code in the Jupyter notebook, and along the way familiarize yourself with some of the mathematical functionality available in Python.\n", "\n", "As with many breakouts in this course, you should **do the following with a partner**.\n", "One of you should have your fingers on the keyboard, while the other watches and helps decide what to type." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Some Basic Notebook Navigation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Jupyter notebooks combine text and executable code: a code cell looks like this, and you can run it by pressing enter/return **while holding the shift key**. Try it out here:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "1 + 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Additionally, you'll sometimes want to insert a new cell to add more code. You can do this by using the **[+]** button which is second from the left at the top of the notebook page.\n", "Before you continue, insert a new cell below this one to make sure you see how it works." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Note* We'll learn more about _Markdown_ later but if you'd like to learn how to incorporate equations into your notebook, google _Markdown_ and _LaTex_" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exploring Arithmetic Operators in Python\n", "\n", "Python can be used quite straightforwardly as a calculator.\n", "Among other commands, it recognizes ``+``, ``-``, ``*``, ``/``, and ``()`` with their typical mathematical meanings.\n", "With this in mind, we're going to quickly practice using Python as a calculator.\n", "\n", "Use Python to solve the following arithmetic problems:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is $4235 + 3154$?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is $4235 - 3154$?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is $4235 \\times 3154$?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is $\\frac{4235}{3154}$?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is $\\frac{4235 - 3154}{2752 + 5013}$?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is $\\frac{-(4315 + 378)}{2(65 - 328)}$" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exploring More Specialized Arithmetic\n", "\n", "Some other useful operators are ``//``, ``%``, and ``**``." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is ``4235 // 3154``?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try the ``//`` operator with some other inputs: can you figure out its meaning?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is ``4235 % 3154``?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try the ``%`` operator with some other inputs: can you figure out its meaning?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is ``4235 ** 3154``?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try the ``**`` operator with some other inputs: can you figure out its meaning?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exploring other Operators\n", "\n", "There's another class of operators recognized by Python: ``&``, ``|``, ``^``, and ``~``.\n", "\n", "Play around with these four operators: can you discover their meaning?\n", "(Don't worry if you can't: these are quite a bit more obscure than the ones above!)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The ``math`` Module\n", "\n", "**Please switch roles now, so the second person has fingers-on-keyboard**\n", "\n", "Along with the basic arithmetic operators, Python has some more sophisticated mathematical operations in the *``math`` module*.\n", "A module in Python is a collection of functionality that can be *imported* and used in your script.\n", "Importing functionality from modules is *extremely* easy in Python compared to some other languages, and it has an incredible array of standard functionality available through these imports.\n", "\n", "A popular XKCD comic draws from this idea:\n", "\n", "\n", "\n", "The way you import a module, like in this xkcd comic, is to use the ``import`` statement. For example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import antigravity" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import math" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With this done, we can now access elements of the ``math`` module using ``math.XXX``, where we replace ``XXX`` with the name of the variable, function, or other object we're interested in.\n", "\n", "For example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# variable containing the value of π\n", "math.pi" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# function to compute the square root of a number\n", "math.sqrt(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use functions in the math module to compute the following:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is $\\sin(2)$?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is $\\cos(2)$?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is $\\exp(2)$ (otherwise known as $e^2$)?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is $\\log(2)$?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is $\\sin^2(2) + \\cos^2(2)$?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is $\\sin(2\\pi)$? (do you notice anything strange?)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exploring Packages with Jupyter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Jupyter notebook includes a very convenient way to explore the contents of a module.\n", "\n", "- If you type ``math.`` followed by the tab key, it will give you a list of valid completions, which in this case consists of a list of all functions and variables in the math module.\n", "\n", "- If you type any function followed by a question mark (e.g. ``math.cos?``) then shift-enter, it will give you a documentation string" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use these two convenience features to explore the ``math`` module.\n", "\n", "Can you find a statement that computes $\\sqrt{x^2 + y^2}$ efficiently in a single line?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is $\\sqrt{4235^2 + 3154^2}$?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is $e^x - 1$ for $x = 10^{-20}$? (Hint: it's not zero!)\n", "Can you find a function in the ``math`` module that computes this function accurately?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Please put a yellow post-it on your laptop, so we know that you've finished the exploration.**\n", "\n", "While you are waiting for others to finish, continue exploring the ``math`` module to see what functionality is available." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "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.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }