{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Python Basics" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- A variable is just a name for a value,\n", " such as `x`, `my_variable`, or `variable_1`\n", "- Python's variables must begin with a letter and are **case sensitive**\n", "- We can create a new variable by assigning a value to it using `=`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "x = 1" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = 2\n", "z = x + y\n", "z" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "ename": "NameError", "evalue": "name 'w' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mw\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'w' is not defined" ] } ], "source": [ "w" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Naming\n", "\n", "- Variable names in Python can contain alphanumerical characters `a-z`, `A-Z`, `0-9` and some special characters such \n", " as `_`. Normal variable names must start with a letter. \n", "- By convention, variable names start with a lower-case letter, and Class names start with a capital letter. \n", "- In addition, there are a number of Python keywords that cannot be used as variable names. These keywords are:\n", "\n", " and, as, assert, break, class,\n", " continue, def, del, elif, else, \n", " except, exec, finally, for, from,\n", " global, if, import, in, is, lambda, \n", " not, or, pass, print, raise, \n", " return, try, while, with, yield\n", "\n", "- _Note_: Be aware of the keyword `lambda`, which could easily be a natural variable name in a scientific program. But \n", " being a keyword, it cannot be used as a variable name." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Data types" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Since Python is **dynamically typed**, it automatically sets the types of your variables upon assignment" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Integers" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 1\n", "type(x)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Floats" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = 2.\n", "type(y)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.0 \n" ] } ], "source": [ "y = 2e0\n", "print(y, type(y))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "31.7" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3.17e1" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Booleans" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = True\n", "b = False\n", "type(a)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Strings" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z = '3'\n", "type(z)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Checking Type" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = 2.\n", "type(b) is int" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Type casting\n", "- Only variables of the same type can be combined\n", "- Where possible, Python will automatically cast one variable to be the same type as another to make them compatible" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "x = 1\n", "print(type(x))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "y = 2.\n", "print(type(y))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(int(y)))" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(x+y))" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'int' and 'str'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mz\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'3'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mw\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mz\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" ] } ], "source": [ "z = '3'\n", "w = x + z" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4 \n" ] } ], "source": [ "w = x + int(z)\n", "print(w, type(w))" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n" ] } ], "source": [ "print(bool(0))\n", "print(bool(1))" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n" ] } ], "source": [ "print(bool(0.))\n", "print(bool(1.))" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n" ] } ], "source": [ "print(bool('0'))\n", "print(bool('1'))" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n" ] } ], "source": [ "print(bool('True'))\n", "print(bool('False'))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Operators\n", "\n", "Operators are defined as \"constructs which can manipulate the values of operands\"..." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Arithmetic operators:\n", "- `+` \n", "- `-` \n", "- `*` \n", "- `/`\n", "- `//` (integer division)\n", "- `**` (power)\n", "- `%` (modulus)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Addition" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + 1" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1. + 1." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + 1." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Subtraction" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 - 2" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5. - 2." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Order of operations" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 - 3 + 1" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 - (3 + 1)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Multiplication" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * 4" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "8.0" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2. * 4" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Division" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "0.75" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 / 4" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "0.75" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3. / 4." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Integer division" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 // 4" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 // 4" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5. // 4." ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 - 2 * 3" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 - (2 * 3)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "24" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(10 - 2) * 3" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "((2 + 3) - 1) * 2" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0 / 1" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "division by zero", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m1\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" ] } ], "source": [ "1 / 0" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Power" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**3" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "8.0" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2.**3" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "8.0" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2. ** 3" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 2. * * 3\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "2. * * 3" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Note: don't try to use the caret (^) for power in Python! \n", "# For the curious: https://docs.python.org/2/reference/expressions.html#binary-bitwise-operations\n", "2^3" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Modulus" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 % 4" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 % 4" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "16 % 4" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "17 % 4" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Comparisons\n", "\n", "[Comparisons](https://docs.python.org/2/reference/expressions.html#not-in) are operators which evaluate properties \n", "of their operands and always return either `True` or `False`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Common comparisons:\n", "- `<`\n", "- `>`\n", "- `==`\n", "- `>=`\n", "- `<=`\n", "- `!=`" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 < 2" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True \n" ] } ], "source": [ "a = 1 < 2\n", "print(a, type(a))" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 == 2" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 == 2." ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 != 2." ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 != 1" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 <= 4" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 <= 3" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 <= 2.9" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 < 2 < 3" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 <= 2 < 3" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 <= (2 < 3)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "source": [ "`is` and `is not` check whether two things point to the same object, not just equality" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 is 1" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 is 1." ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 1\n", "y = 1\n", "x is y" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 1.\n", "y = 1\n", "x is y" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 1.\n", "y = 1.\n", "x is y" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 1.\n", "y = 1.\n", "x == y" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "collapsed": false, "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 1.\n", "y = x\n", "x is y" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### [Boolean operations](https://docs.python.org/2/reference/expressions.html#boolean-operations)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- `and`: `x and y` first evaluates `x`\n", " * if `x` is false, its value is returned\n", " * otherwise, `y` is evaluated and the resulting value is returned" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- `or`: `x or y` first evaluates `x`\n", " * if `x` is true, its value is returned\n", " * otherwise, `y` is evaluated and the resulting value is returned" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- `not`: yields `True` if its argument is false, `False` otherwise" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### `and`" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and False" ] }, { "cell_type": "code", "execution_count": 72, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and True" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 and True" ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 and False" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and 0" ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(True and 0)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### `or`" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True or False" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False or True" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 or False" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### `not`" ] }, { "cell_type": "code", "execution_count": 80, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True is not False" ] }, { "cell_type": "code", "execution_count": 81, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not True" ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not 2" ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not 0" ] }, { "cell_type": "code", "execution_count": 84, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 is not 2" ] }, { "cell_type": "code", "execution_count": 85, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 1\n", "y = 1.\n", "x is not y" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Combining boolean operations" ] }, { "cell_type": "code", "execution_count": 86, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 < 2 < 3" ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1 < 2) and (2 < 3)" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1 < 2) and (1 > 2)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0 is not 1 and 2 is not 3" ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0 is 1 or 2 is 2" ] }, { "cell_type": "code", "execution_count": 91, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(0 is 1) or (2 is 2)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Fun with strings" ] }, { "cell_type": "code", "execution_count": 92, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'hello world'\n", "type(s)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "source": [ "### String indexing" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(s)" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'e'" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[1]" ] }, { "cell_type": "code", "execution_count": 95, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'h'" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[0]" ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "'d'" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[len(s)-1]" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'d'" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[-1]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### String replacement" ] }, { "cell_type": "code", "execution_count": 98, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'goodbye world'" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.replace('hello', 'goodbye')" ] }, { "cell_type": "code", "execution_count": 99, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'heLLo worLd'" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.replace('l', 'L')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Slicing\n", "\n", "You can pull out different chunks of the string using `[start:end:step]` \n", "- `start` defaults to 0\n", "- `end` defaults to `len(string)-1` (i.e. the last character)\n", "- `step` defaults to 1" ] }, { "cell_type": "code", "execution_count": 100, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'hello'" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[0:5]" ] }, { "cell_type": "code", "execution_count": 101, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'hello'" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[:5]" ] }, { "cell_type": "code", "execution_count": 102, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'world'" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[6:]" ] }, { "cell_type": "code", "execution_count": 103, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'hello world'" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[:]" ] }, { "cell_type": "code", "execution_count": 104, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'hello world'" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[::1]" ] }, { "cell_type": "code", "execution_count": 105, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'hlowrd'" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[::2]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### String concatenation" ] }, { "cell_type": "code", "execution_count": 106, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hello'" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'hel' + 'lo'" ] }, { "cell_type": "code", "execution_count": 107, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'helloworld'" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[:5] + s[6:]" ] }, { "cell_type": "code", "execution_count": 108, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "TypeError", "evalue": "Can't convert 'int' object to str implicitly", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m'he'\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m11\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: Can't convert 'int' object to str implicitly" ] } ], "source": [ "'he' + 11 + 0" ] }, { "cell_type": "code", "execution_count": 109, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'he110'" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'he' + str(11) + str(0)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### String formatting\n", "\n", "- Enables you to combine strings however you'd like\n", "- Extremely powerful, so see the [docs](https://docs.python.org/3.6/library/string.html#format-string-syntax) for\n", " more details" ] }, { "cell_type": "code", "execution_count": 110, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello world\n" ] } ], "source": [ "print('hello', 'world')" ] }, { "cell_type": "code", "execution_count": 111, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'hello world'" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'{} {}'.format('hello', 'world')" ] }, { "cell_type": "code", "execution_count": 112, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'world hello'" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'{1} {0}'.format('hello', 'world')" ] }, { "cell_type": "code", "execution_count": 113, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'hello everybody'" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 'everybody'\n", "'{} {}'.format('hello', x)" ] }, { "cell_type": "code", "execution_count": 114, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'pi is 3.142'" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from math import pi\n", "'pi is {:.3f}'.format(pi)" ] }, { "cell_type": "code", "execution_count": 115, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'π is 3.142'" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'π is {:.3f}'.format(pi)" ] }, { "cell_type": "code", "execution_count": 116, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'1 is the loneliest number'" ] }, "execution_count": 116, "metadata": {}, "output_type": "execute_result" } ], "source": [ "number = 1.0\n", "superlative = 'loneliest'\n", "'{num:g} is the {adj} number'.format(adj=superlative, num=number)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Lists\n", "\n", "- A list is similar to a string in that it's a collection of objects\n", "- However, in Python a list can contain objects of any type" ] }, { "cell_type": "code", "execution_count": 117, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1,2,3]\n", "a" ] }, { "cell_type": "code", "execution_count": 118, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "code", "execution_count": 119, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "['hello', 'world']" ] }, "execution_count": 119, "metadata": {}, "output_type": "execute_result" } ], "source": [ "['hello', 'world']" ] }, { "cell_type": "code", "execution_count": 120, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "[1, 'love']" ] }, "execution_count": 120, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1, 'love']" ] }, { "cell_type": "code", "execution_count": 121, "metadata": { "collapsed": true, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "a = [1, 'two', 3.]" ] }, { "cell_type": "code", "execution_count": 122, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "two\n", "3.0\n" ] } ], "source": [ "print(a[0])\n", "print(a[1])\n", "print(a[2])" ] }, { "cell_type": "code", "execution_count": 123, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(a)" ] }, { "cell_type": "code", "execution_count": 124, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "a[3]" ] }, { "cell_type": "code", "execution_count": 125, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 125, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[-1]" ] }, { "cell_type": "code", "execution_count": 126, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 126, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a[-1])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Nested lists" ] }, { "cell_type": "code", "execution_count": 127, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[[1, 2, 3], [4, 5]]" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [[1,2,3], [4,5]]\n", "a" ] }, { "cell_type": "code", "execution_count": 128, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 128, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0]" ] }, { "cell_type": "code", "execution_count": 129, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "[4, 5]" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1]" ] }, { "cell_type": "code", "execution_count": 130, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0][1]" ] }, { "cell_type": "code", "execution_count": 131, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "[4, 5]" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1][:]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Range" ] }, { "cell_type": "code", "execution_count": 132, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "range(5, 15, 2)" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [ "start = 5\n", "stop = 15\n", "step = 2\n", "range(start, stop, step)" ] }, { "cell_type": "code", "execution_count": 133, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "[5, 7, 9, 11, 13]" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(start, stop, step))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Strings and lists" ] }, { "cell_type": "code", "execution_count": 134, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = list('hello world')\n", "a" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Modifying lists" ] }, { "cell_type": "code", "execution_count": 135, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']" ] }, "execution_count": 135, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0] = 'H'\n", "a" ] }, { "cell_type": "code", "execution_count": 136, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']" ] }, "execution_count": 136, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[6] = 'W'\n", "a" ] }, { "cell_type": "code", "execution_count": 137, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!']" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.append('!')\n", "a" ] }, { "cell_type": "code", "execution_count": 138, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']" ] }, "execution_count": 138, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.insert(5, ',')\n", "a" ] }, { "cell_type": "code", "execution_count": 139, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "['H', 'e', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.remove('l')\n", "a" ] }, { "cell_type": "code", "execution_count": 140, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "'Helo, World!'" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" } ], "source": [ "''.join(a)" ] }, { "cell_type": "code", "execution_count": 141, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'H\\ne\\nl\\no\\n,\\n \\nW\\no\\nr\\nl\\nd\\n!'" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'\\n'.join(a)" ] }, { "cell_type": "code", "execution_count": 142, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "H\n", "e\n", "l\n", "o\n", ",\n", " \n", "W\n", "o\n", "r\n", "l\n", "d\n", "!\n" ] } ], "source": [ "print('\\n'.join(a))" ] }, { "cell_type": "code", "execution_count": 143, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "[1, 2]" ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = []\n", "b.append(1)\n", "b.append(2)\n", "b" ] }, { "cell_type": "code", "execution_count": 144, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "[2, 1]" ] }, "execution_count": 144, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.reverse()\n", "b" ] }, { "cell_type": "code", "execution_count": 145, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "[1, 2]" ] }, "execution_count": 145, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.sort()\n", "b" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Checking inside of lists" ] }, { "cell_type": "code", "execution_count": 146, "metadata": { "collapsed": true }, "outputs": [], "source": [ "c = [1,2,3,4,5]" ] }, { "cell_type": "code", "execution_count": 147, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 147, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 in c" ] }, { "cell_type": "code", "execution_count": 148, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 148, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7 in c" ] }, { "cell_type": "code", "execution_count": 149, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 149, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.index(3)" ] }, { "cell_type": "code", "execution_count": 150, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "ValueError", "evalue": "10 is not in list", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: 10 is not in list" ] } ], "source": [ "c.index(10)" ] }, { "cell_type": "code", "execution_count": 151, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5]\n", "[1, 3, 4, 5]\n" ] } ], "source": [ "print(c)\n", "c.remove(2)\n", "print(c)" ] }, { "cell_type": "code", "execution_count": 152, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 3, 4, 5]\n", "[1, 3, 4] 5\n" ] } ], "source": [ "print(c)\n", "popped = c.pop(c.index(5))\n", "print(c, popped)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Tuples\n", "\n", "Tuples are like lists but **immutable**, meaning they cannot be changed" ] }, { "cell_type": "code", "execution_count": 153, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 153, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = (1,2)\n", "d" ] }, { "cell_type": "code", "execution_count": 154, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 154, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[0]" ] }, { "cell_type": "code", "execution_count": 155, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0md\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "source": [ "d[0] = 5" ] }, { "cell_type": "code", "execution_count": 156, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3)" ] }, "execution_count": 156, "metadata": {}, "output_type": "execute_result" } ], "source": [ "e = tuple([1,2,3])\n", "e" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Dictionaries\n", "\n", "- Dictionaries are also like lists, except that instead of values being indexed by their order they're indexed by keys\n", "- Each element is a key-value pair\n", "- The syntax for dictionaries is `{key1 : value1, ...}`" ] }, { "cell_type": "code", "execution_count": 157, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'a': 1, 'b': 2}" ] }, "execution_count": 157, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{'a': 1, 'b': 2}" ] }, { "cell_type": "code", "execution_count": 158, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "{1: 'hello', 'two': 'world'}" ] }, "execution_count": 158, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{1: 'hello', 'two': 'world'}" ] }, { "cell_type": "code", "execution_count": 159, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "{'a': 1, 'b': 2, 'c': 3}" ] }, "execution_count": 159, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = {'a': 1, 'b': 2, 'c': 3}\n", "d" ] }, { "cell_type": "code", "execution_count": 160, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "{'a': 1, 'b': 2, 'c': 3, 'd': 4}" ] }, "execution_count": 160, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['d'] = 4\n", "d" ] }, { "cell_type": "code", "execution_count": 161, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 161, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['c']" ] }, { "cell_type": "code", "execution_count": 162, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 162, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 'c'\n", "d[x]" ] }, { "cell_type": "code", "execution_count": 163, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "{'acceleration': 3.2, 'force': 16.0, 'mass': 5.0}" ] }, "execution_count": 163, "metadata": {}, "output_type": "execute_result" } ], "source": [ "param = {'mass': 5.0, 'acceleration': 3.2}\n", "param['force'] = param['mass'] * param['acceleration']\n", "param" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Control flow\n", "\n", "- `if`, `elif` (else if), and `else`\n", "- Blocks begin with `if condition:` and are then indented below\n", "- Convention is 4 spaces of indentation" ] }, { "cell_type": "code", "execution_count": 164, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hi\n" ] } ], "source": [ "if True: \n", " print('hi')" ] }, { "cell_type": "code", "execution_count": 165, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ " if False:\n", " print('bye') " ] }, { "cell_type": "code", "execution_count": 166, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hi\n" ] } ], "source": [ "if True:\n", " print('hi')\n", "else:\n", " print('bye')" ] }, { "cell_type": "code", "execution_count": 167, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 is less than 1\n" ] } ], "source": [ "x = 0\n", "if x < 1:\n", " print('{} is less than 1'.format(x))\n", "elif x > 1:\n", " print('{} is greater than 1'.format(x))\n", "else:\n", " print('I guess {} *is* 1?'.format(x))" ] }, { "cell_type": "code", "execution_count": 168, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I guess 1 *is* 1?\n" ] } ], "source": [ "x = 1\n", "if x < 1:\n", " print('{} is less than 1'.format(x))\n", "elif x > 1:\n", " print('{} is greater than 1'.format(x))\n", "else:\n", " print('I guess {} *is* 1?'.format(x))" ] }, { "cell_type": "code", "execution_count": 169, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 is greater than 1\n" ] } ], "source": [ "x = 2\n", "if x < 1:\n", " print('{} is less than 1'.format(x))\n", "elif x > 1:\n", " print('{} is greater than 1'.format(x))\n", "else:\n", " print('I guess {} *is* 1?'.format(x))" ] }, { "cell_type": "code", "execution_count": 170, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 170, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1\n", "if a == 1:\n", " a = 2\n", "elif a == 2:\n", " a = 3\n", "else:\n", " a = 4\n", "a" ] }, { "cell_type": "code", "execution_count": 171, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 171, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 2\n", "if a == 1:\n", " a = 2\n", "elif a == 2:\n", " a = 3\n", "else:\n", " a = 4\n", "a" ] }, { "cell_type": "code", "execution_count": 172, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 172, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 7\n", "if a == 1:\n", " a = 2\n", "elif a == 2:\n", " a = 3\n", "else:\n", " a = 4\n", "a" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Loops" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### `for` loops" ] }, { "cell_type": "code", "execution_count": 173, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for x in [1,2,3]:\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 174, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for x in range(3):\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 175, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n", "world\n" ] } ], "source": [ "for x in ['hello', 'world']:\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 176, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "h\n", "e\n", "l\n", "l\n", "o\n", " \n", "w\n", "o\n", "r\n", "l\n", "d\n" ] } ], "source": [ "for x in 'hello world':\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 177, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 squared is 0!\n", "1 squared is 1!\n", "2 squared is 4!\n", "3 squared is 9!\n", "4 squared is 16!\n", "5 squared is 25!\n", "6 squared is 36!\n", "7 squared is 49!\n", "8 squared is 64!\n", "9 squared is 81!\n", "10 squared is 100!\n" ] } ], "source": [ "for x in range(11):\n", " sq = x**2\n", " print('{x} squared is {sq}!'.format(x=x, sq=sq))" ] }, { "cell_type": "code", "execution_count": 178, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 is 0 squared!\n", "1 is 1 squared!\n", "4 is 2 squared!\n", "9 is 3 squared!\n", "16 is 4 squared!\n", "25 is 5 squared!\n", "36 is 6 squared!\n", "49 is 7 squared!\n", "64 is 8 squared!\n", "81 is 9 squared!\n", "100 is 10 squared!\n" ] } ], "source": [ "from math import sqrt\n", "for x in range(101):\n", " rt = sqrt(x)\n", " if rt == round(rt):\n", " print('{x} is {rt:g} squared!'.format(x=x, rt=rt))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### `while` loops\n", "- `while` loops keep executing until they evalute to `False`\n", "- Be careful not to create an infinite loop!\n", " * Use `Ctl+C` (or `Ctl+D`) if you do..." ] }, { "cell_type": "code", "execution_count": 179, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "x = 0\n", "while x < 5:\n", " print(x)\n", " x = x + 1" ] }, { "cell_type": "code", "execution_count": 180, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "y = 0\n", "while y < 5:\n", " print(y)\n", " y += 1" ] }, { "cell_type": "code", "execution_count": 181, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "4\n", "3\n", "2\n", "1\n" ] } ], "source": [ "z = 5\n", "while z:\n", " print(z)\n", " z -= 1" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### List comprehensions" ] }, { "cell_type": "code", "execution_count": 182, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" ] }, "execution_count": 182, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[x for x in range(11)]" ] }, { "cell_type": "code", "execution_count": 183, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]" ] }, "execution_count": 183, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[x**2 for x in range(11)]" ] }, { "cell_type": "code", "execution_count": 184, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "['0 squared is 0',\n", " '1 squared is 1',\n", " '2 squared is 4',\n", " '3 squared is 9',\n", " '4 squared is 16',\n", " '5 squared is 25',\n", " '6 squared is 36',\n", " '7 squared is 49',\n", " '8 squared is 64',\n", " '9 squared is 81',\n", " '10 squared is 100']" ] }, "execution_count": 184, "metadata": {}, "output_type": "execute_result" } ], "source": [ "['{num} squared is {sq}'.format(num=x, sq=x**2) for x in range(11)]" ] }, { "cell_type": "code", "execution_count": 185, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}\n" ] } ], "source": [ "squares = {x: x**2 for x in range(11)}\n", "print(squares)" ] }, { "cell_type": "code", "execution_count": 186, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 186, "metadata": {}, "output_type": "execute_result" } ], "source": [ "squares[5]" ] }, { "cell_type": "code", "execution_count": 187, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 is the square root of 0\n", "1 is the square root of 1\n", "2 is the square root of 4\n", "3 is the square root of 9\n", "4 is the square root of 16\n", "5 is the square root of 25\n", "6 is the square root of 36\n", "7 is the square root of 49\n", "8 is the square root of 64\n", "9 is the square root of 81\n", "10 is the square root of 100\n" ] } ], "source": [ "for key, value in squares.items():\n", " print('{k} is the square root of {v}'.format(k=key, v=value))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Modules\n", "\n", "- The Python Language Reference: http://docs.python.org/2/reference/index.html\n", "- The Python Standard Library: http://docs.python.org/2/library/\n", "\n", "To use a module in a Python program it first has to be imported. A module can be imported using the `import` statement. For example, to import the module `math`, which contains many standard mathematical functions, we can do:" ] }, { "cell_type": "code", "execution_count": 188, "metadata": { "collapsed": true, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "import math" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "$ \\cos(2 \\pi) = 1 $" ] }, { "cell_type": "code", "execution_count": 189, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 189, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.cos(2. * math.pi)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "$ \\cos(\\frac{\\pi}{2}) = 0 $" ] }, { "cell_type": "code", "execution_count": 190, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "6.123233995736766e-17" ] }, "execution_count": 190, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.cos(math.pi / 2.)" ] }, { "cell_type": "code", "execution_count": 191, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 191, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# https://docs.python.org/2/tutorial/floatingpoint.html\n", "round(math.cos(math.pi / 2.), 10)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Importing from modules" ] }, { "cell_type": "code", "execution_count": 192, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 192, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from math import cos, pi\n", "cos(2. * pi)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "`import *`" ] }, { "cell_type": "code", "execution_count": 193, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "-2.4492935982947064e-16" ] }, "execution_count": 193, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from math import *\n", "sin(2. * pi)" ] }, { "cell_type": "code", "execution_count": 194, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "['atan',\n", " 'atan2',\n", " 'atanh',\n", " 'ceil',\n", " 'copysign',\n", " 'cos',\n", " 'cosh',\n", " 'degrees',\n", " 'e',\n", " 'erf']" ] }, "execution_count": 194, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(math)[10:20]" ] }, { "cell_type": "code", "execution_count": 195, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m math.\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "math." ] }, { "cell_type": "code", "execution_count": 196, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function log in module math:\n", "\n", "log(...)\n", " log(x[, base])\n", " \n", " Return the logarithm of x to the given base.\n", " If the base not specified, returns the natural logarithm (base e) of x.\n", "\n" ] } ], "source": [ "help(math.log)" ] }, { "cell_type": "code", "execution_count": 197, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 197, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.log(math.e)" ] }, { "cell_type": "code", "execution_count": 198, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 198, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.log(10., 10.)" ] }, { "cell_type": "code", "execution_count": 199, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9.0\n", "9.0\n" ] } ], "source": [ "print(3.**2)\n", "print(math.pow(3,2))" ] }, { "cell_type": "code", "execution_count": 200, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20.085536923187664\n", "20.085536923187668\n" ] } ], "source": [ "print(math.e**3.)\n", "print(math.exp(3.))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Functions\n", "\n", "- Functions are reusable and flexible bits of Python code\n", "- Functions are **scoped** - they have access to global variables, but variables created inside of them are local to the function and invisible outside of it\n", "\n", " def func_name(x):\n", " return x**2" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Simple functions" ] }, { "cell_type": "code", "execution_count": 201, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def print_hello():\n", " print('hello')" ] }, { "cell_type": "code", "execution_count": 202, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n" ] } ], "source": [ "print_hello()" ] }, { "cell_type": "code", "execution_count": 203, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello, world\n" ] } ], "source": [ "def print_hello(name):\n", " ''' Prints \"hello, {name}\".'''\n", " print('hello, {}'.format(name))\n", "\n", "print_hello('world')" ] }, { "cell_type": "code", "execution_count": 204, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "TypeError", "evalue": "print_hello() missing 1 required positional argument: 'name'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint_hello\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: print_hello() missing 1 required positional argument: 'name'" ] } ], "source": [ "print_hello()" ] }, { "cell_type": "code", "execution_count": 205, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def print_hello(name='buddy'):\n", " print('hello, {}'.format(name))" ] }, { "cell_type": "code", "execution_count": 206, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello, world\n" ] } ], "source": [ "print_hello('world')" ] }, { "cell_type": "code", "execution_count": 207, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello, buddy\n" ] } ], "source": [ "print_hello()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Returning values" ] }, { "cell_type": "code", "execution_count": 208, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def sq_print(x):\n", " ''' Prints the square of x (x^2) '''\n", " print(x**2)" ] }, { "cell_type": "code", "execution_count": 209, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n" ] } ], "source": [ "sq_print(3)" ] }, { "cell_type": "code", "execution_count": 210, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n" ] } ], "source": [ "three_sq = sq_print(3)" ] }, { "cell_type": "code", "execution_count": 211, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "print(three_sq)" ] }, { "cell_type": "code", "execution_count": 212, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "NoneType" ] }, "execution_count": 212, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(three_sq)" ] }, { "cell_type": "code", "execution_count": 213, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n" ] }, { "ename": "TypeError", "evalue": "unsupported operand type(s) for *: 'NoneType' and 'int'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msq_print\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for *: 'NoneType' and 'int'" ] } ], "source": [ "sq_print(3) * 5" ] }, { "cell_type": "code", "execution_count": 214, "metadata": { "collapsed": true, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "def sq(x):\n", " ''' Returns the square of x (x^2)'''\n", " return x**2" ] }, { "cell_type": "code", "execution_count": 215, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 215, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sq(3)" ] }, { "cell_type": "code", "execution_count": 216, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n" ] } ], "source": [ "three_sq = sq(3)\n", "print(three_sq)" ] }, { "cell_type": "code", "execution_count": 217, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 217, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(three_sq)" ] }, { "cell_type": "code", "execution_count": 218, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "45" ] }, "execution_count": 218, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sq(3) * 5" ] }, { "cell_type": "code", "execution_count": 219, "metadata": { "collapsed": true, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "def sq_with_print(x):\n", " ''' Returns the square of x (x^2) and prints it '''\n", " s = x**2\n", " print('{x} squared is {s}'.format(x=x, s=s))\n", " return s" ] }, { "cell_type": "code", "execution_count": 220, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 squared is 25\n" ] }, { "data": { "text/plain": [ "25" ] }, "execution_count": 220, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sq_with_print(5)" ] }, { "cell_type": "code", "execution_count": 221, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 squared is 25\n", "25\n", "\n" ] } ], "source": [ "s = sq_with_print(5)\n", "print(s)\n", "print(type(s))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Returning multiple values" ] }, { "cell_type": "code", "execution_count": 222, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "def rectangle(w, h):\n", " ''' Given a width `w` and height `h`, returns the area and perimeter of the corresponding rectangle '''\n", " area = w * h\n", " perim = 2 * (w + h)\n", " return area, perim" ] }, { "cell_type": "code", "execution_count": 223, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(50, 30)" ] }, "execution_count": 223, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rectangle(5, 10)" ] }, { "cell_type": "code", "execution_count": 224, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "50\n" ] } ], "source": [ "my_rect = rectangle(5, 10)\n", "print(type(my_rect))\n", "print(my_rect[0])" ] }, { "cell_type": "code", "execution_count": 225, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "50\n" ] } ], "source": [ "area, perim = rectangle(5, 10)\n", "print(type(area))\n", "print(area)" ] }, { "cell_type": "code", "execution_count": 226, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def rectangle(w, h):\n", " ''' Given a width `w` and height `h`, returns the area and perimeter of the corresponding rectangle in a dict '''\n", " area = w * h\n", " perim = 2 * (w + h)\n", " return {'area': area, 'perim': perim}" ] }, { "cell_type": "code", "execution_count": 227, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "{'area': 56, 'perim': 30}" ] }, "execution_count": 227, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rectangle(7, 8)" ] }, { "cell_type": "code", "execution_count": 228, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "56\n" ] } ], "source": [ "my_rect = rectangle(7, 8)\n", "print(my_rect['area'])" ] }, { "cell_type": "code", "execution_count": 229, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def rectangle(w, h):\n", " ''' Given a width w and height h, returns some info about the corresponding rectangle '''\n", " area = w * h\n", " perim = 2 * (w + h)\n", " return {'w': w,\n", " 'h': h,\n", " 'area': area, \n", " 'perim': perim}" ] }, { "cell_type": "code", "execution_count": 230, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "{'area': 12, 'h': 4, 'perim': 14, 'w': 3}" ] }, "execution_count": 230, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rectangle(3, 4)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Keyword args" ] }, { "cell_type": "code", "execution_count": 231, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "from math import pow\n", "pow?" ] }, { "cell_type": "code", "execution_count": 232, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def my_pow(x, y=2):\n", " ''' Return x to the power of y, where y defaults to 2 '''\n", " return x**y" ] }, { "cell_type": "code", "execution_count": 233, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 233, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_pow(3, 2)" ] }, { "cell_type": "code", "execution_count": 234, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 234, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_pow(3)" ] }, { "cell_type": "code", "execution_count": 235, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 235, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_pow(x=3, y=2)" ] }, { "cell_type": "code", "execution_count": 236, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 236, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_pow(y=2, x=3)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Function scope" ] }, { "cell_type": "code", "execution_count": 237, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "a = 1\n", "def make_a_two():\n", " ''' Try to change the value of a to 2 '''\n", " a = 2" ] }, { "cell_type": "code", "execution_count": 238, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(a)" ] }, { "cell_type": "code", "execution_count": 239, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "make_a_two()\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 240, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def make_a_two():\n", " ''' Try to change the value of a to 2 '''\n", " a = 2\n", " print('The value of a is {}'.format(a))" ] }, { "cell_type": "code", "execution_count": 241, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The value of a is 2\n" ] } ], "source": [ "make_a_two()" ] }, { "cell_type": "code", "execution_count": 242, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(a)" ] }, { "cell_type": "code", "execution_count": 243, "metadata": { "collapsed": true, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "global_a = 5" ] }, { "cell_type": "code", "execution_count": 244, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def print_global_a():\n", " ''' Print out the value of global_a '''\n", " print('global_a contains {}'.format(global_a))" ] }, { "cell_type": "code", "execution_count": 245, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "global_a contains 5\n" ] } ], "source": [ "print_global_a()" ] }, { "cell_type": "code", "execution_count": 246, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def print_global_a():\n", " ''' Print out the value of global_a '''\n", " global_a = 7\n", " print('global_a contains {}'.format(global_a))" ] }, { "cell_type": "code", "execution_count": 247, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "global_a contains 7\n" ] } ], "source": [ "print_global_a()" ] }, { "cell_type": "code", "execution_count": 248, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "print(global_a)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Exercise 2\n", "\n", "- Open [Lecture 2/Exercise 2.ipynb](./Exercise 2.ipynb) using your Jupyter notebook server and follow the instructions\n", "- You can check your solutions in [Lecture 2/Exercise 2 - Solutions.ipynb](./Exercise 2 - Solutions.ipynb)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# References\n", "\n", "- Slide materials inspired by and adapted from [Chris Fonnesbeck](https://github.com/fonnesbeck/HealthPolicyPython) and [J Robert Johansson](https://github.com/jrjohansson/scientific-python-lectures)" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [conda root]", "language": "python", "name": "conda-root-py" }, "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.2" }, "livereveal": { "height": 768, "scroll": true, "slideNumber": true, "start_slideshow_at": "selected", "theme": "league", "width": 1024 } }, "nbformat": 4, "nbformat_minor": 1 }