{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "ipub": { "ignore": true } }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" }, "toc-hr-collapsed": false }, "source": [ "# Overview of Python\n", "\n", "(c) 2019 [Steve Phelps](mailto:sphelps@sphelps.net) " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Python is interpreted\n", "\n", "- Python is an _interpreted_ language, in contrast to Java and C which are compiled languages.\n", "\n", "- This means we can type statements into the interpreter and they are executed immediately.\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 + 5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Groups of statements are all executed one after the other:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "x = 5\n", "y = 'Hello There'\n", "z = 10.5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- We can visualize the above code using [PythonTutor](http://pythontutor.com/visualize.html#code=x%20%3D%205%0Ay%20%3D%20'Hello%20There'%0Az%20%3D%2010.5&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x + 5" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Assignments versus equations\n", "\n", "- In Python when we write `x = 5` this means something different from an equation $x=5$.\n", "\n", "- Unlike variables in mathematical models, variables in Python can refer to different things as more statements are interpreted.\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The value of x is 1\n", "Now the value of x is 2.5\n", "Now it is hello there\n" ] } ], "source": [ "x = 1\n", "print('The value of x is', x)\n", "\n", "x = 2.5\n", "print('Now the value of x is', x)\n", "\n", "x = 'hello there'\n", "print('Now it is ', x)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Calling Functions\n", "\n", "We can call functions in a conventional way using round brackets" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(3.14)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Types\n", "\n", "- Values in Python have an associated _type_.\n", "\n", "- If we combine types incorrectly we get an error." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello There\n" ] } ], "source": [ "print(y)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "ename": "TypeError", "evalue": "can only concatenate str (not \"int\") to 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[0;32m----> 1\u001b[0;31m \u001b[0my\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str" ] } ], "source": [ "y + 5" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## The type function\n", "\n", "- We can query the type of a value using the `type` function." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(1)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type('hello')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(2.5)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(True)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Null values\n", "\n", "- Sometimes we represent \"no data\" or \"not applicable\". \n", "\n", "- In Python we use the special value `None`.\n", "\n", "- This corresponds to `Null` in Java or SQL.\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "result = None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- When we fetch the value `None` in the interactive interpreter, no result is printed out.\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "result" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Testing for Null values\n", "\n", "- We can check whether there is a result or not using the `is` operator:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result is None" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 5\n", "x is None" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Converting values between types\n", "\n", "- We can convert values between different types.\n", "\n", "### Converting to floating-point\n", "\n", "- To convert an integer to a floating-point number use the `float()` function.\n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 1\n", "x" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(x)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = float(x)\n", "y" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Converting to integers\n", "\n", "- To convert a floating-point to an integer use the `int()` function." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(y)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(y)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Variables are not typed\n", "\n", "- _Variables_ themselves, on the other hand, do not have a fixed type.\n", "- It is only the values that they refer to that have a type.\n", "- This means that the type referred to by a variable can change as more statements are interpreted.\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The type of the value referred to by y is \n", "And now the type of the value is \n" ] } ], "source": [ "y = 'hello'\n", "print('The type of the value referred to by y is ', type(y))\n", "y = 5.0\n", "print('And now the type of the value is ', type(y))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" }, "toc-hr-collapsed": false }, "source": [ "## Polymorphism\n", "\n", "- The meaning of an operator depends on the types we are applying it to.\n", "\n" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + 1" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ab'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'a' + 'b'" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'11'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'1' + '1'" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" }, "toc-hr-collapsed": false }, "source": [ "## Conditional Statements and Indentation\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- The syntax for control structures in Python uses _colons_ and _indentation_.\n", "\n", "- Beware that white-space affects the semantics of Python code.\n", "\n", "- Statements that are indented using the Tab key are grouped together." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `if` statements" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x is strictly positive.\n", "5\n", "finished.\n" ] } ], "source": [ "x = 5\n", "if x > 0:\n", " print('x is strictly positive.')\n", " print(x)\n", " \n", "print('finished.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Visualize the above on [PythonTutor](http://pythontutor.com/visualize.html#code=x%20%3D%205%0Aif%20x%20%3E%200%3A%0A%20%20%20%20print%28'x%20is%20strictly%20positive.'%29%0A%20%20%20%20print%28x%29%0A%20%20%20%20%0Aprint%28'finished.'%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Changing indentation " ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "finished.\n" ] } ], "source": [ "x = 0\n", "if x > 0:\n", " print('x is strictly positive.')\n", "print(x)\n", " \n", "print('finished.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Visualize the above on [PythonTutor](http://pythontutor.com/visualize.html#code=x%20%3D%200%0Aif%20x%20%3E%200%3A%0A%20%20%20%20print%28'x%20is%20strictly%20positive.'%29%0Aprint%28x%29%0A%20%20%20%20%0Aprint%28'finished.'%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `if` and `else`" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Starting.\n", "x is zero.\n", "finished.\n" ] } ], "source": [ "x = 0\n", "print('Starting.')\n", "if x > 0:\n", " print('x is strictly positive.')\n", "else:\n", " if x < 0:\n", " print('x is strictly negative.')\n", " else:\n", " print('x is zero.')\n", "print('finished.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Visualize the above on [PythonTutor](http://pythontutor.com/live.html#code=x%20%3D%200%0Aprint%28'Starting.'%29%0Aif%20x%20%3E%200%3A%0A%20%20%20%20print%28'x%20is%20strictly%20positive.'%29%0Aelse%3A%0A%20%20%20%20if%20x%20%3C%200%3A%0A%20%20%20%20%20%20%20%20print%28'x%20is%20strictly%20negative.'%29%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20print%28'x%20is%20zero.'%29%0Aprint%28'finished.'%29&cumulative=false&curInstr=6&heapPrimitives=nevernest&mode=display&origin=opt-live.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `elif`" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Starting.\n", "x is zero\n", "finished.\n" ] } ], "source": [ "print('Starting.')\n", "if x > 0:\n", " print('x is strictly positive')\n", "elif x < 0:\n", " print('x is strictly negative')\n", "else:\n", " print('x is zero')\n", "print('finished.')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Lists\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use _lists_ to hold an ordered sequence of values." ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['first', 'second', 'third']" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = ['first', 'second', 'third']\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists can contain different types of variable, even in the same list." ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['first', 'second', 'third', 1, 2, 3]" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "another_list = ['first', 'second', 'third', 1, 2, 3]\n", "another_list" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" }, "toc-hr-collapsed": true, "toc-nb-collapsed": true }, "source": [ "## Mutable Datastructures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists are _mutable_; their contents can change as more statements are interpreted." ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['first', 'second', 'third', 'fourth']" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.append('fourth')\n", "l" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" }, "toc-hr-collapsed": true, "toc-nb-collapsed": true }, "source": [ "## References\n", "\n", "- Whenever we bind a variable to a value in Python we create a *reference*.\n", "\n", "- A reference is distinct from the value that it refers to.\n", "\n", "- Variables are names for references.\n" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "X = [1, 2, 3]\n", "Y = X" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Side effects" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- The above code creates two different references (named `X` and `Y`) to the *same* value `[1, 2, 3]`\n", "\n", "- Because lists are mutable, changing them can have side-effects on other variables.\n", "\n", "- If we append something to `X` what will happen to `Y`?" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X.append(4)\n", "X" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Visualize the above on [PythonTutor](http://pythontutor.com/visualize.html#code=X%20%3D%20%5B1,%202,%203%5D%0AY%20%3D%20X%0AX.append%284%29%0Aprint%28Y%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## State and identity\n", "\n", "- The state referred to by a variable is *different* from its identity.\n", "\n", "- To compare *state* use the `==` operator.\n", "\n", "- To compare *identity* use the `is` operator.\n", "\n", "- When we compare identity we check equality of references.\n", "\n", "- When we compare state we check equality of values.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example\n", "\n", "- We will create two *different* lists, with two associated variables." ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "X = [1, 2]\n", "Y = [1]\n", "Y.append(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Visualize the above code on [PythonTutor](http://pythontutor.com/visualize.html#code=X%20%3D%20%5B1,%202%5D%0AY%20%3D%20%5B1%5D%0AY.append%282%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comparing state" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2]" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2]" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Y" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X == Y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comparing identity" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X is Y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Copying data prevents side effects\n", "\n", "- In this example, because we have two different lists we avoid side effects" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "[1, 2]" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Y.append(3)\n", "X\n" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X == Y" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X is Y" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" }, "toc-hr-collapsed": true, "toc-nb-collapsed": true }, "source": [ "## Iteration\n", "\n", "- We can iterate over each element of a list in turn using a `for` loop:\n" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "first\n", "second\n", "third\n", "fourth\n" ] } ], "source": [ "my_list = ['first', 'second', 'third', 'fourth']\n", "for i in my_list:\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Visualize the above on [PythonTutor](http://pythontutor.com/visualize.html#code=my_list%20%3D%20%5B'first',%20'second',%20'third',%20'fourth'%5D%0Afor%20i%20in%20my_list%3A%0A%20%20%20%20print%28i%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Including more than one statement inside the loop" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The next item is:\n", "first\n", "\n", "The next item is:\n", "second\n", "\n", "The next item is:\n", "third\n", "\n", "The next item is:\n", "fourth\n", "\n" ] } ], "source": [ "my_list = ['first', 'second', 'third', 'fourth']\n", "for i in my_list:\n", " print(\"The next item is:\")\n", " print(i)\n", " print()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Visualize the above code on [PythonTutor](http://pythontutor.com/visualize.html#code=my_list%20%3D%20%5B'first',%20'second',%20'third',%20'fourth'%5D%0Afor%20i%20in%20my_list%3A%0A%20%20%20%20print%28%22The%20next%20item%20is%3A%22%29%0A%20%20%20%20print%28i%29%0A%20%20%20%20print%28%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Looping a specified number of times" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To perform a statement a certain number of times, we can iterate over a list of the required size." ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello!\n", "Hello!\n", "Hello!\n", "Hello!\n" ] } ], "source": [ "for i in [0, 1, 2, 3]:\n", " print(\"Hello!\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### The `range` function\n", "\n", "- To save from having to manually write the numbers out, we can use the function `range()` to count for us. \n", "\n", "- We count starting at 0 (as in Java and C++)." ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3]" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(4))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### `for` loops with the `range` function" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello!\n", "Hello!\n", "Hello!\n", "Hello!\n" ] } ], "source": [ "for i in range(4):\n", " print(\"Hello!\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## List Indexing\n", "\n", "- Lists can be indexed using square brackets to retrieve the element stored in a particular position.\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['first', 'second', 'third', 'fourth']" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'first'" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list[0]" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'second'" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list[1]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## List Slicing\n", "\n", "- We can also a specify a _range_ of positions. \n", "\n", "- This is called _slicing_.\n", "\n", "- The example below indexes from position 0 (inclusive) to 2 (exclusive).\n", "\n" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['first', 'second']" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list[0:2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Indexing from the start or end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- If we leave out the starting index it implies the beginning of the list:\n", "\n" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['first', 'second']" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list[:2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- If we leave out the final index it implies the end of the list:" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['third', 'fourth']" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list[2:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Copying a list\n", "\n", "- We can conveniently copy a list by indexing from start to end:\n" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "new_list = my_list[:]" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['first', 'second', 'third', 'fourth']" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_list" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_list is my_list" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_list == my_list" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Negative Indexing\n", "\n", "- Negative indices count from the end of the list:\n", "\n" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'fourth'" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list[-1]" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['first', 'second', 'third']" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list[:-1]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" }, "toc-hr-collapsed": false }, "source": [ "## Collections\n", "\n", "- Lists are an example of a *collection*.\n", "\n", "- A collection is a type of value that can contain other values.\n", "\n", "- There are other collection types in Python:\n", "\n", " - `tuple`\n", " - `set`\n", " - `dict`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Tuples\n", "\n", "- Tuples are another way to combine different values.\n", "\n", "- The combined values can be of different types.\n", "\n", "- Like lists, they have a well-defined ordering and can be indexed.\n", "\n", "- To create a tuple in Python, use round brackets instead of square brackets" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(50, 'hello')" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tuple1 = (50, 'hello')\n", "tuple1" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "50" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tuple1[0]" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(tuple1)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Tuples are immutable\n", "\n", "- Unlike lists, tuples are *immutable*. Once we have created a tuple we cannot add values to it.\n", "\n" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'tuple' object has no attribute 'append'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\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[0mtuple1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" ] } ], "source": [ "tuple1.append(2)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Sets\n", "\n", "- Lists can contain duplicate values.\n", "\n", "- A set, in contrast, contains no duplicates.\n", "\n", "- Sets can be created from lists using the `set()` function.\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4}" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = set([1, 2, 3, 3, 4])\n", "X" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "set" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(X)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Alternatively we can write a set literal using the `{` and `}` brackets." ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "set" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = {1, 2, 3, 4}\n", "type(X)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Sets are mutable\n", "\n", "- Sets are mutable like lists:" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 5}" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X.add(5)\n", "X" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Duplicates are automatically removed" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 5}" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X.add(5)\n", "X\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Sets are unordered\n", "\n", "- Sets do not have an ordering.\n", "\n", "- Therefore we cannot index or slice them:\n", "\n" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'set' object is not subscriptable", "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[0mX\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\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: 'set' object is not subscriptable" ] } ], "source": [ "X[0]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Operations on sets\n", "\n", "- Union: $X \\cup Y$\n" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 5, 6}" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = {1, 2, 3}\n", "Y = {4, 5, 6}\n", "X | Y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Intersection: $X \\cap Y$:" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{3, 4}" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = {1, 2, 3, 4}\n", "Y = {3, 4, 5}\n", "X & Y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Difference $X - Y$:\n" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2}" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X - Y" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Dictionaries\n", "\n", "- A dictionary contains a mapping between *keys*, and corresponding *values*.\n", " \n", " - Mathematically it is a one-to-one function with a finite domain and range.\n", " \n", "- Given a key, we can very quickly look up the corresponding value.\n", "\n", "- The values can be any type (and need not all be of the same type).\n", "\n", "- Keys can be any immutable (hashable) type.\n", "\n", "- They are abbreviated by the keyword `dict`.\n", "\n", "- In other programming languages they are sometimes called *associative arrays*." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Creating a dictionary\n", "\n", "- A dictionary contains a set of key-value pairs.\n", "\n", "- To create a dictionary:\n" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [], "source": [ "students = { 107564: 'Xu', 108745: 'Ian', 102567: 'Steve' }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- The above initialises the dictionary students so that it contains three key-value pairs.\n", "\n", "- The keys are the student id numbers (integers).\n", "\n", "- The values are the names of the students (strings).\n", "\n", "- Although we use the same brackets as for sets, this is a different type of collection:" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(students)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Accessing the values in a dictionary\n", "\n", "- We can access the value corresponding to a given key using the same syntax to access particular elements of a list: " ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Ian'" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "students[108745]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Accessing a non-existent key will generate a `KeyError`:" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "ename": "KeyError", "evalue": "123", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyError\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[0mstudents\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m123\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mKeyError\u001b[0m: 123" ] } ], "source": [ "students[123]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Updating dictionary entries\n", "\n", "- Dictionaries are mutable, so we can update the mapping:" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Fred\n" ] } ], "source": [ "students[108745] = 'Fred'\n", "print(students[108745])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- We can also grow the dictionary by adding new keys:" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "John\n" ] } ], "source": [ "students[104587] = 'John'\n", "print(students[104587])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Dictionary keys can be any immutable type\n", "\n", "- We can use any immutable type for the keys of a dictionary\n", "\n", "- For example, we can map names onto integers:" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [], "source": [ "age = { 'John':21, 'Steve':47, 'Xu': 22 }" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "47" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "age['Steve']" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Creating an empty dictionary\n", "\n", "- We often want to initialise a dictionary with no keys or values.\n", "\n", "- To do this call the function `dict()`:" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [], "source": [ "result = dict()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "- We can then progressively add entries to the dictionary, e.g. using iteration:" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}\n" ] } ], "source": [ "for i in range(5):\n", " result[i] = i**2\n", "print(result)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Iterating over a dictionary\n", "\n", "- We can use a for loop with dictionaries, just as we can with other collections such as sets.\n", "- When we iterate over a dictionary, we iterate over the *keys*.\n", "- We can then perform some computation on each key inside the loop.\n", "- Typically we will also access the corresponding value." ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Xu\n", "Fred\n", "Steve\n", "John\n" ] } ], "source": [ "for id in students:\n", " print(students[id])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The size of a collection\n", "\n", "- We can count the number of values in a collection using the `len` (length) function.\n", "\n", "- This can be used with any type of collection (list, set, tuple etc.).\n" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(students)" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(['one', 'two'])" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len({'one', 'two', 'three'})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Empty collections" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Empty collections have a size of zero:" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "empty_list = []\n", "len(empty_list) == 0" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Arrays\n", "\n", "- Python also has arrays which contain a *single* type of value.\n", "\n", "- i.e. we *cannot* have different types of value within the same array. \n", "\n", "- Arrays are mutable like lists; we can modify the existing elements of an array.\n", "\n", "- However, we typically do not change the size of the array; i.e. it has a fixed length." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" }, "toc-hr-collapsed": true, "toc-nb-collapsed": true }, "source": [ "## The `numpy` module\n", "\n", "- Arrays are provided by a separate _module_ called numpy. Modules correspond to packages in e.g. Java.\n", "\n", "- We can import the module and then give it a shorter _alias_." ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- We can now use the functions defined in this package by prefixing them with `np`. \n", "\n", "- The function `array()` creates an array given a list." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Creating an array\n", "\n", "- We can create an array from a list by using the `array()` function defined in the `numpy` module:" ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4])" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.array([0, 1, 2, 3, 4])\n", "x" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(x)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Functions over arrays\n", "\n", "- When we use arithmetic operators on arrays, we create a new array with the result of applying the operator to each element." ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 2, 4, 6, 8])" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = x * 2\n", "y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- The same goes for functions:" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4])" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.array([-1, 2, 3, -4])\n", "y = abs(x)\n", "y" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Populating Arrays\n", "\n", "- To populate an array with a range of values we use the `np.arange()` function:\n" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.arange(0, 10)\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- We can also use floating point increments.\n" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.arange(0, 1, 0.1)\n", "x" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Basic Plotting\n", "\n", "- We will use a module called `matplotlib` to plot some simple graphs.\n", "\n", "- This module provides functions which are very similar to MATLAB plotting commands.\n" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD4CAYAAADiry33AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3deXhU9dn/8fcNYQv7EnbCIvuqOICIG66IoiK21Vp3RW217dNWwZ3ivrTV+lMRW60+daklbIos8rhRFzRYSFgSlrAkhH0NCSHb/fsjYxtjgAkkmcnk87quXJk553tm7vmSfHI4c+Y+5u6IiEj0qhXuAkREpHIp6EVEopyCXkQkyinoRUSinIJeRCTKxYS7gLK0atXKu3TpEu4yRESqjSVLlux097iy1kVk0Hfp0oXExMRwlyEiUm2Y2cbDrdOhGxGRKKegFxGJcgp6EZEop6AXEYlyCnoRkSinoBcRiXIKehGRKKegFxGJAN9s2M2UT9dVymNH5AemRERqigOHCnhqXgpvfLmR+BaxXDu8M7F1KzaaFfQiImHySep27puxnMx9B7lhRBd+d36vCg95UNCLiFS5Pdl5PDxnJdO/3Uz31o2YdtupnNy5eaU9X0hBb2bNgL8A/QEHbnT3L0usvwu4usRj9gHi3H23mW0AsoBCoMDdAxVXvoh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dVwN/c/Q9mNhz43+B8FFV+eZWnuu7Rh3LR8f+MMbMYiv8bFo2XMQzpAuxmdi5wH3CJux+qotrC4Wjz0RjoD3xiZhuAU4DZUfqGbKi/J7PcPd/d1wOpFAd/NAplPm4C3gVw9y+B+hQ3PKvWqmvQfwP0MLOuZlaX4jdbZ5caMxu4Lnj7CuAjD77DEmWOOhfBQxUvUxzy0XwMFo4yH+6+z91buXsXd+9C8XsWl7h7NF67MpTfk5kUv1mPmbWi+FBOWpVWWXVCmY9NwDkAZtaH4qDfUaVVVoJqGfTBY+53APOBVcC77r7CzCab2SXBYX8FWprZWuA3wGFPs6vOQpyLp4FGwD/NbKmZlf7hjhohzkeNEOJczAd2mdlK4GPgLnffFZ6KK1eI8/Fb4BYzWwa8DVwfDTuIaoEgIhLlquUevYiIhE5BLyIS5RT0IiJRTkEvIhLlFPQiIlFOQS8iEuUU9CIiUe7/A/YiIDC1nL75AAAAAElFTkSuQmCC\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "import matplotlib.pyplot as plt\n", "\n", "y = x*2 + 5\n", "plt.plot(x, y)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Plotting a sine curve" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAD4CAYAAADhNOGaAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3dd3hUZfrG8e+THkICpBAgCTUhNKkRRASRbgN1bai71kVXXVHXXVF3ddd1f6uuu6soFiwr2BArNqSDhRoQ6SWEQEJLQgiQXub9/ZGDV8SElJnkTHk+1zVXZs6cM3PHknveOeUVYwxKK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ktYh8KCI7rP8XhtqeyVf2EYiIP7ALGEvVZDnrgMnGmG22BqsnERkBFACzjTF97M7TGCLSHmhvjNkgIuHAeuAyD/p3IECYMaZARAKB74CpxpjVNkdrEBG5H0gBIowxl9idp6FEJANIMcZ45AllIjIL+NYY85o1R0sLY0y+nZl8aUQwGEgzxqQbY8qAOcAkmzPVmzHmGyDP7hzOMMYcMsZssO6fpGp+ilrnqXY3pkqB9TDQunnUJykRiQcuBl6zO4svEpEIYATW3CvGmDK7SwB8qwjigMxqj7PwoD9C3kZEOgMDgDX2JmkY62uVjUA2sMgY41H5gWeBPwEOu4M4wQALRWS9iEyxO0wDdQVygP9ZX8+9JiJhdofypSKQGpZ51Kc5byEiLYGPgHuNMSfsztMQxphKY0x/qubYHiwiHvM1nYhcAmQbY9bbncVJw4wxA4ELgbusr009RQAwEHjJGDMAKARs31/pS0WQBSRUexwPHLQpi8+yvlv/CHjHGPOx3XkayxrOLwcm2BylIYYBE63v2OcAo0TkbXsjNZwx5qD1Mxv4hKqvfT1FFpBVbST5IVXFYCtfKoJ1QJKIdLF20FwLfGZzJp9i7Wx9HdhujPmP3XkaSkRiRKS1dT8UGAPssDdV/RljHjLGxBtjOlP13/9SY8wNNsdqEBEJsw40wPpKZRzgMUfSGWMOA5kikmwtGg3YfrBEgN0BmosxpkJE7gYWAP7AG8aYrTbHqjcReQ8YCUSLSBbwmDHmdXtTNdgw4NfAZut7doCHjTFf2ZipIdoDs6wj0PyAucYYjzwE04PFAp9UfaYgAHjXGPO1vZEa7PfAO9YH0nTgZpvz+M7ho0oppWrmS18NKaWUqoEWgVJK+TgtAqWU8nFaBEop5eO0CJRSysdpESillI/TIlBKKR/3/5mfHUNzAEwCAAAAAElFTkSuQmCC\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "from numpy import pi, sin\n", "\n", "x = np.arange(0, 2*pi, 0.01)\n", "y = sin(x)\n", "plt.plot(x, y)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Plotting a histogram\n", "\n", "- We can use the `hist()` function in `matplotlib` to plot a histogram" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAOH0lEQVR4nO3df6jd9X3H8eer6txQh4pXSWPYlZKN2bHFcrEFx3DY1V+j0T8cymizTkgLyhQ6MLUwuw0hZasdHZssRWkEqxNUFHSb1jmcf/jjRjJ/RdfQphoTzG1dqyJ0RN/7434zT+NJ7jn33Jtz7sfnAw7f7/dzPt/zfX9I7ivffM73+72pKiRJbfnIuAuQJC09w12SGmS4S1KDDHdJapDhLkkNOnrcBQCccsopNT09Pe4yJGlF2bZt24+raqrfexMR7tPT08zOzo67DElaUZL86FDvLTgtk2RNkkeT7EjyQpJruvavJXktyfbudVHPPl9JsjPJy0nOX5phSJIGNciZ+37gy1X1TJITgG1JHu7e+2ZV/W1v5yRnApcDHwc+Cnwvya9X1btLWbgk6dAWPHOvqr1V9Uy3/hawA1h9mF3WA3dW1c+r6ofATuDspShWkjSYoa6WSTINnAU82TVdneTZJLcmOalrWw282rPbbvr8Y5BkY5LZJLNzc3NDFy5JOrSBwz3J8cDdwLVV9SZwM/AxYB2wF/jGga59dv/AA2yqaktVzVTVzNRU3y97JUmLNFC4JzmG+WC/varuAaiq16vq3ap6D/g270+97AbW9Ox+OrBn6UqWJC1kkKtlAtwC7Kiqm3raV/V0uxR4vlu/H7g8ybFJzgDWAk8tXcmSpIUMcrXMOcDngOeSbO/argeuSLKO+SmXXcAXAarqhSR3AS8yf6XNVV4pI0lH1oLhXlWP038e/cHD7HMjcO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JId3d+la8Zd07CS/HKSp5L8VzeGvxx3TYcy8XPuSX61qt7s1v8MOLOqvjTmsoaS5DPAv3dfNH8doKquG3NZQ0nym8B7wD8Bf15Vs2MuaSDdozD+G/gD5i/RfRq4oqpeHGthQ0rye8DbwG1V9VvjrmcxkqwCVlXVM0lOALYBl6ykP4skAY6rqreTHAM8DlxTVU+MubQPmPgz9wPB3jmOg26GWgmq6qGq2t9tPsH8df8rSlXtqKp+dxFPuiYehVFVjwFvjLuOUVTV3qp6plt/C9jB/N3sK0bNe7vbPKZ7TWQmTXy4AyS5McmrwB8DfzHuekb0p8C/jLuID5F+j8JYUYHSoiTTwFnAk+OtZHhJjkqyHdgHPFxVEzmGiQj3JN9L8nyf13qAqvpqVa0BbgeuHm+1/S00hq7PV4H9zI9j4gwyhhVowUdh6MhKcjxwN3DtQf8zXxGq6t2qWsf8/8DPTjKR02QT8cs6qurTA3b9LvAAcMMylrMoC40hyQbgD4HzakK/6Bjiz2El8VEYE6Sbp74buL2q7hl3PaOoqp8m+Q/gAmDivuieiDP3w0mytmfzs8BL46plsZJcAFwHfLaq3hl3PR8yPgpjQnRfRt4C7Kiqm8Zdz2IkmTpwtVuSXwE+zYRm0kq4WuZu4DeYv1LjR8CXquq18VY1nCQ7gWOBn3RNT6zAK34uBf4emAJ+CmyvqvPHW9VgklwE/B3vPwrjxjGXNLQkdwDnMv+o2deBG6rqlrEWNaQkvwv8J/Ac8z/PANdX1YPjq2o4SX4b2Mr836WPAHdV1V+Nt6r+Jj7cJUnDm/hpGUnS8Ax3SWqQ4S5JDTLcJalBhrskNchwl6QGGe6S1KD/A4z0ktD4JfaYAAAAAElFTkSuQmCC\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "# Generate some random data\n", "data = np.random.randn(1000)\n", "\n", "ax = plt.hist(data)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Computing histograms as matrices\n", "\n", "- The function `histogram()` in the `numpy` module will count frequencies into bins and return the result as a 2-dimensional array." ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([ 14, 41, 128, 178, 243, 203, 109, 66, 14, 4]),\n", " array([-2.81515826, -2.19564948, -1.57614071, -0.95663193, -0.33712315,\n", " 0.28238562, 0.9018944 , 1.52140318, 2.14091195, 2.76042073,\n", " 3.3799295 ]))" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.histogram(data)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Defining new functions\n", "\n" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def squared(x):\n", " return x ** 2\n", "\n", "squared(5)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Local Variables\n", "\n", "- Variables created inside functions are _local_ to that function.\n", "\n", "- They are not accessable to code outside of that function." ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def squared(x):\n", " temp = x ** 2\n", " return temp\n", "\n", "squared(5)" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'temp' 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[0mtemp\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'temp' is not defined" ] } ], "source": [ "temp" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Functional Programming\n", "\n", "- Functions are first-class citizens in Python.\n", "\n", "- They can be passed around just like any other value." ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "squared" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = squared\n", "y" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y(5)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Mapping the elements of a collection\n", "\n", "- We can apply a function to each element of a collection using the built-in function `map()`.\n", "\n", "- This will work with any collection: list, set, tuple or string.\n", "\n", "- This will take as an argument _another function_, and the list we want to apply it to.\n", "\n", "- It will return the results of applying the function, as a list." ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 9, 16]" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(map(squared, [1, 2, 3, 4]))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## List Comprehensions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Because this is such a common operation, Python has a special syntax to do the same thing, called a _list comprehension_.\n" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 9, 16]" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[squared(i) for i in [1, 2, 3, 4]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- If we want a set instead of a list we can use a set comprehension" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 4, 9, 16}" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{squared(i) for i in [1, 2, 3, 4]}" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Cartesian product using list comprehensions\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " image courtesy of [Quartl](https://commons.wikimedia.org/wiki/User:Quartl)\n", "\n", "The [Cartesian product](https://en.wikipedia.org/wiki/Cartesian_product) of two collections $X = A \\times B$ can be expressed by using multiple `for` statements in a comprehension.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### example" ] }, { "cell_type": "code", "execution_count": 109, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "{('x', 1),\n", " ('x', 2),\n", " ('x', 3),\n", " ('y', 1),\n", " ('y', 2),\n", " ('y', 3),\n", " ('z', 1),\n", " ('z', 2),\n", " ('z', 3)}" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = {'x', 'y', 'z'}\n", "B = {1, 2, 3}\n", "{(a,b) for a in A for b in B}" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Cartesian products with other collections\n", "\n", "- The syntax for Cartesian products can be used with any collection type.\n" ] }, { "cell_type": "code", "execution_count": 110, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "[('Steve', 'Smith'),\n", " ('Steve', 'Doe'),\n", " ('Steve', 'Rabbit'),\n", " ('John', 'Smith'),\n", " ('John', 'Doe'),\n", " ('John', 'Rabbit'),\n", " ('Peter', 'Smith'),\n", " ('Peter', 'Doe'),\n", " ('Peter', 'Rabbit')]" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "first_names = ('Steve', 'John', 'Peter')\n", "surnames = ('Smith', 'Doe', 'Rabbit')\n", "\n", "[(first_name, surname) for first_name in first_names for surname in surnames]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Joining collections using a zip\n", "\n", "- The Cartesian product pairs every combination of elements.\n", "\n", "- If we want a 1-1 pairing we use an operation called a zip.\n", "\n", "- A zip pairs values at the same position in each sequence.\n", "\n", "- Therefore:\n", " - it can only be used with sequences (not sets); and\n", " - both collections must be of the same length." ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('Steve', 'Smith'), ('John', 'Doe'), ('Peter', 'Rabbit')]" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(zip(first_names, surnames))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Anonymous Function Literals\n", "\n", "- We can also write _anonymous_ functions.\n", "- These are function literals, and do not necessarily have a name.\n", "- They are called _lambda expressions_ (after the $\\lambda-$calculus)." ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 9, 16]" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(map(lambda x: x ** 2, [1, 2, 3, 4]))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Filtering data\n", "\n", "- We can filter a list by applying a _predicate_ to each element of the list.\n", "\n", "- A predicate is a function which takes a single argument, and returns a boolean value.\n", "\n", "- `filter(p, X)` is equivalent to $\\{ x : p(x) \\; \\forall x \\in X \\}$ in set-builder notation.\n" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 1]" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(filter(lambda x: x > 0, [-5, 2, 3, -10, 0, 1]))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "We can use both `filter()` and `map()` on other collections such as strings or sets." ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(filter(lambda x: x > 0, {-5, 2, 3, -10, 0, 1}))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Filtering using a list comprehension\n", "\n", "- Again, because this is such a common operation, we can use simpler syntax to say the same thing.\n", "\n", "- We can express a filter using a list-comprehension by using the keyword `if`:" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 1]" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = [-5, 2, 3, -10, 0, 1]\n", "[x for x in data if x > 0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- We can also filter and then map in the same expression:" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1.4142135623730951, 1.7320508075688772, 1.0]" ] }, "execution_count": 116, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from numpy import sqrt\n", "[sqrt(x) for x in data if x > 0]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## The reduce function\n", "\n", "- The `reduce()` function recursively applies another function to pairs of values over the entire list, resulting in a _single_ return value." ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from functools import reduce\n", "reduce(lambda x, y: x + y, [0, 1, 2, 3, 4, 5])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Big Data\n", "\n", "- The `map()` and `reduce()` functions form the basis of the map-reduce programming model.\n", "\n", "- [Map-reduce](https://en.wikipedia.org/wiki/MapReduce) is the basis of modern highly-distributed large-scale computing frameworks.\n", "\n", "- It is used in BigTable, Hadoop and Apache Spark. \n", "\n", "- See [these examples in Python](https://spark.apache.org/examples.html) for Apache Spark." ] } ], "metadata": { "celltoolbar": "Edit Metadata", "ipub": { "customcss": "fitch.css", "titlepage": { "author": "(c) Steve Phelps 2019", "email": "sphelps@sphelps.net", "logo": "figs/fitch-logo.jpeg", "title": "Scientific Computing for Finance using Python" } }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" }, "latex_metadata": { "chapter": { "setcounter": 2 }, "title": "Overview of Python" }, "toc": { "base_numbering": "1", "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": { "height": "calc(100% - 180px)", "left": "10px", "top": "150px", "width": "295.4px" }, "toc_section_display": true, "toc_window_display": true }, "toc-autonumbering": true, "toc-showmarkdowntxt": true, "toc-showtags": false }, "nbformat": 4, "nbformat_minor": 4 }