├── .gitignore ├── old_notebooks ├── intro_to_python.ipynb └── Lesson1_Python_Basics.ipynb ├── README.md ├── data └── course_stats.ipynb └── one_day_workshop ├── 03_pandas_earthquake_blank.ipynb ├── 04_xray_sst_blank.ipynb ├── 02_numpy_and_matplotlib_blank.ipynb ├── 01_core_python_blank.ipynb └── 01_core_python.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *.ipynb_* 3 | *.egg-info/ 4 | *.egg 5 | pyqg/*.so 6 | pyqg/*.c 7 | .installed.cfg 8 | nosetests.xml 9 | build/* 10 | -------------------------------------------------------------------------------- /old_notebooks/intro_to_python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "gist_id": "f2ea22dcd7e5e449d4bc", 4 | "name": "", 5 | "signature": "sha256:be3e79be54f98e31e5fe40d63a7c8f3fdebbea649ffb84b765ba9ac11fc55015" 6 | }, 7 | "nbformat": 3, 8 | "nbformat_minor": 0, 9 | "worksheets": [ 10 | { 11 | "cells": [ 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Overview\n", 17 | "\n", 18 | "Basically just using the official [python tutorial](https://docs.python.org/2/tutorial/).\n", 19 | "\n", 20 | "### Python Basics\n", 21 | "1. Python (iPython) shell\n", 22 | "* Python files\n", 23 | "* iPython notebook\n", 24 | "* Everything is an object\n", 25 | "* Numbers and strings\n", 26 | "* Collections of items\n", 27 | " - Lists\n", 28 | " - Tuples\n", 29 | " - Dictionaries\n", 30 | "* Flow Control\n", 31 | " - Conditionals\n", 32 | " - Loops\n", 33 | " - Exceptions\n", 34 | "\n", 35 | "### Modules\n", 36 | "1. The Namespace\n", 37 | "* Importing\n", 38 | "* Functions\n", 39 | "* Organizing your code in modules\n", 40 | "\n", 41 | "### NumPy\n", 42 | "\n", 43 | "### SciPy\n", 44 | "\n", 45 | "### Links to Data\n", 46 | "\n", 47 | "ftp://ftp.cdc.noaa.gov/Datasets/ncep.reanalysis/surface/air.sig995.2012.nc\n", 48 | "\n", 49 | "http://www.cpc.ncep.noaa.gov/products/precip/CWlink/daily_ao_index/monthly.ao.index.b50.current.ascii\n" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "collapsed": false, 55 | "input": [], 56 | "language": "python", 57 | "metadata": {}, 58 | "outputs": [] 59 | } 60 | ], 61 | "metadata": {} 62 | } 63 | ] 64 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python for Scientific Computing # 2 | 3 | Workshop held at [Lamont Doherty Earth Observatory](http://ldeo.columbia.edu) on Aug. 21, 2015. 4 | 5 | ## Attendance Data ## 6 | 7 | Affiliation | Count 8 | ------------|------ 9 | LDEO Research Scientist | 15 10 | DEES Grad Student | 14 11 | LDEO/IRI Postdoc | 7 12 | DEES / LDEO Faculty | 7 13 | APAM Postdoc | 2 14 | APAM Grad Student | 2 15 | Other Postdoc | 2 16 | Other | 10 17 | 18 | ## Course Materials as IPython Notebooks ## 19 | 20 | * [Lesson 1: Basic Python](http://nbviewer.ipython.org/github/rabernat/python_teaching/blob/master/one_day_workshop/01_core_python.ipynb) 21 | * [Lesson 2: Numpy and Matplotlib](http://nbviewer.ipython.org/github/rabernat/python_teaching/blob/master/one_day_workshop/02_numpy_and_matplotlib.ipynb) 22 | * [Lesson 3: Pandas with Earthquake Data](http://nbviewer.ipython.org/github/rabernat/python_teaching/blob/master/one_day_workshop/03_pandas_earthquake.ipynb) 23 | * [Lesson 4: xray and Pacific SSTs](http://nbviewer.ipython.org/github/rabernat/python_teaching/blob/master/one_day_workshop/04_xray_sst.ipynb) 24 | 25 | ## Data Download Links ## 26 | 27 | * ARGO Float Data: http://www.ldeo.columbia.edu/~rpa/argo_float_4901412.npz 28 | * USGS Earthquake Data: http://www.ldeo.columbia.edu/~rpa/usgs_earthquakes_2014.csv 29 | * ERSST Data: http://iridl.ldeo.columbia.edu/SOURCES/.NOAA/.NCDC/.ERSST/.version4/anom/.sst/T/(days%20since%201960-01-01)/streamgridunitconvert/dods 30 | 31 | ## Schedule ## 32 | 33 | Time | Activity 34 | ------ | -------- 35 | 9:00am | Intro Presentation 36 | 9:15am | Python core language and data structures 37 | 10:00am | Individual Exercise 1: Fibbonacci sequence and list adding 38 | 10:30am | Numpy and matplotlib 39 | 12:00pm | Lunch 40 | 1:00pm | Pandas for tabular data analysis 41 | 2:30pm | xray for multidimensional data analysis 42 | 43 | 44 | ## Other Resources ## 45 | 46 | There are lots of great resources out there. First, the official documentation for the main ingredients of the SciPy ecosystem: 47 | * The [official python documentation](https://www.python.org/doc/) 48 | * [SciPy.org](http://www.scipy.org/), the home base for scientific python 49 | * [NumPy](http://www.numpy.org/) for everying to do with numerical arrays 50 | * [MatPlotLib](http://matplotlib.org/) for plotting 51 | * [IPython](http://ipython.org/) for the rich interactive environment 52 | 53 | These tutorial materials are about general python and are not specific to SciPy/ Numpy: 54 | * The [official python tutorial](https://docs.python.org/2/tutorial/) 55 | * The [Google python class](https://developers.google.com/edu/python/) 56 | * [Dive into python](http://www.diveintopython.net/toc/index.html) 57 | 58 | These are tutorials more specifically focused on scientific use: 59 | * [Python Scientific Lectures](http://scipy-lectures.github.io/) 60 | * [Python for Geosciences](https://github.com/koldunovn/python_for_geosciences) 61 | * A [gallery of interesting IPython notebooks](https://github.com/ipython/ipython/wiki/A-gallery-of-interesting-IPython-Notebooks) 62 | 63 | Some good blogs about python and science: 64 | * [Jake Vanderplas](http://jakevdp.github.io/) 65 | * [Fernando Perez](http://blog.fperez.org/) 66 | 67 | 68 | -------------------------------------------------------------------------------- /data/course_stats.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 19, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import pandas as pd\n", 12 | "from matplotlib import pyplot as plt\n", 13 | "%matplotlib inline" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 31, 19 | "metadata": { 20 | "collapsed": true 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "d = pd.read_csv('https://docs.google.com/spreadsheets/d/'\n", 25 | " '1-E_du9XFhNzjW4n0KIhCZGKcu-AxirPqNtrZn8Oty-E/pub?output=csv')" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 36, 31 | "metadata": { 32 | "collapsed": false 33 | }, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "Status\n", 39 | "LDEO Research Scientist 15\n", 40 | "DEES Grad Student 14\n", 41 | "LDEO/IRI Postdoc 7\n", 42 | "DEES / LDEO Faculty (any professor title) 7\n", 43 | "APAM Postdoc 2\n", 44 | "APAM Grad Student 2\n", 45 | "Other Postdoc 2\n", 46 | "Other Grad Student 1\n", 47 | "LDEO research staff assistant 1\n", 48 | "LDEO Staff Associate 1\n", 49 | "System Analyst 1\n", 50 | "LDEO Computer Programmer 1\n", 51 | "IRI Research Scientist 1\n", 52 | "IRI Research Assistant 1\n", 53 | "Research Assistant 1\n", 54 | "CUNY Grad Student 1\n", 55 | "Senior Research Staff 1\n", 56 | "Undergrad 1\n", 57 | "Name: Status, dtype: int64" 58 | ] 59 | }, 60 | "execution_count": 36, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "g = d.groupby('Status')['Status'].count('Status')\n", 67 | "g.sort(ascending=False)\n", 68 | "g" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "metadata": { 75 | "collapsed": true 76 | }, 77 | "outputs": [], 78 | "source": [] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": { 84 | "collapsed": true 85 | }, 86 | "outputs": [], 87 | "source": [] 88 | } 89 | ], 90 | "metadata": { 91 | "kernelspec": { 92 | "display_name": "Python 3", 93 | "language": "python", 94 | "name": "python3" 95 | }, 96 | "language_info": { 97 | "codemirror_mode": { 98 | "name": "ipython", 99 | "version": 3 100 | }, 101 | "file_extension": ".py", 102 | "mimetype": "text/x-python", 103 | "name": "python", 104 | "nbconvert_exporter": "python", 105 | "pygments_lexer": "ipython3", 106 | "version": "3.4.3" 107 | } 108 | }, 109 | "nbformat": 4, 110 | "nbformat_minor": 0 111 | } 112 | -------------------------------------------------------------------------------- /one_day_workshop/03_pandas_earthquake_blank.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import numpy as np\n", 12 | "import pandas as pd\n", 13 | "from matplotlib import pyplot as plt\n", 14 | "%matplotlib inline\n" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 2, 20 | "metadata": { 21 | "collapsed": false 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "uri = 'http://www.ldeo.columbia.edu/~rpa/usgs_earthquakes_2014.csv'" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 18, 31 | "metadata": { 32 | "collapsed": false 33 | }, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | " % Total % Received % Xferd Average Speed Time Time Time Current\n", 40 | " Dload Upload Total Spent Left Speed\n", 41 | "100 18.5M 100 18.5M 0 0 86265 0 0:03:45 0:03:45 --:--:-- 81187\n" 42 | ] 43 | } 44 | ], 45 | "source": [ 46 | "! curl -O http://www.ldeo.columbia.edu/~rpa/usgs_earthquakes_2014.csv" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 2, 52 | "metadata": { 53 | "collapsed": false 54 | }, 55 | "outputs": [], 56 | "source": [ 57 | "d = pd.read_csv('usgs_earthquakes_2014.csv', parse_dates = ['time'], index_col='time')" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 1, 63 | "metadata": { 64 | "collapsed": false 65 | }, 66 | "outputs": [], 67 | "source": [ 68 | "# head\n" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 2, 74 | "metadata": { 75 | "collapsed": false 76 | }, 77 | "outputs": [], 78 | "source": [ 79 | "# describe\n" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 3, 85 | "metadata": { 86 | "collapsed": false, 87 | "scrolled": false 88 | }, 89 | "outputs": [], 90 | "source": [ 91 | "# sort by mag\n" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 4, 97 | "metadata": { 98 | "collapsed": false 99 | }, 100 | "outputs": [], 101 | "source": [ 102 | "# histogram\n" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 5, 108 | "metadata": { 109 | "collapsed": false 110 | }, 111 | "outputs": [], 112 | "source": [ 113 | "# daily timeseries\n" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 6, 119 | "metadata": { 120 | "collapsed": false 121 | }, 122 | "outputs": [], 123 | "source": [ 124 | "# hourly timeseries\n", 125 | " " 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 19, 131 | "metadata": { 132 | "collapsed": false 133 | }, 134 | "outputs": [ 135 | { 136 | "data": { 137 | "text/plain": [ 138 | "matplotlib.axes._subplots.AxesSubplot" 139 | ] 140 | }, 141 | "execution_count": 19, 142 | "metadata": {}, 143 | "output_type": "execute_result" 144 | } 145 | ], 146 | "source": [ 147 | "type(ax)" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 7, 153 | "metadata": { 154 | "collapsed": false 155 | }, 156 | "outputs": [], 157 | "source": [ 158 | "# scatter\n" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 8, 164 | "metadata": { 165 | "collapsed": false 166 | }, 167 | "outputs": [], 168 | "source": [ 169 | "# bokeh version\n" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 9, 175 | "metadata": { 176 | "collapsed": false, 177 | "scrolled": false 178 | }, 179 | "outputs": [], 180 | "source": [ 181 | "# make bokeh figure\n" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": null, 187 | "metadata": { 188 | "collapsed": true 189 | }, 190 | "outputs": [], 191 | "source": [] 192 | } 193 | ], 194 | "metadata": { 195 | "kernelspec": { 196 | "display_name": "Python 3", 197 | "language": "python", 198 | "name": "python3" 199 | }, 200 | "language_info": { 201 | "codemirror_mode": { 202 | "name": "ipython", 203 | "version": 3 204 | }, 205 | "file_extension": ".py", 206 | "mimetype": "text/x-python", 207 | "name": "python", 208 | "nbconvert_exporter": "python", 209 | "pygments_lexer": "ipython3", 210 | "version": "3.4.3" 211 | } 212 | }, 213 | "nbformat": 4, 214 | "nbformat_minor": 0 215 | } 216 | -------------------------------------------------------------------------------- /one_day_workshop/04_xray_sst_blank.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import xray\n", 12 | "import numpy as np\n", 13 | "from matplotlib import pyplot as plt\n", 14 | "%matplotlib inline" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 3, 20 | "metadata": { 21 | "collapsed": false, 22 | "scrolled": false 23 | }, 24 | "outputs": [], 25 | "source": [ 26 | "url = ('http://iridl.ldeo.columbia.edu/SOURCES/.NOAA/.NCDC/.ERSST/'\n", 27 | " '.version4/anom/.sst/T/(days%20since%201960-01-01)/streamgridunitconvert/dods')\n", 28 | "\n", 29 | "# open dataset\n" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 5, 35 | "metadata": { 36 | "collapsed": false 37 | }, 38 | "outputs": [], 39 | "source": [ 40 | "# make pcolormesh using xray\n" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "## Calculate Niño 3.4 Index ##" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 7, 53 | "metadata": { 54 | "collapsed": false 55 | }, 56 | "outputs": [], 57 | "source": [ 58 | "# The Niño 3.4 Region is bounded by 120°W-170°W and 5°S- 5°N.\n", 59 | "# extract region\n" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 8, 65 | "metadata": { 66 | "collapsed": false 67 | }, 68 | "outputs": [], 69 | "source": [ 70 | "# pcolormesh\n" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 9, 76 | "metadata": { 77 | "collapsed": false 78 | }, 79 | "outputs": [], 80 | "source": [ 81 | "# define index\n" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 10, 87 | "metadata": { 88 | "collapsed": false 89 | }, 90 | "outputs": [], 91 | "source": [ 92 | "# plot nino3.4 timeseries\n" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "## EOF Analysis of Tropical Pacific SST ##" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 12, 105 | "metadata": { 106 | "collapsed": true 107 | }, 108 | "outputs": [], 109 | "source": [ 110 | "# extract sst anomaly\n" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 13, 116 | "metadata": { 117 | "collapsed": false 118 | }, 119 | "outputs": [], 120 | "source": [ 121 | "# pcolormesh\n" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 14, 127 | "metadata": { 128 | "collapsed": false 129 | }, 130 | "outputs": [], 131 | "source": [ 132 | "# extract and normalize raw data as numpy array\n" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 15, 138 | "metadata": { 139 | "collapsed": false 140 | }, 141 | "outputs": [], 142 | "source": [ 143 | "# reshape\n" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 16, 149 | "metadata": { 150 | "collapsed": false 151 | }, 152 | "outputs": [], 153 | "source": [ 154 | "# calculate svd\n" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 17, 160 | "metadata": { 161 | "collapsed": false 162 | }, 163 | "outputs": [], 164 | "source": [ 165 | "# examine EOF patterns\n" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 18, 171 | "metadata": { 172 | "collapsed": false 173 | }, 174 | "outputs": [], 175 | "source": [ 176 | "# plot timeseries of EOF amplitude\n" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 19, 182 | "metadata": { 183 | "collapsed": false 184 | }, 185 | "outputs": [], 186 | "source": [ 187 | "# look at relative size of EOF components\n" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 127, 193 | "metadata": { 194 | "collapsed": false 195 | }, 196 | "outputs": [ 197 | { 198 | "data": { 199 | "text/plain": [ 200 | "2966.7355797239511" 201 | ] 202 | }, 203 | "execution_count": 127, 204 | "metadata": {}, 205 | "output_type": "execute_result" 206 | } 207 | ], 208 | "source": [ 209 | "np.sqrt((a**2).sum()) * 2" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": null, 215 | "metadata": { 216 | "collapsed": true 217 | }, 218 | "outputs": [], 219 | "source": [] 220 | } 221 | ], 222 | "metadata": { 223 | "kernelspec": { 224 | "display_name": "Python 3", 225 | "language": "python", 226 | "name": "python3" 227 | }, 228 | "language_info": { 229 | "codemirror_mode": { 230 | "name": "ipython", 231 | "version": 3 232 | }, 233 | "file_extension": ".py", 234 | "mimetype": "text/x-python", 235 | "name": "python", 236 | "nbconvert_exporter": "python", 237 | "pygments_lexer": "ipython3", 238 | "version": "3.4.3" 239 | } 240 | }, 241 | "nbformat": 4, 242 | "nbformat_minor": 0 243 | } 244 | -------------------------------------------------------------------------------- /one_day_workshop/02_numpy_and_matplotlib_blank.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Numpy and Matplotlib #\n", 8 | "\n", 9 | "These are two of the most fundamental parts of the scientific python \"ecosystem\". Most everything else is built on top of them.\n" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": { 16 | "collapsed": true 17 | }, 18 | "outputs": [], 19 | "source": [ 20 | "# import numpy\n" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "metadata": {}, 26 | "source": [ 27 | "What did we just do? We _imported_ a package. This brings new variables (mostly functions) into our interpreter. We access them as follows." 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 2, 33 | "metadata": { 34 | "collapsed": false, 35 | "scrolled": true 36 | }, 37 | "outputs": [], 38 | "source": [ 39 | "# find out what is in our namespace\n" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 3, 45 | "metadata": { 46 | "collapsed": false 47 | }, 48 | "outputs": [], 49 | "source": [ 50 | "# find out what's in numpy\n" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 4, 56 | "metadata": { 57 | "collapsed": false 58 | }, 59 | "outputs": [], 60 | "source": [ 61 | "# find out what version we have\n" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "The numpy documentation is crucial!\n", 69 | "\n", 70 | "http://docs.scipy.org/doc/numpy/reference/\n", 71 | "\n", 72 | "## NDArrays ##\n", 73 | "\n", 74 | "The core class is the numpy ndarray (n-dimensional array)." 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 29, 80 | "metadata": { 81 | "collapsed": false 82 | }, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/html": [ 87 | "" 88 | ], 89 | "text/plain": [ 90 | "" 91 | ] 92 | }, 93 | "execution_count": 29, 94 | "metadata": {}, 95 | "output_type": "execute_result" 96 | } 97 | ], 98 | "source": [ 99 | "from IPython.display import Image\n", 100 | "Image(url='http://docs.scipy.org/doc/numpy/_images/threefundamental.png')" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 5, 106 | "metadata": { 107 | "collapsed": false 108 | }, 109 | "outputs": [], 110 | "source": [ 111 | "# create an array from a list\n" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 6, 117 | "metadata": { 118 | "collapsed": false 119 | }, 120 | "outputs": [], 121 | "source": [ 122 | "# find out the datatype\n" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 7, 128 | "metadata": { 129 | "collapsed": false 130 | }, 131 | "outputs": [], 132 | "source": [ 133 | "# find out the shape\n" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 8, 139 | "metadata": { 140 | "collapsed": false 141 | }, 142 | "outputs": [], 143 | "source": [ 144 | "# what is the shape\n" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 9, 150 | "metadata": { 151 | "collapsed": true 152 | }, 153 | "outputs": [], 154 | "source": [ 155 | "# another array with a different datatype and shape\n" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 10, 161 | "metadata": { 162 | "collapsed": false 163 | }, 164 | "outputs": [], 165 | "source": [ 166 | "# check dtype and shape\n" 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": {}, 172 | "source": [ 173 | "__Important Concept__: The fastest varying dimension is the last dimension! The outer level of the hierarchy is the first dimension. (This is called \"c-style\" indexing)" 174 | ] 175 | }, 176 | { 177 | "cell_type": "markdown", 178 | "metadata": {}, 179 | "source": [ 180 | "## More array creation ##\n", 181 | "\n", 182 | "There are lots of ways to create arrays." 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 11, 188 | "metadata": { 189 | "collapsed": false 190 | }, 191 | "outputs": [], 192 | "source": [ 193 | "# create some uniform arrays\n" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 12, 199 | "metadata": { 200 | "collapsed": false 201 | }, 202 | "outputs": [], 203 | "source": [ 204 | "# create some ranges\n" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 13, 210 | "metadata": { 211 | "collapsed": false 212 | }, 213 | "outputs": [], 214 | "source": [ 215 | "# arange is left inclusive, right exclusive\n" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 14, 221 | "metadata": { 222 | "collapsed": false 223 | }, 224 | "outputs": [], 225 | "source": [ 226 | "# linearly spaced\n" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 15, 232 | "metadata": { 233 | "collapsed": false 234 | }, 235 | "outputs": [], 236 | "source": [ 237 | "# log spaced\n" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 16, 243 | "metadata": { 244 | "collapsed": false 245 | }, 246 | "outputs": [], 247 | "source": [ 248 | "# two dimensional grids\n" 249 | ] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "metadata": {}, 254 | "source": [ 255 | "## Indexing ##\n", 256 | "\n", 257 | "Basic indexing is similar to lists" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": 17, 263 | "metadata": { 264 | "collapsed": false 265 | }, 266 | "outputs": [], 267 | "source": [ 268 | "# get some individual elements of xx\n" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 18, 274 | "metadata": { 275 | "collapsed": false, 276 | "scrolled": true 277 | }, 278 | "outputs": [], 279 | "source": [ 280 | "# get some whole rows and columns\n" 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": 19, 286 | "metadata": { 287 | "collapsed": false 288 | }, 289 | "outputs": [], 290 | "source": [ 291 | "# get some ranges using slice notation\n" 292 | ] 293 | }, 294 | { 295 | "cell_type": "markdown", 296 | "metadata": {}, 297 | "source": [ 298 | "There are many advanced ways to index arrays. You can read about them in the manual. Here is one example." 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": 21, 304 | "metadata": { 305 | "collapsed": false 306 | }, 307 | "outputs": [], 308 | "source": [ 309 | "# use a boolean array as an index\n" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": 22, 315 | "metadata": { 316 | "collapsed": false 317 | }, 318 | "outputs": [], 319 | "source": [ 320 | "# the array got flattened\n" 321 | ] 322 | }, 323 | { 324 | "cell_type": "markdown", 325 | "metadata": {}, 326 | "source": [ 327 | "## Array Operations ##\n", 328 | "\n", 329 | "There are a huge number of operations available on arrays. All the familiar arithemtic operators are applied on an element-by-element basis.\n", 330 | "\n", 331 | "### Basic Math ##" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": 20, 337 | "metadata": { 338 | "collapsed": true 339 | }, 340 | "outputs": [], 341 | "source": [ 342 | "# calc something\n" 343 | ] 344 | }, 345 | { 346 | "cell_type": "markdown", 347 | "metadata": {}, 348 | "source": [ 349 | "At this point you might be getting curious what these arrays \"look\" like. So we need to introduce some visualization." 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": 21, 355 | "metadata": { 356 | "collapsed": true 357 | }, 358 | "outputs": [], 359 | "source": [ 360 | "# import matplotlib\n" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": 22, 366 | "metadata": { 367 | "collapsed": false 368 | }, 369 | "outputs": [], 370 | "source": [ 371 | "# pcolormesh our array\n" 372 | ] 373 | }, 374 | { 375 | "cell_type": "markdown", 376 | "metadata": {}, 377 | "source": [ 378 | "## Manipulating array dimensions ##" 379 | ] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": 23, 384 | "metadata": { 385 | "collapsed": false 386 | }, 387 | "outputs": [], 388 | "source": [ 389 | "# transpose\n" 390 | ] 391 | }, 392 | { 393 | "cell_type": "code", 394 | "execution_count": 24, 395 | "metadata": { 396 | "collapsed": false 397 | }, 398 | "outputs": [], 399 | "source": [ 400 | "# transpose\n" 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": 25, 406 | "metadata": { 407 | "collapsed": false 408 | }, 409 | "outputs": [], 410 | "source": [ 411 | "# reshape an array (wrong size)\n" 412 | ] 413 | }, 414 | { 415 | "cell_type": "code", 416 | "execution_count": 26, 417 | "metadata": { 418 | "collapsed": false 419 | }, 420 | "outputs": [], 421 | "source": [ 422 | "# reshape an array (right size) and mess it up\n" 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": 27, 428 | "metadata": { 429 | "collapsed": false 430 | }, 431 | "outputs": [], 432 | "source": [ 433 | "# tile an array\n" 434 | ] 435 | }, 436 | { 437 | "cell_type": "markdown", 438 | "metadata": {}, 439 | "source": [ 440 | "## Broadcasting ##\n", 441 | "\n", 442 | "Broadcasting is an efficient way to multiply arrays of different sizes\n" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": 30, 448 | "metadata": { 449 | "collapsed": false 450 | }, 451 | "outputs": [ 452 | { 453 | "data": { 454 | "text/html": [ 455 | "" 456 | ], 457 | "text/plain": [ 458 | "" 459 | ] 460 | }, 461 | "execution_count": 30, 462 | "metadata": {}, 463 | "output_type": "execute_result" 464 | } 465 | ], 466 | "source": [ 467 | "Image(url='http://scipy-lectures.github.io/_images/numpy_broadcasting.png',\n", 468 | " width=720)" 469 | ] 470 | }, 471 | { 472 | "cell_type": "code", 473 | "execution_count": 31, 474 | "metadata": { 475 | "collapsed": false 476 | }, 477 | "outputs": [], 478 | "source": [ 479 | "# multiply f by x\n" 480 | ] 481 | }, 482 | { 483 | "cell_type": "code", 484 | "execution_count": 32, 485 | "metadata": { 486 | "collapsed": false 487 | }, 488 | "outputs": [], 489 | "source": [ 490 | "# multiply f by y (shouldn't work)\n" 491 | ] 492 | }, 493 | { 494 | "cell_type": "code", 495 | "execution_count": 33, 496 | "metadata": { 497 | "collapsed": false 498 | }, 499 | "outputs": [], 500 | "source": [ 501 | "# use newaxis special syntax\n" 502 | ] 503 | }, 504 | { 505 | "cell_type": "code", 506 | "execution_count": 34, 507 | "metadata": { 508 | "collapsed": false 509 | }, 510 | "outputs": [], 511 | "source": [ 512 | "# look at result\n" 513 | ] 514 | }, 515 | { 516 | "cell_type": "markdown", 517 | "metadata": {}, 518 | "source": [ 519 | "## Reduction Operations ##" 520 | ] 521 | }, 522 | { 523 | "cell_type": "code", 524 | "execution_count": 35, 525 | "metadata": { 526 | "collapsed": false 527 | }, 528 | "outputs": [], 529 | "source": [ 530 | "# sum\n" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": 36, 536 | "metadata": { 537 | "collapsed": false 538 | }, 539 | "outputs": [], 540 | "source": [ 541 | "# mean\n" 542 | ] 543 | }, 544 | { 545 | "cell_type": "code", 546 | "execution_count": 37, 547 | "metadata": { 548 | "collapsed": false 549 | }, 550 | "outputs": [], 551 | "source": [ 552 | "# std\n" 553 | ] 554 | }, 555 | { 556 | "cell_type": "code", 557 | "execution_count": 38, 558 | "metadata": { 559 | "collapsed": false 560 | }, 561 | "outputs": [], 562 | "source": [ 563 | "# apply on just one axis\n" 564 | ] 565 | }, 566 | { 567 | "cell_type": "code", 568 | "execution_count": 39, 569 | "metadata": { 570 | "collapsed": false 571 | }, 572 | "outputs": [], 573 | "source": [ 574 | "# plot\n" 575 | ] 576 | }, 577 | { 578 | "cell_type": "markdown", 579 | "metadata": {}, 580 | "source": [ 581 | "## Fancy Plotting ##\n", 582 | "\n", 583 | "Enough lessons, let's have some fun." 584 | ] 585 | }, 586 | { 587 | "cell_type": "code", 588 | "execution_count": 39, 589 | "metadata": { 590 | "collapsed": false 591 | }, 592 | "outputs": [], 593 | "source": [ 594 | "# create axes\n" 595 | ] 596 | }, 597 | { 598 | "cell_type": "code", 599 | "execution_count": 40, 600 | "metadata": { 601 | "collapsed": false 602 | }, 603 | "outputs": [], 604 | "source": [ 605 | "# big plot\n" 606 | ] 607 | }, 608 | { 609 | "cell_type": "markdown", 610 | "metadata": {}, 611 | "source": [ 612 | "## Real Data ##\n", 613 | "\n", 614 | "ARGO float profile from North Atlantic" 615 | ] 616 | }, 617 | { 618 | "cell_type": "code", 619 | "execution_count": 174, 620 | "metadata": { 621 | "collapsed": false 622 | }, 623 | "outputs": [ 624 | { 625 | "name": "stdout", 626 | "output_type": "stream", 627 | "text": [ 628 | " % Total % Received % Xferd Average Speed Time Time Time Current\n", 629 | " Dload Upload Total Spent Left Speed\n", 630 | "100 140k 100 140k 0 0 1468k 0 --:--:-- --:--:-- --:--:-- 1480k\n" 631 | ] 632 | } 633 | ], 634 | "source": [ 635 | "# download with curl\n", 636 | "!curl -O http://www.ldeo.columbia.edu/~rpa/argo_float_4901412.npz" 637 | ] 638 | }, 639 | { 640 | "cell_type": "code", 641 | "execution_count": 41, 642 | "metadata": { 643 | "collapsed": false 644 | }, 645 | "outputs": [], 646 | "source": [ 647 | "# load numpy file and examine keys\n" 648 | ] 649 | }, 650 | { 651 | "cell_type": "code", 652 | "execution_count": 42, 653 | "metadata": { 654 | "collapsed": true 655 | }, 656 | "outputs": [], 657 | "source": [ 658 | "# access some data\n" 659 | ] 660 | }, 661 | { 662 | "cell_type": "code", 663 | "execution_count": 43, 664 | "metadata": { 665 | "collapsed": false 666 | }, 667 | "outputs": [], 668 | "source": [ 669 | "# there are \"nans\", missing data, which screw up our routines\n" 670 | ] 671 | }, 672 | { 673 | "cell_type": "markdown", 674 | "metadata": {}, 675 | "source": [ 676 | "## Masked Arrays ##\n", 677 | "\n", 678 | "This is how we deal with missing data in numpy" 679 | ] 680 | }, 681 | { 682 | "cell_type": "code", 683 | "execution_count": 44, 684 | "metadata": { 685 | "collapsed": false 686 | }, 687 | "outputs": [], 688 | "source": [ 689 | "# create masked array\n" 690 | ] 691 | }, 692 | { 693 | "cell_type": "code", 694 | "execution_count": 45, 695 | "metadata": { 696 | "collapsed": false 697 | }, 698 | "outputs": [], 699 | "source": [ 700 | "# max and min\n" 701 | ] 702 | }, 703 | { 704 | "cell_type": "code", 705 | "execution_count": 46, 706 | "metadata": { 707 | "collapsed": false 708 | }, 709 | "outputs": [], 710 | "source": [ 711 | "# load other data\n" 712 | ] 713 | }, 714 | { 715 | "cell_type": "code", 716 | "execution_count": 47, 717 | "metadata": { 718 | "collapsed": false 719 | }, 720 | "outputs": [], 721 | "source": [ 722 | "# scatter plot\n" 723 | ] 724 | }, 725 | { 726 | "cell_type": "markdown", 727 | "metadata": {}, 728 | "source": [ 729 | "# Individual Exercise #\n", 730 | "\n", 731 | "* Calculate and plot the depth mean profile of T, S and P with error bars to indicate the standard deviation.\n", 732 | "* Calculate the timeseries of the depth-averaged profiles of T, S, and P" 733 | ] 734 | }, 735 | { 736 | "cell_type": "code", 737 | "execution_count": null, 738 | "metadata": { 739 | "collapsed": true 740 | }, 741 | "outputs": [], 742 | "source": [] 743 | } 744 | ], 745 | "metadata": { 746 | "kernelspec": { 747 | "display_name": "Python 3", 748 | "language": "python", 749 | "name": "python3" 750 | }, 751 | "language_info": { 752 | "codemirror_mode": { 753 | "name": "ipython", 754 | "version": 3 755 | }, 756 | "file_extension": ".py", 757 | "mimetype": "text/x-python", 758 | "name": "python", 759 | "nbconvert_exporter": "python", 760 | "pygments_lexer": "ipython3", 761 | "version": "3.4.3" 762 | } 763 | }, 764 | "nbformat": 4, 765 | "nbformat_minor": 0 766 | } 767 | -------------------------------------------------------------------------------- /one_day_workshop/01_core_python_blank.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Core Python Language #\n", 8 | "\n", 9 | "Mostly copied from the [official python tutorial](https://docs.python.org/3/tutorial/)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "## Invoking Python ##\n", 17 | "\n", 18 | "There are three main ways to use python.\n", 19 | "\n", 20 | "1. By running a python file, e.g. `python myscript.py`\n", 21 | "1. Through an interactive console (python interpreter or ipython shell)\n", 22 | "1. In an interactive iPython notebook\n", 23 | "\n", 24 | "We will be using the iPython notebook." 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "## Python Versions ##\n", 32 | "\n", 33 | "There are two versions of the python language out there: python 2 and python 3. Python 2 is more common in the wild but is depracated. The community is moving to python 3. As new python learners, you should learn python 3. But it is important to be aware that python 2 exists. It is possible that a package you want to use is only supported in python 2. In general, it is pretty easy to switch between then.\n", 34 | "\n", 35 | "Some of the main changes in python 3 are:\n", 36 | "\n", 37 | "* ``print`` is a function\n", 38 | "* Integer division returns a float\n", 39 | "* Iterators behave differently\n", 40 | "* Unicode is used for encoding code\n" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "## Basic Variables: Numbers and String ##" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 1, 53 | "metadata": { 54 | "collapsed": true 55 | }, 56 | "outputs": [], 57 | "source": [ 58 | "# this is a comment\n" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "The following identifiers are used as reserved words, or keywords of the language, and cannot be used as ordinary identifiers. They must be spelled exactly as written here:\n", 66 | "\n", 67 | " False class finally is return\n", 68 | " None continue for lambda try\n", 69 | " True def from nonlocal while\n", 70 | " and del global not with\n", 71 | " as elif if or yield\n", 72 | " assert else import pass\n", 73 | " break except in raise\n", 74 | " \n", 75 | "Additionally, the following a built in functions which are always available in your namespace once you open a python interpreter\n", 76 | "\n", 77 | " abs() dict() help() min() setattr() all() dir() hex() next() slice() any()\n", 78 | " divmod() id() object() sorted() ascii() enumerate() input() oct() staticmethod()\n", 79 | " bin() eval() int() open() str() bool() exec() isinstance() ord() sum() bytearray()\n", 80 | " filter() issubclass() pow() super() bytes() float() iter() print() tuple()\n", 81 | " callable() format() len() property() type() chr() frozenset() list() range()\n", 82 | " vars() classmethod() getattr() locals() repr() zip() compile() globals() map()\n", 83 | " reversed() __import__() complex() hasattr() max() round() delattr() hash()\n", 84 | " memoryview() set()\n", 85 | "\n" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 2, 91 | "metadata": { 92 | "collapsed": false 93 | }, 94 | "outputs": [], 95 | "source": [ 96 | "# how to we see our variables?\n" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | "All variables are objects. Every object has a type (class). To find out what type your variables are" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 3, 109 | "metadata": { 110 | "collapsed": false 111 | }, 112 | "outputs": [], 113 | "source": [ 114 | "# examine types\n" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 2, 120 | "metadata": { 121 | "collapsed": false 122 | }, 123 | "outputs": [], 124 | "source": [ 125 | "# as a shortcut, iPython notebooks will automatically print whatever is on the last line\n" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 4, 131 | "metadata": { 132 | "collapsed": false 133 | }, 134 | "outputs": [], 135 | "source": [ 136 | "# we can check for the type of an object\n" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "Different objects attributes and methods, which can be accessed via the syntax ``variable.method``\n", 144 | "\n", 145 | "IPython will autocomplete if you press ```` to show you the methods available." 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 5, 151 | "metadata": { 152 | "collapsed": false 153 | }, 154 | "outputs": [], 155 | "source": [ 156 | "# this returns the method itself\n" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 6, 162 | "metadata": { 163 | "collapsed": false 164 | }, 165 | "outputs": [], 166 | "source": [ 167 | "# call the method\n" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 7, 173 | "metadata": { 174 | "collapsed": false 175 | }, 176 | "outputs": [], 177 | "source": [ 178 | "# binary operations act differently on different types of objects\n" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "## Math ##\n", 186 | "\n", 187 | "Basic arithmetic and boolean logic is part of the core python library." 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 8, 193 | "metadata": { 194 | "collapsed": false 195 | }, 196 | "outputs": [], 197 | "source": [ 198 | "# addition / subtraction\n" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 9, 204 | "metadata": { 205 | "collapsed": false 206 | }, 207 | "outputs": [], 208 | "source": [ 209 | "# multiplication\n" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 10, 215 | "metadata": { 216 | "collapsed": false 217 | }, 218 | "outputs": [], 219 | "source": [ 220 | "# division\n" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 11, 226 | "metadata": { 227 | "collapsed": false 228 | }, 229 | "outputs": [], 230 | "source": [ 231 | "# that was automatically converted to a float\n" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": 12, 237 | "metadata": { 238 | "collapsed": false 239 | }, 240 | "outputs": [], 241 | "source": [ 242 | "# exponentiation\n" 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "execution_count": 13, 248 | "metadata": { 249 | "collapsed": false 250 | }, 251 | "outputs": [], 252 | "source": [ 253 | "# rounding\n" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": 14, 259 | "metadata": { 260 | "collapsed": false 261 | }, 262 | "outputs": [], 263 | "source": [ 264 | "# built in complex number support\n" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": 15, 270 | "metadata": { 271 | "collapsed": false 272 | }, 273 | "outputs": [], 274 | "source": [ 275 | "# logic\n" 276 | ] 277 | }, 278 | { 279 | "cell_type": "markdown", 280 | "metadata": {}, 281 | "source": [ 282 | "## Conditionals ##\n", 283 | "\n", 284 | "The first step to programming. Plus an intro to python syntax." 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": 16, 290 | "metadata": { 291 | "collapsed": false 292 | }, 293 | "outputs": [], 294 | "source": [ 295 | "# conditional if, elif, else\n" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": 17, 301 | "metadata": { 302 | "collapsed": false 303 | }, 304 | "outputs": [], 305 | "source": [ 306 | "# indentation is MANDATORY\n", 307 | "# blocks are closed by indentation level\n" 308 | ] 309 | }, 310 | { 311 | "cell_type": "markdown", 312 | "metadata": {}, 313 | "source": [ 314 | "## More Flow Control ##" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 18, 320 | "metadata": { 321 | "collapsed": false 322 | }, 323 | "outputs": [], 324 | "source": [ 325 | "# make a loop with while\n" 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": 19, 331 | "metadata": { 332 | "collapsed": false 333 | }, 334 | "outputs": [], 335 | "source": [ 336 | "# use range\n" 337 | ] 338 | }, 339 | { 340 | "cell_type": "markdown", 341 | "metadata": {}, 342 | "source": [ 343 | "__Important point__: in python, we always count from 0!" 344 | ] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": 20, 349 | "metadata": { 350 | "collapsed": false 351 | }, 352 | "outputs": [], 353 | "source": [ 354 | "# what is range?\n" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": 22, 360 | "metadata": { 361 | "collapsed": false 362 | }, 363 | "outputs": [], 364 | "source": [ 365 | "# infinite loop\n" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 23, 371 | "metadata": { 372 | "collapsed": false 373 | }, 374 | "outputs": [], 375 | "source": [ 376 | "# iterate over a list we make up\n" 377 | ] 378 | }, 379 | { 380 | "cell_type": "markdown", 381 | "metadata": {}, 382 | "source": [ 383 | "What is the thing in brackets? __A list!__ Lists are one of the core python data structures.\n", 384 | "\n", 385 | "## Lists ##" 386 | ] 387 | }, 388 | { 389 | "cell_type": "code", 390 | "execution_count": 24, 391 | "metadata": { 392 | "collapsed": false 393 | }, 394 | "outputs": [], 395 | "source": [ 396 | "# create list\n" 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "execution_count": 25, 402 | "metadata": { 403 | "collapsed": false 404 | }, 405 | "outputs": [], 406 | "source": [ 407 | "# list have lots of methods\n" 408 | ] 409 | }, 410 | { 411 | "cell_type": "code", 412 | "execution_count": 26, 413 | "metadata": { 414 | "collapsed": false 415 | }, 416 | "outputs": [], 417 | "source": [ 418 | "# we can convert a range to a list\n" 419 | ] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "execution_count": 27, 424 | "metadata": { 425 | "collapsed": false 426 | }, 427 | "outputs": [], 428 | "source": [ 429 | "# pop from list\n" 430 | ] 431 | }, 432 | { 433 | "cell_type": "markdown", 434 | "metadata": {}, 435 | "source": [ 436 | "There are many different ways to interact with lists. Exploring them is part of the fun of python.\n", 437 | "\n", 438 | "__list.append(x)__ Add an item to the end of the list. Equivalent to a[len(a):] = [x].\n", 439 | "\n", 440 | "__list.extend(L)__ \n", 441 | "Extend the list by appending all the items in the given list. Equivalent to a[len(a):] = L.\n", 442 | "\n", 443 | "__list.insert(i, x)__ Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).\n", 444 | "\n", 445 | "__list.remove(x)__ Remove the first item from the list whose value is x. It is an error if there is no such item.\n", 446 | "\n", 447 | "__list.pop([i])__ Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)\n", 448 | "\n", 449 | "__list.clear()__ Remove all items from the list. Equivalent to del a[:].\n", 450 | "\n", 451 | "__list.index(x)__ Return the index in the list of the first item whose value is x. It is an error if there is no such item.\n", 452 | "\n", 453 | "__list.count(x)__ Return the number of times x appears in the list.\n", 454 | "\n", 455 | "__list.sort()__ Sort the items of the list in place.\n", 456 | "\n", 457 | "__list.reverse()__ Reverse the elements of the list in place.\n", 458 | "\n", 459 | "__list.copy()__ Return a shallow copy of the list. Equivalent to a[:].\n", 460 | "\n", 461 | "\n", 462 | "Don't assume you know how list operations work!" 463 | ] 464 | }, 465 | { 466 | "cell_type": "code", 467 | "execution_count": 62, 468 | "metadata": { 469 | "collapsed": false 470 | }, 471 | "outputs": [ 472 | { 473 | "data": { 474 | "text/plain": [ 475 | "[0, 1, 2, 3, 4, 10, 11, 12, 13, 14]" 476 | ] 477 | }, 478 | "execution_count": 62, 479 | "metadata": {}, 480 | "output_type": "execute_result" 481 | } 482 | ], 483 | "source": [ 484 | "# \"add\" two lists\n", 485 | "x = list(range(5))\n", 486 | "y = list(range(10,15))\n", 487 | "z = x + y\n", 488 | "z" 489 | ] 490 | }, 491 | { 492 | "cell_type": "code", 493 | "execution_count": 63, 494 | "metadata": { 495 | "collapsed": false 496 | }, 497 | "outputs": [ 498 | { 499 | "name": "stdout", 500 | "output_type": "stream", 501 | "text": [ 502 | "first 0\n", 503 | "last 14\n", 504 | "first 3 [0, 1, 2]\n", 505 | "last 3 [12, 13, 14]\n", 506 | "middle, skipping every other item [10, 12, 14]\n" 507 | ] 508 | } 509 | ], 510 | "source": [ 511 | "# access items from a list\n", 512 | "print('first', z[0])\n", 513 | "print('last', z[-1])\n", 514 | "print('first 3', z[:3])\n", 515 | "print('last 3', z[-3:])\n", 516 | "print('middle, skipping every other item', z[5:10:2])" 517 | ] 518 | }, 519 | { 520 | "cell_type": "markdown", 521 | "metadata": {}, 522 | "source": [ 523 | "__MEMORIZE THIS SYNTAX!__ It is central to so much of python and often proves confusing for users coming from other languages.\n", 524 | "\n", 525 | "In terms of set notation, python indexing is _left inclusive_, _right exclusive_. If you remember this, you will never go wrong." 526 | ] 527 | }, 528 | { 529 | "cell_type": "code", 530 | "execution_count": 28, 531 | "metadata": { 532 | "collapsed": false 533 | }, 534 | "outputs": [], 535 | "source": [ 536 | "# that means we get an error from the following\n" 537 | ] 538 | }, 539 | { 540 | "cell_type": "code", 541 | "execution_count": 29, 542 | "metadata": { 543 | "collapsed": false 544 | }, 545 | "outputs": [], 546 | "source": [ 547 | "# this index notation also applies to strings\n" 548 | ] 549 | }, 550 | { 551 | "cell_type": "code", 552 | "execution_count": 30, 553 | "metadata": { 554 | "collapsed": false 555 | }, 556 | "outputs": [], 557 | "source": [ 558 | "# you can also test for the presence of items in a list\n" 559 | ] 560 | }, 561 | { 562 | "cell_type": "markdown", 563 | "metadata": {}, 564 | "source": [ 565 | "Lists are not meant for math! They don't have a datatype." 566 | ] 567 | }, 568 | { 569 | "cell_type": "code", 570 | "execution_count": 31, 571 | "metadata": { 572 | "collapsed": false 573 | }, 574 | "outputs": [], 575 | "source": [ 576 | "# set an element to something other than a number\n" 577 | ] 578 | }, 579 | { 580 | "cell_type": "markdown", 581 | "metadata": {}, 582 | "source": [ 583 | "Python is full of tricks for iterating and working with lists" 584 | ] 585 | }, 586 | { 587 | "cell_type": "code", 588 | "execution_count": 32, 589 | "metadata": { 590 | "collapsed": false 591 | }, 592 | "outputs": [], 593 | "source": [ 594 | "# a cool python trick: list comprehension\n" 595 | ] 596 | }, 597 | { 598 | "cell_type": "code", 599 | "execution_count": 33, 600 | "metadata": { 601 | "collapsed": false 602 | }, 603 | "outputs": [], 604 | "source": [ 605 | "# iterate over two lists together uzing zip\n" 606 | ] 607 | }, 608 | { 609 | "cell_type": "markdown", 610 | "metadata": {}, 611 | "source": [ 612 | "## Other Data Structures ##\n", 613 | "\n", 614 | "We are almost there. We have the building blocks we need to do basic programming. But python has some other data structures we need to learn about.\n", 615 | "\n", 616 | "## Tuples ##\n", 617 | "\n", 618 | "Tuples are similar to lists, but they are _immutable_—they can't be extended or modified. What is the point of this? Generally speaking: to pack together inhomogeneous data. Tuples can then be unpacked and distributed by other parts of your code.\n", 619 | "\n", 620 | "Tuples may seem confusing at first, but with time you will come to appreciate them." 621 | ] 622 | }, 623 | { 624 | "cell_type": "code", 625 | "execution_count": 32, 626 | "metadata": { 627 | "collapsed": false 628 | }, 629 | "outputs": [], 630 | "source": [ 631 | "# tuples are created with parentheses, or just commas\n" 632 | ] 633 | }, 634 | { 635 | "cell_type": "code", 636 | "execution_count": 33, 637 | "metadata": { 638 | "collapsed": false 639 | }, 640 | "outputs": [], 641 | "source": [ 642 | "# can be indexed like arrays\n" 643 | ] 644 | }, 645 | { 646 | "cell_type": "code", 647 | "execution_count": 34, 648 | "metadata": { 649 | "collapsed": true 650 | }, 651 | "outputs": [], 652 | "source": [ 653 | "# and they can be unpacked\n" 654 | ] 655 | }, 656 | { 657 | "cell_type": "markdown", 658 | "metadata": {}, 659 | "source": [ 660 | "## Dictionaries ##\n", 661 | "\n", 662 | "This is an extremely useful data structure. It maps __keys__ to __values__.\n", 663 | "\n", 664 | "Dictionaries are unordered!" 665 | ] 666 | }, 667 | { 668 | "cell_type": "code", 669 | "execution_count": 35, 670 | "metadata": { 671 | "collapsed": false 672 | }, 673 | "outputs": [], 674 | "source": [ 675 | "# different ways to create dictionaries\n" 676 | ] 677 | }, 678 | { 679 | "cell_type": "code", 680 | "execution_count": 36, 681 | "metadata": { 682 | "collapsed": false 683 | }, 684 | "outputs": [], 685 | "source": [ 686 | "# access a value\n" 687 | ] 688 | }, 689 | { 690 | "cell_type": "markdown", 691 | "metadata": {}, 692 | "source": [ 693 | "Square brackets ``[...]`` are python for \"get item\" in many different contexts." 694 | ] 695 | }, 696 | { 697 | "cell_type": "code", 698 | "execution_count": 37, 699 | "metadata": { 700 | "collapsed": false 701 | }, 702 | "outputs": [], 703 | "source": [ 704 | "# test for the presence of a key\n" 705 | ] 706 | }, 707 | { 708 | "cell_type": "code", 709 | "execution_count": 38, 710 | "metadata": { 711 | "collapsed": false 712 | }, 713 | "outputs": [], 714 | "source": [ 715 | "# try to access a non-existant key\n" 716 | ] 717 | }, 718 | { 719 | "cell_type": "code", 720 | "execution_count": 39, 721 | "metadata": { 722 | "collapsed": false 723 | }, 724 | "outputs": [], 725 | "source": [ 726 | "# add a new key\n" 727 | ] 728 | }, 729 | { 730 | "cell_type": "code", 731 | "execution_count": 40, 732 | "metadata": { 733 | "collapsed": false 734 | }, 735 | "outputs": [], 736 | "source": [ 737 | "# keys don't have to be strings\n" 738 | ] 739 | }, 740 | { 741 | "cell_type": "code", 742 | "execution_count": 41, 743 | "metadata": { 744 | "collapsed": false 745 | }, 746 | "outputs": [], 747 | "source": [ 748 | "# iterate over keys\n" 749 | ] 750 | }, 751 | { 752 | "cell_type": "code", 753 | "execution_count": 44, 754 | "metadata": { 755 | "collapsed": false 756 | }, 757 | "outputs": [], 758 | "source": [ 759 | "# better way\n" 760 | ] 761 | }, 762 | { 763 | "cell_type": "markdown", 764 | "metadata": {}, 765 | "source": [ 766 | "## Functions ##\n", 767 | "\n", 768 | "Functions are a central part of advanced python programming. You should try to write and use your own functions as often as possible." 769 | ] 770 | }, 771 | { 772 | "cell_type": "code", 773 | "execution_count": 34, 774 | "metadata": { 775 | "collapsed": false 776 | }, 777 | "outputs": [], 778 | "source": [ 779 | "# define a function\n" 780 | ] 781 | }, 782 | { 783 | "cell_type": "code", 784 | "execution_count": 45, 785 | "metadata": { 786 | "collapsed": false 787 | }, 788 | "outputs": [], 789 | "source": [ 790 | "# functions are also objects\n" 791 | ] 792 | }, 793 | { 794 | "cell_type": "code", 795 | "execution_count": 46, 796 | "metadata": { 797 | "collapsed": false 798 | }, 799 | "outputs": [], 800 | "source": [ 801 | "# this doesnt call\n" 802 | ] 803 | }, 804 | { 805 | "cell_type": "code", 806 | "execution_count": 47, 807 | "metadata": { 808 | "collapsed": false 809 | }, 810 | "outputs": [], 811 | "source": [ 812 | "# this does\n" 813 | ] 814 | }, 815 | { 816 | "cell_type": "code", 817 | "execution_count": 48, 818 | "metadata": { 819 | "collapsed": false 820 | }, 821 | "outputs": [], 822 | "source": [ 823 | "# assign the result to something\n" 824 | ] 825 | }, 826 | { 827 | "cell_type": "code", 828 | "execution_count": 49, 829 | "metadata": { 830 | "collapsed": true 831 | }, 832 | "outputs": [], 833 | "source": [ 834 | "# take some arguments\n" 835 | ] 836 | }, 837 | { 838 | "cell_type": "code", 839 | "execution_count": 50, 840 | "metadata": { 841 | "collapsed": false 842 | }, 843 | "outputs": [], 844 | "source": [ 845 | "# intended usage\n" 846 | ] 847 | }, 848 | { 849 | "cell_type": "code", 850 | "execution_count": 51, 851 | "metadata": { 852 | "collapsed": false 853 | }, 854 | "outputs": [], 855 | "source": [ 856 | "# unintented usage\n" 857 | ] 858 | }, 859 | { 860 | "cell_type": "code", 861 | "execution_count": 52, 862 | "metadata": { 863 | "collapsed": true 864 | }, 865 | "outputs": [], 866 | "source": [ 867 | "# redefine the function\n" 868 | ] 869 | }, 870 | { 871 | "cell_type": "code", 872 | "execution_count": 55, 873 | "metadata": { 874 | "collapsed": false 875 | }, 876 | "outputs": [], 877 | "source": [ 878 | "# test it\n" 879 | ] 880 | }, 881 | { 882 | "cell_type": "code", 883 | "execution_count": 53, 884 | "metadata": { 885 | "collapsed": false 886 | }, 887 | "outputs": [], 888 | "source": [ 889 | "# take an optional keyword argument\n" 890 | ] 891 | }, 892 | { 893 | "cell_type": "code", 894 | "execution_count": 54, 895 | "metadata": { 896 | "collapsed": false 897 | }, 898 | "outputs": [], 899 | "source": [ 900 | "# test it\n" 901 | ] 902 | }, 903 | { 904 | "cell_type": "code", 905 | "execution_count": 56, 906 | "metadata": { 907 | "collapsed": true 908 | }, 909 | "outputs": [], 910 | "source": [ 911 | "# flexible number of arguments\n" 912 | ] 913 | }, 914 | { 915 | "cell_type": "code", 916 | "execution_count": 57, 917 | "metadata": { 918 | "collapsed": false 919 | }, 920 | "outputs": [], 921 | "source": [ 922 | "# test it\n" 923 | ] 924 | }, 925 | { 926 | "cell_type": "markdown", 927 | "metadata": {}, 928 | "source": [ 929 | "We could spend the rest of the day talking about functions, but we have to move on." 930 | ] 931 | }, 932 | { 933 | "cell_type": "markdown", 934 | "metadata": {}, 935 | "source": [ 936 | "# Individual Exercises #" 937 | ] 938 | }, 939 | { 940 | "cell_type": "markdown", 941 | "metadata": {}, 942 | "source": [ 943 | "## Fibonacci Sequence ##\n", 944 | "\n", 945 | "The Fibonacci sequence is the 1,1,2,3,5,8..., the sum of each number with the preceding one. Write a function to compute the Fibonacci sequence of length n. (Hint, use some list methods.)" 946 | ] 947 | }, 948 | { 949 | "cell_type": "code", 950 | "execution_count": null, 951 | "metadata": { 952 | "collapsed": true 953 | }, 954 | "outputs": [], 955 | "source": [] 956 | }, 957 | { 958 | "cell_type": "code", 959 | "execution_count": 89, 960 | "metadata": { 961 | "collapsed": false 962 | }, 963 | "outputs": [ 964 | { 965 | "data": { 966 | "text/plain": [ 967 | "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]" 968 | ] 969 | }, 970 | "execution_count": 89, 971 | "metadata": {}, 972 | "output_type": "execute_result" 973 | } 974 | ], 975 | "source": [ 976 | "# test it\n", 977 | "fib(10)" 978 | ] 979 | }, 980 | { 981 | "cell_type": "markdown", 982 | "metadata": {}, 983 | "source": [ 984 | "## Add and Multiply Lists Item by Item ##\n", 985 | "\n", 986 | "Write functions to add and multiply each item in a list." 987 | ] 988 | }, 989 | { 990 | "cell_type": "code", 991 | "execution_count": null, 992 | "metadata": { 993 | "collapsed": true 994 | }, 995 | "outputs": [], 996 | "source": [] 997 | }, 998 | { 999 | "cell_type": "code", 1000 | "execution_count": 36, 1001 | "metadata": { 1002 | "collapsed": false 1003 | }, 1004 | "outputs": [], 1005 | "source": [ 1006 | "# test\n" 1007 | ] 1008 | }, 1009 | { 1010 | "cell_type": "code", 1011 | "execution_count": 37, 1012 | "metadata": { 1013 | "collapsed": false 1014 | }, 1015 | "outputs": [], 1016 | "source": [ 1017 | "# time it\n" 1018 | ] 1019 | }, 1020 | { 1021 | "cell_type": "code", 1022 | "execution_count": 38, 1023 | "metadata": { 1024 | "collapsed": false 1025 | }, 1026 | "outputs": [], 1027 | "source": [ 1028 | "# time it with numpy\n" 1029 | ] 1030 | }, 1031 | { 1032 | "cell_type": "markdown", 1033 | "metadata": {}, 1034 | "source": [ 1035 | "On to numpy" 1036 | ] 1037 | } 1038 | ], 1039 | "metadata": { 1040 | "kernelspec": { 1041 | "display_name": "Python 3", 1042 | "language": "python", 1043 | "name": "python3" 1044 | }, 1045 | "language_info": { 1046 | "codemirror_mode": { 1047 | "name": "ipython", 1048 | "version": 3 1049 | }, 1050 | "file_extension": ".py", 1051 | "mimetype": "text/x-python", 1052 | "name": "python", 1053 | "nbconvert_exporter": "python", 1054 | "pygments_lexer": "ipython3", 1055 | "version": "3.4.3" 1056 | } 1057 | }, 1058 | "nbformat": 4, 1059 | "nbformat_minor": 0 1060 | } 1061 | -------------------------------------------------------------------------------- /old_notebooks/Lesson1_Python_Basics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "", 4 | "signature": "sha256:0af6d7988989427b2995417bd4bd97afff69458a09fbfe927418f11e9d92d7a3" 5 | }, 6 | "nbformat": 3, 7 | "nbformat_minor": 0, 8 | "worksheets": [ 9 | { 10 | "cells": [ 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "# My First IPython Notebook #\n", 16 | "\n", 17 | "This notebook goes over the very basic elements of the Python language such as variables, variable types, lists, loops, and functions." 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "## Variables and Strings ##\n", 25 | "\n", 26 | "Assign a variable \"a\". (Hit shift-return to execute the cell.)" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "collapsed": false, 32 | "input": [ 33 | "a = 'hello'" 34 | ], 35 | "language": "python", 36 | "metadata": {}, 37 | "outputs": [], 38 | "prompt_number": 20 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "To produce a list of variables that are present in the kernel you can call \"whos\"" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "collapsed": false, 50 | "input": [ 51 | "whos" 52 | ], 53 | "language": "python", 54 | "metadata": {}, 55 | "outputs": [ 56 | { 57 | "output_type": "stream", 58 | "stream": "stdout", 59 | "text": [ 60 | "Variable Type Data/Info\n", 61 | "-----------------------------\n", 62 | "a str hello\n", 63 | "c int 5\n", 64 | "d float 2.0\n" 65 | ] 66 | } 67 | ], 68 | "prompt_number": 21 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "To find out the type of a certain variable use the function type()" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "collapsed": false, 80 | "input": [ 81 | "type(a)" 82 | ], 83 | "language": "python", 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "metadata": {}, 88 | "output_type": "pyout", 89 | "prompt_number": 22, 90 | "text": [ 91 | "str" 92 | ] 93 | } 94 | ], 95 | "prompt_number": 22 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "In this case, we see that a is a str (string). Everything in python is an object, and every type of object has special methods that can be called. To access these methods we use a dot (period) after the variable name.\n", 102 | "\n", 103 | "If you type \"a.\" (a and dot) and then hit tab, a dropdown menu lists all the methods applicable to the variable. (This is one of the perks of the IPython notebook.)\n" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "collapsed": false, 109 | "input": [ 110 | "a.capitalize()" 111 | ], 112 | "language": "python", 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "metadata": {}, 117 | "output_type": "pyout", 118 | "prompt_number": 23, 119 | "text": [ 120 | "'Hello'" 121 | ] 122 | } 123 | ], 124 | "prompt_number": 23 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "## Inline Documentation ##\n", 131 | "\n", 132 | "To access the help about a function you can add a question mark at the end of the function, with no parenthesis, and hit shift-return." 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "collapsed": false, 138 | "input": [ 139 | "a.capitalize?" 140 | ], 141 | "language": "python", 142 | "metadata": {}, 143 | "outputs": [], 144 | "prompt_number": 24 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": {}, 149 | "source": [ 150 | "Or you can write the full function (i.e. a.capitalize() ) and then place your cursor within the parenthesis and hit \"shift+tab\"." 151 | ] 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "metadata": {}, 156 | "source": [ 157 | "## Conditionals ##\n", 158 | "\n", 159 | "Here we write our first if-statement in python." 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "collapsed": false, 165 | "input": [ 166 | "b='goodbye'\n", 167 | "print b.upper()\n", 168 | "if a==b:\n", 169 | " print \"a and b are equal\"\n", 170 | "else: \n", 171 | " print \"a and b are NOT equal\"" 172 | ], 173 | "language": "python", 174 | "metadata": {}, 175 | "outputs": [ 176 | { 177 | "output_type": "stream", 178 | "stream": "stdout", 179 | "text": [ 180 | "GOODBYE\n", 181 | "a and b are NOT equal\n" 182 | ] 183 | } 184 | ], 185 | "prompt_number": 25 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": {}, 190 | "source": [ 191 | "NOTE:\n", 192 | "\n", 193 | "1) Python cares about indenting; the indenting tells python which loop/level a statement belongs to.\n", 194 | "\n", 195 | "2) the indentation is actually 4 spaces, but in a python editor or IPython notebook, the tab key will work fine. If you use another editor, you need to configure it to use \"soft tabs\" (i.e. spaces instead of the tab character).\n", 196 | "\n", 197 | "SIDE NOTE: If you want to empty your workspace of all variables, go to Kernel and restart the kernel. You don't loose the code You just clear the workspace. After that, you have to execute all the cells again (shift+enter) to re-run any part of the code.\n", 198 | "\n", 199 | "## Printing Output ##\n", 200 | "\n", 201 | "IPython will automatically print the last line of each cell. But it will _not_ automatically print the output of a function if it is assigned to a variable." 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "collapsed": false, 207 | "input": [ 208 | "c=len(a) #not printing to screen" 209 | ], 210 | "language": "python", 211 | "metadata": {}, 212 | "outputs": [], 213 | "prompt_number": 26 214 | }, 215 | { 216 | "cell_type": "code", 217 | "collapsed": false, 218 | "input": [ 219 | "len(a) #printing to screen " 220 | ], 221 | "language": "python", 222 | "metadata": {}, 223 | "outputs": [ 224 | { 225 | "metadata": {}, 226 | "output_type": "pyout", 227 | "prompt_number": 27, 228 | "text": [ 229 | "5" 230 | ] 231 | } 232 | ], 233 | "prompt_number": 27 234 | }, 235 | { 236 | "cell_type": "markdown", 237 | "metadata": {}, 238 | "source": [ 239 | "It will print only the last line of a cell" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "collapsed": false, 245 | "input": [ 246 | "c=len(a)\n", 247 | "len(a) #it is not going to be printed because it is not the last line\n", 248 | "c" 249 | ], 250 | "language": "python", 251 | "metadata": {}, 252 | "outputs": [ 253 | { 254 | "metadata": {}, 255 | "output_type": "pyout", 256 | "prompt_number": 28, 257 | "text": [ 258 | "5" 259 | ] 260 | } 261 | ], 262 | "prompt_number": 28 263 | }, 264 | { 265 | "cell_type": "markdown", 266 | "metadata": {}, 267 | "source": [ 268 | "To force screen print at any point of the cell use print(). The print method doesn't necessarily need the parenthesis, but it is neater to use them." 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "collapsed": false, 274 | "input": [ 275 | "c=len(a)\n", 276 | "print c \n", 277 | "print(c)\n", 278 | "type(c)" 279 | ], 280 | "language": "python", 281 | "metadata": {}, 282 | "outputs": [ 283 | { 284 | "output_type": "stream", 285 | "stream": "stdout", 286 | "text": [ 287 | "5\n", 288 | "5\n" 289 | ] 290 | }, 291 | { 292 | "metadata": {}, 293 | "output_type": "pyout", 294 | "prompt_number": 29, 295 | "text": [ 296 | "int" 297 | ] 298 | } 299 | ], 300 | "prompt_number": 29 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "metadata": {}, 305 | "source": [ 306 | "To suppress printing, you can use a semicolon." 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "collapsed": false, 312 | "input": [ 313 | "c;" 314 | ], 315 | "language": "python", 316 | "metadata": {}, 317 | "outputs": [], 318 | "prompt_number": 30 319 | }, 320 | { 321 | "cell_type": "markdown", 322 | "metadata": {}, 323 | "source": [ 324 | "# Numeric Data Types #\n", 325 | "\n", 326 | "If you use a number without and decimal points, it will be assumed to be an integer. Operations involving integers will only produce other integers, even division. This can be a big point of confusion in python." 327 | ] 328 | }, 329 | { 330 | "cell_type": "code", 331 | "collapsed": false, 332 | "input": [ 333 | "print(c/2)" 334 | ], 335 | "language": "python", 336 | "metadata": {}, 337 | "outputs": [ 338 | { 339 | "output_type": "stream", 340 | "stream": "stdout", 341 | "text": [ 342 | "2\n" 343 | ] 344 | } 345 | ], 346 | "prompt_number": 31 347 | }, 348 | { 349 | "cell_type": "markdown", 350 | "metadata": {}, 351 | "source": [ 352 | "\"c/2\" (5/2) has been rounded to the lower integer. \n", 353 | "If you need the result to be floating number, one of the two numbers has to be floating type. \n", 354 | "To do this i just add a \".\" after the number and it makes it into a floating number." 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "collapsed": false, 360 | "input": [ 361 | "2. #2. corresponds to a floating number" 362 | ], 363 | "language": "python", 364 | "metadata": {}, 365 | "outputs": [ 366 | { 367 | "metadata": {}, 368 | "output_type": "pyout", 369 | "prompt_number": 32, 370 | "text": [ 371 | "2.0" 372 | ] 373 | } 374 | ], 375 | "prompt_number": 32 376 | }, 377 | { 378 | "cell_type": "code", 379 | "collapsed": false, 380 | "input": [ 381 | "float(2) #float will change the data type to floating type." 382 | ], 383 | "language": "python", 384 | "metadata": {}, 385 | "outputs": [ 386 | { 387 | "metadata": {}, 388 | "output_type": "pyout", 389 | "prompt_number": 33, 390 | "text": [ 391 | "2.0" 392 | ] 393 | } 394 | ], 395 | "prompt_number": 33 396 | }, 397 | { 398 | "cell_type": "code", 399 | "collapsed": false, 400 | "input": [ 401 | "float(c)" 402 | ], 403 | "language": "python", 404 | "metadata": {}, 405 | "outputs": [ 406 | { 407 | "metadata": {}, 408 | "output_type": "pyout", 409 | "prompt_number": 34, 410 | "text": [ 411 | "5.0" 412 | ] 413 | } 414 | ], 415 | "prompt_number": 34 416 | }, 417 | { 418 | "cell_type": "code", 419 | "collapsed": false, 420 | "input": [ 421 | "print(c/2.)" 422 | ], 423 | "language": "python", 424 | "metadata": {}, 425 | "outputs": [ 426 | { 427 | "output_type": "stream", 428 | "stream": "stdout", 429 | "text": [ 430 | "2.5\n" 431 | ] 432 | } 433 | ], 434 | "prompt_number": 35 435 | }, 436 | { 437 | "cell_type": "code", 438 | "collapsed": false, 439 | "input": [ 440 | "type(c/2.) # the type of the data obtained by this operation is float" 441 | ], 442 | "language": "python", 443 | "metadata": {}, 444 | "outputs": [ 445 | { 446 | "metadata": {}, 447 | "output_type": "pyout", 448 | "prompt_number": 36, 449 | "text": [ 450 | "float" 451 | ] 452 | } 453 | ], 454 | "prompt_number": 36 455 | }, 456 | { 457 | "cell_type": "code", 458 | "collapsed": false, 459 | "input": [ 460 | "type(c+2.)" 461 | ], 462 | "language": "python", 463 | "metadata": {}, 464 | "outputs": [ 465 | { 466 | "metadata": {}, 467 | "output_type": "pyout", 468 | "prompt_number": 37, 469 | "text": [ 470 | "float" 471 | ] 472 | } 473 | ], 474 | "prompt_number": 37 475 | }, 476 | { 477 | "cell_type": "code", 478 | "collapsed": false, 479 | "input": [ 480 | "d=2.\n", 481 | "print(c+d)" 482 | ], 483 | "language": "python", 484 | "metadata": {}, 485 | "outputs": [ 486 | { 487 | "output_type": "stream", 488 | "stream": "stdout", 489 | "text": [ 490 | "7.0\n" 491 | ] 492 | } 493 | ], 494 | "prompt_number": 38 495 | }, 496 | { 497 | "cell_type": "markdown", 498 | "metadata": {}, 499 | "source": [ 500 | "NOTE: the operator + can work with string as well. Each type of object can interpret arithmetic operators differently. " 501 | ] 502 | }, 503 | { 504 | "cell_type": "code", 505 | "collapsed": false, 506 | "input": [ 507 | "print(a+b)\n", 508 | "print a.__add__(b)" 509 | ], 510 | "language": "python", 511 | "metadata": {}, 512 | "outputs": [ 513 | { 514 | "output_type": "stream", 515 | "stream": "stdout", 516 | "text": [ 517 | "hellogoodbye\n", 518 | "hellogoodbye\n" 519 | ] 520 | } 521 | ], 522 | "prompt_number": 40 523 | }, 524 | { 525 | "cell_type": "markdown", 526 | "metadata": {}, 527 | "source": [ 528 | "What other data types do we have?\n", 529 | "\n", 530 | "Besides INT STR and FLOAT we have then BOOL (boolean, True/False, 1/0)" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "collapsed": false, 536 | "input": [ 537 | "f = a==b\n", 538 | "print type(f)\n", 539 | "print f" 540 | ], 541 | "language": "python", 542 | "metadata": {}, 543 | "outputs": [ 544 | { 545 | "output_type": "stream", 546 | "stream": "stdout", 547 | "text": [ 548 | "\n", 549 | "False\n" 550 | ] 551 | } 552 | ], 553 | "prompt_number": 42 554 | }, 555 | { 556 | "cell_type": "markdown", 557 | "metadata": {}, 558 | "source": [ 559 | "In order to change the data type of a variable, use the functions that are named after the name of the type:\n", 560 | "\n", 561 | "- to change to integer -> int()\n", 562 | "- to change to str -> str()\n", 563 | "- to change to boolean -> bool()" 564 | ] 565 | }, 566 | { 567 | "cell_type": "code", 568 | "collapsed": false, 569 | "input": [ 570 | "print a+str(5) # changing the type of 5 to a string allows us to \"add\" it to hello\n", 571 | "print(int (2.5)) \n", 572 | "print float(4)\n", 573 | "print bool(4)\n", 574 | "print bool(0)\n", 575 | "print bool(1)" 576 | ], 577 | "language": "python", 578 | "metadata": {}, 579 | "outputs": [ 580 | { 581 | "output_type": "stream", 582 | "stream": "stdout", 583 | "text": [ 584 | "hello5\n", 585 | "2\n", 586 | "4.0\n", 587 | "True\n", 588 | "False\n", 589 | "True\n" 590 | ] 591 | } 592 | ], 593 | "prompt_number": 44 594 | }, 595 | { 596 | "cell_type": "markdown", 597 | "metadata": {}, 598 | "source": [ 599 | "If you had tried to sum a+5 without transforming 5 to str:" 600 | ] 601 | }, 602 | { 603 | "cell_type": "code", 604 | "collapsed": false, 605 | "input": [ 606 | "print a+5" 607 | ], 608 | "language": "python", 609 | "metadata": {}, 610 | "outputs": [ 611 | { 612 | "ename": "TypeError", 613 | "evalue": "cannot concatenate 'str' and 'int' objects", 614 | "output_type": "pyerr", 615 | "traceback": [ 616 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 617 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 618 | "\u001b[0;31mTypeError\u001b[0m: cannot concatenate 'str' and 'int' objects" 619 | ] 620 | } 621 | ], 622 | "prompt_number": 45 623 | }, 624 | { 625 | "cell_type": "markdown", 626 | "metadata": {}, 627 | "source": [ 628 | "You would get an error, called \"TypeError\"\n", 629 | "\n", 630 | "## Error Handling ##\n", 631 | "\n", 632 | "Python has a very rich framework for generating and handling errors (a.k.a. exceptions).\n", 633 | "\n", 634 | "Errors can be \"caught\" by using the \"try\" syntax. Python will detect if there is a specific type of error (in this case \"TypeError\") and execute special code to deal with the situation." 635 | ] 636 | }, 637 | { 638 | "cell_type": "code", 639 | "collapsed": false, 640 | "input": [ 641 | "try:\n", 642 | " print a+5\n", 643 | "except TypeError:\n", 644 | " print a + str(5)\n", 645 | " print 'caught an error'" 646 | ], 647 | "language": "python", 648 | "metadata": {}, 649 | "outputs": [ 650 | { 651 | "output_type": "stream", 652 | "stream": "stdout", 653 | "text": [ 654 | "hello5\n", 655 | "caught an error\n" 656 | ] 657 | } 658 | ], 659 | "prompt_number": 46 660 | }, 661 | { 662 | "cell_type": "markdown", 663 | "metadata": {}, 664 | "source": [ 665 | "# Containers #\n", 666 | "\n", 667 | "Here we are going to talk about \"lists.\" These are part of the core python code, so it is not in numpy; because they belong to the core of python, these are general containers for data, and are not necessarily optimized for scientific data and operations.\n", 668 | "\n", 669 | "## List ##\n", 670 | "A list is a data structure that holds multiple items in a specific order.\n", 671 | "To create a list, we enclose the items in square brackets, separated by commas. Any type of data can be included." 672 | ] 673 | }, 674 | { 675 | "cell_type": "code", 676 | "collapsed": false, 677 | "input": [ 678 | "mylist=[a,b,c,d]\n", 679 | "print mylist" 680 | ], 681 | "language": "python", 682 | "metadata": {}, 683 | "outputs": [ 684 | { 685 | "output_type": "stream", 686 | "stream": "stdout", 687 | "text": [ 688 | "['hello', 'goodbye', 5, 2.0]\n" 689 | ] 690 | } 691 | ], 692 | "prompt_number": 47 693 | }, 694 | { 695 | "cell_type": "markdown", 696 | "metadata": {}, 697 | "source": [ 698 | "To access specific items from the list, use square brackets." 699 | ] 700 | }, 701 | { 702 | "cell_type": "code", 703 | "collapsed": false, 704 | "input": [ 705 | "#get the first item\n", 706 | "print mylist[0]\n", 707 | "#get the last item if we knew the lenght\n", 708 | "print mylist[3]\n", 709 | "#get the last item if we didn't know the lenght\n", 710 | "print mylist[-1]\n", 711 | "#get the second to last\n", 712 | "print mylist[-2]\n", 713 | "#get the first 2 items\n", 714 | "print mylist[:2]\n", 715 | "#get the last 2 elements if we knew the lenght\n", 716 | "print mylist[2:]\n", 717 | "#if we didn't know the lenght\n", 718 | "print mylist[-2:]" 719 | ], 720 | "language": "python", 721 | "metadata": {}, 722 | "outputs": [ 723 | { 724 | "output_type": "stream", 725 | "stream": "stdout", 726 | "text": [ 727 | "hello\n", 728 | "2.0\n", 729 | "2.0\n", 730 | "5\n", 731 | "['hello', 'goodbye']\n", 732 | "[5, 2.0]\n", 733 | "[5, 2.0]\n" 734 | ] 735 | } 736 | ], 737 | "prompt_number": 48 738 | }, 739 | { 740 | "cell_type": "markdown", 741 | "metadata": {}, 742 | "source": [ 743 | "IMPORTANT NOTE: the first element is indexed 0, and not 1 like Matlab.\n", 744 | "\n", 745 | "Indexing in python: [0:3] - it will include the 0 element up to the last one BUT not the last one => [0 1 2]; it is always such that it does not include the last one.\n", 746 | "\n", 747 | "mylist. will have a series of methods. Like append => mylist.append()\n", 748 | "\n", 749 | "This appends values to the list." 750 | ] 751 | }, 752 | { 753 | "cell_type": "code", 754 | "collapsed": false, 755 | "input": [ 756 | "mylist.append(False) #if I want to get the help without using the ?, you can write the brackets () and then press shift+tab\n", 757 | "print(mylist)\n" 758 | ], 759 | "language": "python", 760 | "metadata": {}, 761 | "outputs": [ 762 | { 763 | "output_type": "stream", 764 | "stream": "stdout", 765 | "text": [ 766 | "['hello', 'goodbye', 5, 2.0, False]\n" 767 | ] 768 | } 769 | ], 770 | "prompt_number": 49 771 | }, 772 | { 773 | "cell_type": "markdown", 774 | "metadata": {}, 775 | "source": [ 776 | "Another method is pop => mylist.pop() \n", 777 | "\n", 778 | "this will pop - delete - the last value of mylist, and it will create a variable with that deleted value and that data type." 779 | ] 780 | }, 781 | { 782 | "cell_type": "code", 783 | "collapsed": false, 784 | "input": [ 785 | "mylist.pop()\n" 786 | ], 787 | "language": "python", 788 | "metadata": {}, 789 | "outputs": [ 790 | { 791 | "metadata": {}, 792 | "output_type": "pyout", 793 | "prompt_number": 50, 794 | "text": [ 795 | "False" 796 | ] 797 | } 798 | ], 799 | "prompt_number": 50 800 | }, 801 | { 802 | "cell_type": "code", 803 | "collapsed": false, 804 | "input": [ 805 | "mylist.append(False) \n", 806 | "last = mylist.pop()" 807 | ], 808 | "language": "python", 809 | "metadata": {}, 810 | "outputs": [], 811 | "prompt_number": 51 812 | }, 813 | { 814 | "cell_type": "code", 815 | "collapsed": false, 816 | "input": [ 817 | "mylist.append(False) \n", 818 | "last = mylist.pop()\n", 819 | "print(last)\n", 820 | "type(last)" 821 | ], 822 | "language": "python", 823 | "metadata": {}, 824 | "outputs": [ 825 | { 826 | "output_type": "stream", 827 | "stream": "stdout", 828 | "text": [ 829 | "False\n" 830 | ] 831 | }, 832 | { 833 | "metadata": {}, 834 | "output_type": "pyout", 835 | "prompt_number": 52, 836 | "text": [ 837 | "bool" 838 | ] 839 | } 840 | ], 841 | "prompt_number": 52 842 | }, 843 | { 844 | "cell_type": "code", 845 | "collapsed": false, 846 | "input": [ 847 | "last = mylist.pop() #here it pops the last value which will be 2.\n", 848 | "print(mylist)\n", 849 | "print(last)\n", 850 | "type(last)" 851 | ], 852 | "language": "python", 853 | "metadata": {}, 854 | "outputs": [ 855 | { 856 | "output_type": "stream", 857 | "stream": "stdout", 858 | "text": [ 859 | "['hello', 'goodbye', 5]\n", 860 | "2.0\n" 861 | ] 862 | }, 863 | { 864 | "metadata": {}, 865 | "output_type": "pyout", 866 | "prompt_number": 53, 867 | "text": [ 868 | "float" 869 | ] 870 | } 871 | ], 872 | "prompt_number": 53 873 | }, 874 | { 875 | "cell_type": "markdown", 876 | "metadata": {}, 877 | "source": [ 878 | "## Nested lists\n", 879 | "\n", 880 | "To have a \"multidimensional list,\" we just make a list of lists." 881 | ] 882 | }, 883 | { 884 | "cell_type": "code", 885 | "collapsed": false, 886 | "input": [ 887 | "nested_list= [[1,2],[3,4]]\n", 888 | "print(nested_list)\n", 889 | "print(nested_list[1]) #remember 1 is actually the 2nd element!!! therefore the second element of the nested_list is [3,4]\n", 890 | "print(nested_list[1][1]) # it prints the 2nd element of the second element\n", 891 | "type(nested_list[1][1])" 892 | ], 893 | "language": "python", 894 | "metadata": {}, 895 | "outputs": [ 896 | { 897 | "output_type": "stream", 898 | "stream": "stdout", 899 | "text": [ 900 | "[[1, 2], [3, 4]]\n", 901 | "[3, 4]\n", 902 | "4\n" 903 | ] 904 | }, 905 | { 906 | "metadata": {}, 907 | "output_type": "pyout", 908 | "prompt_number": 54, 909 | "text": [ 910 | "int" 911 | ] 912 | } 913 | ], 914 | "prompt_number": 54 915 | }, 916 | { 917 | "cell_type": "markdown", 918 | "metadata": {}, 919 | "source": [ 920 | "# Loops #\n", 921 | "\n", 922 | "Now we learn a bit about how to iterate over lists. In this case, we writ a code to look at each item in our list, print it, and test whether it is an integer." 923 | ] 924 | }, 925 | { 926 | "cell_type": "code", 927 | "collapsed": false, 928 | "input": [ 929 | "mylist=[a,b,c,d]\n", 930 | "print mylist\n", 931 | "\n", 932 | "print('Now starting the loop')\n", 933 | "\n", 934 | "for item in mylist: # we don't have to define how long the vector is, it knows how long it is.\n", 935 | " print item\n", 936 | " if isinstance(item, int):\n", 937 | " print \"Found an integer\"" 938 | ], 939 | "language": "python", 940 | "metadata": {}, 941 | "outputs": [ 942 | { 943 | "output_type": "stream", 944 | "stream": "stdout", 945 | "text": [ 946 | "['hello', 'goodbye', 5, 2.0]\n", 947 | "Now starting the loop\n", 948 | "hello\n", 949 | "goodbye\n", 950 | "5\n", 951 | "Found an integer\n", 952 | "2.0\n" 953 | ] 954 | } 955 | ], 956 | "prompt_number": 57 957 | }, 958 | { 959 | "cell_type": "markdown", 960 | "metadata": {}, 961 | "source": [ 962 | "Please note the indentation, which tells python which line belongs to the for loop (\"print item\" and \"if isinstance(item, int):\") and which belongs to the if loop (\"print \"Found an integer\"\")\n", 963 | "\n", 964 | "\n", 965 | "Let's now write a loop to create a list. First create an empty list, and then append elements one by one." 966 | ] 967 | }, 968 | { 969 | "cell_type": "code", 970 | "collapsed": false, 971 | "input": [ 972 | "newlist = []\n", 973 | "for n in range(5):\n", 974 | " newlist.append(n)\n", 975 | " \n", 976 | "print(type(newlist))\n", 977 | "print(type(newlist[0]))\n", 978 | "print newlist\n", 979 | "print newlist == range(5)" 980 | ], 981 | "language": "python", 982 | "metadata": {}, 983 | "outputs": [ 984 | { 985 | "output_type": "stream", 986 | "stream": "stdout", 987 | "text": [ 988 | "\n", 989 | "\n", 990 | "[0, 1, 2, 3, 4]\n", 991 | "True\n" 992 | ] 993 | } 994 | ], 995 | "prompt_number": 58 996 | }, 997 | { 998 | "cell_type": "markdown", 999 | "metadata": {}, 1000 | "source": [ 1001 | "The \"range\" function builds list of integers that starts from zero (by default, unless a different start is indicated), and goes up to the last element, but similarly to the indexing, doesn't include it:\n", 1002 | "\n", 1003 | "```\n", 1004 | "range([start,] stop[, step]) -> list of integers\n", 1005 | "```\n", 1006 | "\n", 1007 | "Return a list containing an arithmetic progression of integers.\n", 1008 | "range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n", 1009 | "When step is given, it specifies the increment (or decrement).\n", 1010 | "For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n", 1011 | "These are exactly the valid indices for a list of 4 elements.\n" 1012 | ] 1013 | }, 1014 | { 1015 | "cell_type": "code", 1016 | "collapsed": false, 1017 | "input": [ 1018 | "print range(50,54)\n", 1019 | "print range(5) + range(50,54) # this \"sum\" is intendend as an appending, not a mathematical sum.\n" 1020 | ], 1021 | "language": "python", 1022 | "metadata": {}, 1023 | "outputs": [ 1024 | { 1025 | "output_type": "stream", 1026 | "stream": "stdout", 1027 | "text": [ 1028 | "[50, 51, 52, 53]\n", 1029 | "[0, 1, 2, 3, 4, 50, 51, 52, 53]\n" 1030 | ] 1031 | } 1032 | ], 1033 | "prompt_number": 60 1034 | }, 1035 | { 1036 | "cell_type": "markdown", 1037 | "metadata": {}, 1038 | "source": [ 1039 | "# Functions #\n", 1040 | "\n", 1041 | "Let's now write our first fucntion in python.\n", 1042 | "It's important to notice that whatever variables are defined within the function do not enter into the top-level namespace. \n", 1043 | "\n", 1044 | "This function is designed to take two lists and add them together element by element.\n", 1045 | "In this function, we use variables named a and b (already defined in the notebook), but these variable names are within the function." 1046 | ] 1047 | }, 1048 | { 1049 | "cell_type": "code", 1050 | "collapsed": false, 1051 | "input": [ 1052 | "def add_lists(a,b):\n", 1053 | " c = []\n", 1054 | " for item1,item2 in zip(a,b):\n", 1055 | " c.append(item1 + item2)\n", 1056 | " return c\n", 1057 | "\n", 1058 | "#Here I call and print the function\n", 1059 | "print add_lists(range(5), range(50,55))\n", 1060 | "\n", 1061 | "#Here I call and print the function\n", 1062 | "print add_lists(range(6), range(60,66))" 1063 | ], 1064 | "language": "python", 1065 | "metadata": {}, 1066 | "outputs": [ 1067 | { 1068 | "output_type": "stream", 1069 | "stream": "stdout", 1070 | "text": [ 1071 | "[50, 52, 54, 56, 58]\n", 1072 | "[60, 62, 64, 66, 68, 70]\n" 1073 | ] 1074 | } 1075 | ], 1076 | "prompt_number": 63 1077 | }, 1078 | { 1079 | "cell_type": "markdown", 1080 | "metadata": {}, 1081 | "source": [ 1082 | "# Final remarks #\n", 1083 | "\n", 1084 | "Lists are core python data formats, not designed for scientific or numerical work. Our next lesson is on numpy arrays. Let's get a preview of how things are different when we use numpy.\n", 1085 | "\n", 1086 | "(The magic function %timeit can be used to evaluate how fast a line of code runs.)" 1087 | ] 1088 | }, 1089 | { 1090 | "cell_type": "code", 1091 | "collapsed": false, 1092 | "input": [ 1093 | "%timeit add_lists(range(10000), range(10000))" 1094 | ], 1095 | "language": "python", 1096 | "metadata": {}, 1097 | "outputs": [ 1098 | { 1099 | "output_type": "stream", 1100 | "stream": "stdout", 1101 | "text": [ 1102 | "1000 loops, best of 3: 1.68 ms per loop\n" 1103 | ] 1104 | } 1105 | ], 1106 | "prompt_number": 64 1107 | }, 1108 | { 1109 | "cell_type": "markdown", 1110 | "metadata": {}, 1111 | "source": [ 1112 | "it is pretty slow, but if we use numpy it will be faster." 1113 | ] 1114 | }, 1115 | { 1116 | "cell_type": "code", 1117 | "collapsed": false, 1118 | "input": [ 1119 | "import numpy\n", 1120 | "%timeit numpy.arange(10000)+numpy.arange(10000)" 1121 | ], 1122 | "language": "python", 1123 | "metadata": {}, 1124 | "outputs": [ 1125 | { 1126 | "output_type": "stream", 1127 | "stream": "stdout", 1128 | "text": [ 1129 | "100000 loops, best of 3: 19.1 \u00b5s per loop\n" 1130 | ] 1131 | } 1132 | ], 1133 | "prompt_number": 65 1134 | } 1135 | ], 1136 | "metadata": {} 1137 | } 1138 | ] 1139 | } -------------------------------------------------------------------------------- /one_day_workshop/01_core_python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Core Python Language #\n", 8 | "\n", 9 | "Mostly copied from the [official python tutorial](https://docs.python.org/3/tutorial/)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "## Invoking Python ##\n", 17 | "\n", 18 | "There are three main ways to use python.\n", 19 | "\n", 20 | "1. By running a python file, e.g. `python myscript.py`\n", 21 | "1. Through an interactive console (python interpreter or ipython shell)\n", 22 | "1. In an interactive iPython notebook\n", 23 | "\n", 24 | "We will be using the iPython notebook." 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "## Python Versions ##\n", 32 | "\n", 33 | "There are two versions of the python language out there: python 2 and python 3. Python 2 is more common in the wild but is depracated. The community is moving to python 3. As new python learners, you should learn python 3. But it is important to be aware that python 2 exists. It is possible that a package you want to use is only supported in python 2. In general, it is pretty easy to switch between then.\n", 34 | "\n", 35 | "Some of the main changes in python 3 are:\n", 36 | "\n", 37 | "* ``print`` is a function\n", 38 | "* Integer division returns a float\n", 39 | "* Iterators behave differently\n", 40 | "* Unicode is used for encoding code\n" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "## Basic Variables: Numbers and String ##" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 2, 53 | "metadata": { 54 | "collapsed": true 55 | }, 56 | "outputs": [], 57 | "source": [ 58 | "# comments are anything that comes after the \"#\" symbol\n", 59 | "a = 1 # assign 1 to variable a\n", 60 | "b = \"hello\" # assign \"hello\" to variable b" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "The following identifiers are used as reserved words, or keywords of the language, and cannot be used as ordinary identifiers. They must be spelled exactly as written here:\n", 68 | "\n", 69 | " False class finally is return\n", 70 | " None continue for lambda try\n", 71 | " True def from nonlocal while\n", 72 | " and del global not with\n", 73 | " as elif if or yield\n", 74 | " assert else import pass\n", 75 | " break except in raise\n", 76 | " \n", 77 | "Additionally, the following a built in functions which are always available in your namespace once you open a python interpreter\n", 78 | "\n", 79 | " abs() dict() help() min() setattr() all() dir() hex() next() slice() any()\n", 80 | " divmod() id() object() sorted() ascii() enumerate() input() oct() staticmethod()\n", 81 | " bin() eval() int() open() str() bool() exec() isinstance() ord() sum() bytearray()\n", 82 | " filter() issubclass() pow() super() bytes() float() iter() print() tuple()\n", 83 | " callable() format() len() property() type() chr() frozenset() list() range()\n", 84 | " vars() classmethod() getattr() locals() repr() zip() compile() globals() map()\n", 85 | " reversed() __import__() complex() hasattr() max() round() delattr() hash()\n", 86 | " memoryview() set()\n", 87 | "\n" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 3, 93 | "metadata": { 94 | "collapsed": false 95 | }, 96 | "outputs": [ 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "1\n", 102 | "hello\n", 103 | "1 hello\n" 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "# how to we see our variables?\n", 109 | "print(a)\n", 110 | "print(b)\n", 111 | "print(a,b)" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "All variables are objects. Every object has a type (class). To find out what type your variables are" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 4, 124 | "metadata": { 125 | "collapsed": false 126 | }, 127 | "outputs": [ 128 | { 129 | "name": "stdout", 130 | "output_type": "stream", 131 | "text": [ 132 | "\n", 133 | "\n" 134 | ] 135 | } 136 | ], 137 | "source": [ 138 | "print(type(a))\n", 139 | "print(type(b))" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 5, 145 | "metadata": { 146 | "collapsed": false 147 | }, 148 | "outputs": [ 149 | { 150 | "data": { 151 | "text/plain": [ 152 | "str" 153 | ] 154 | }, 155 | "execution_count": 5, 156 | "metadata": {}, 157 | "output_type": "execute_result" 158 | } 159 | ], 160 | "source": [ 161 | "# as a shortcut, iPython notebooks will automatically print whatever is on the last line\n", 162 | "type(b)" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 6, 168 | "metadata": { 169 | "collapsed": false 170 | }, 171 | "outputs": [ 172 | { 173 | "name": "stdout", 174 | "output_type": "stream", 175 | "text": [ 176 | "True\n", 177 | "False\n" 178 | ] 179 | } 180 | ], 181 | "source": [ 182 | "# we can check for the type of an object\n", 183 | "print(type(a) is int)\n", 184 | "print(type(a) is str)" 185 | ] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": {}, 190 | "source": [ 191 | "Different objects attributes and methods, which can be accessed via the syntax ``variable.method``\n", 192 | "\n", 193 | "IPython will autocomplete if you press ```` to show you the methods available." 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 7, 199 | "metadata": { 200 | "collapsed": false 201 | }, 202 | "outputs": [ 203 | { 204 | "data": { 205 | "text/plain": [ 206 | "" 207 | ] 208 | }, 209 | "execution_count": 7, 210 | "metadata": {}, 211 | "output_type": "execute_result" 212 | } 213 | ], 214 | "source": [ 215 | "# this returns the method itself\n", 216 | "b.capitalize" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": 8, 222 | "metadata": { 223 | "collapsed": false 224 | }, 225 | "outputs": [ 226 | { 227 | "data": { 228 | "text/plain": [ 229 | "'Hello'" 230 | ] 231 | }, 232 | "execution_count": 8, 233 | "metadata": {}, 234 | "output_type": "execute_result" 235 | } 236 | ], 237 | "source": [ 238 | "# this calls the method\n", 239 | "b.capitalize()\n", 240 | "# there are lots of other methods" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 9, 246 | "metadata": { 247 | "collapsed": false 248 | }, 249 | "outputs": [ 250 | { 251 | "name": "stdout", 252 | "output_type": "stream", 253 | "text": [ 254 | "helloWorld\n", 255 | "3\n" 256 | ] 257 | }, 258 | { 259 | "ename": "TypeError", 260 | "evalue": "unsupported operand type(s) for +: 'int' and 'str'", 261 | "output_type": "error", 262 | "traceback": [ 263 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 264 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 265 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 266 | "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" 267 | ] 268 | } 269 | ], 270 | "source": [ 271 | "# binary operations act differently on different types of objects\n", 272 | "c = 'World'\n", 273 | "print(b + c)\n", 274 | "print(a + 2)\n", 275 | "print(a + b)" 276 | ] 277 | }, 278 | { 279 | "cell_type": "markdown", 280 | "metadata": {}, 281 | "source": [ 282 | "## Math ##\n", 283 | "\n", 284 | "Basic arithmetic and boolean logic is part of the core python library." 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": 10, 290 | "metadata": { 291 | "collapsed": false 292 | }, 293 | "outputs": [ 294 | { 295 | "data": { 296 | "text/plain": [ 297 | "-3" 298 | ] 299 | }, 300 | "execution_count": 10, 301 | "metadata": {}, 302 | "output_type": "execute_result" 303 | } 304 | ], 305 | "source": [ 306 | "# addition / subtraction\n", 307 | "1+1-5" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": 11, 313 | "metadata": { 314 | "collapsed": false 315 | }, 316 | "outputs": [ 317 | { 318 | "data": { 319 | "text/plain": [ 320 | "50" 321 | ] 322 | }, 323 | "execution_count": 11, 324 | "metadata": {}, 325 | "output_type": "execute_result" 326 | } 327 | ], 328 | "source": [ 329 | "# multiplication\n", 330 | "5 * 10" 331 | ] 332 | }, 333 | { 334 | "cell_type": "code", 335 | "execution_count": 12, 336 | "metadata": { 337 | "collapsed": false 338 | }, 339 | "outputs": [ 340 | { 341 | "data": { 342 | "text/plain": [ 343 | "0.5" 344 | ] 345 | }, 346 | "execution_count": 12, 347 | "metadata": {}, 348 | "output_type": "execute_result" 349 | } 350 | ], 351 | "source": [ 352 | "# division\n", 353 | "1/2" 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "execution_count": 13, 359 | "metadata": { 360 | "collapsed": false 361 | }, 362 | "outputs": [ 363 | { 364 | "data": { 365 | "text/plain": [ 366 | "float" 367 | ] 368 | }, 369 | "execution_count": 13, 370 | "metadata": {}, 371 | "output_type": "execute_result" 372 | } 373 | ], 374 | "source": [ 375 | "# that was automatically converted to a float\n", 376 | "type(1/2)" 377 | ] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "execution_count": 14, 382 | "metadata": { 383 | "collapsed": false 384 | }, 385 | "outputs": [ 386 | { 387 | "data": { 388 | "text/plain": [ 389 | "16" 390 | ] 391 | }, 392 | "execution_count": 14, 393 | "metadata": {}, 394 | "output_type": "execute_result" 395 | } 396 | ], 397 | "source": [ 398 | "# exponentiation\n", 399 | "2**4" 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "execution_count": 15, 405 | "metadata": { 406 | "collapsed": false 407 | }, 408 | "outputs": [ 409 | { 410 | "data": { 411 | "text/plain": [ 412 | "1" 413 | ] 414 | }, 415 | "execution_count": 15, 416 | "metadata": {}, 417 | "output_type": "execute_result" 418 | } 419 | ], 420 | "source": [ 421 | "# rounding\n", 422 | "round(9/10)" 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": 16, 428 | "metadata": { 429 | "collapsed": false 430 | }, 431 | "outputs": [ 432 | { 433 | "data": { 434 | "text/plain": [ 435 | "(-0.2+0.4j)" 436 | ] 437 | }, 438 | "execution_count": 16, 439 | "metadata": {}, 440 | "output_type": "execute_result" 441 | } 442 | ], 443 | "source": [ 444 | "# built in complex number support\n", 445 | "(1+2j) / (3-4j)" 446 | ] 447 | }, 448 | { 449 | "cell_type": "code", 450 | "execution_count": 17, 451 | "metadata": { 452 | "collapsed": false 453 | }, 454 | "outputs": [ 455 | { 456 | "data": { 457 | "text/plain": [ 458 | "True" 459 | ] 460 | }, 461 | "execution_count": 17, 462 | "metadata": {}, 463 | "output_type": "execute_result" 464 | } 465 | ], 466 | "source": [ 467 | "# logic\n", 468 | "True and True" 469 | ] 470 | }, 471 | { 472 | "cell_type": "code", 473 | "execution_count": 18, 474 | "metadata": { 475 | "collapsed": false 476 | }, 477 | "outputs": [ 478 | { 479 | "data": { 480 | "text/plain": [ 481 | "False" 482 | ] 483 | }, 484 | "execution_count": 18, 485 | "metadata": {}, 486 | "output_type": "execute_result" 487 | } 488 | ], 489 | "source": [ 490 | "True and False" 491 | ] 492 | }, 493 | { 494 | "cell_type": "code", 495 | "execution_count": 19, 496 | "metadata": { 497 | "collapsed": false 498 | }, 499 | "outputs": [ 500 | { 501 | "data": { 502 | "text/plain": [ 503 | "True" 504 | ] 505 | }, 506 | "execution_count": 19, 507 | "metadata": {}, 508 | "output_type": "execute_result" 509 | } 510 | ], 511 | "source": [ 512 | "True or True" 513 | ] 514 | }, 515 | { 516 | "cell_type": "code", 517 | "execution_count": 20, 518 | "metadata": { 519 | "collapsed": false 520 | }, 521 | "outputs": [ 522 | { 523 | "data": { 524 | "text/plain": [ 525 | "True" 526 | ] 527 | }, 528 | "execution_count": 20, 529 | "metadata": {}, 530 | "output_type": "execute_result" 531 | } 532 | ], 533 | "source": [ 534 | "(not True) or (not False)" 535 | ] 536 | }, 537 | { 538 | "cell_type": "markdown", 539 | "metadata": {}, 540 | "source": [ 541 | "## Conditionals ##\n", 542 | "\n", 543 | "The first step to programming. Plus an intro to python syntax." 544 | ] 545 | }, 546 | { 547 | "cell_type": "code", 548 | "execution_count": 21, 549 | "metadata": { 550 | "collapsed": false 551 | }, 552 | "outputs": [ 553 | { 554 | "name": "stdout", 555 | "output_type": "stream", 556 | "text": [ 557 | "Positive Number\n" 558 | ] 559 | } 560 | ], 561 | "source": [ 562 | "x = 100\n", 563 | "if x > 0:\n", 564 | " print('Positive Number')\n", 565 | "elif x < 0:\n", 566 | " print('Negative Number')\n", 567 | "else:\n", 568 | " print ('Zero!')" 569 | ] 570 | }, 571 | { 572 | "cell_type": "code", 573 | "execution_count": 22, 574 | "metadata": { 575 | "collapsed": false 576 | }, 577 | "outputs": [ 578 | { 579 | "name": "stdout", 580 | "output_type": "stream", 581 | "text": [ 582 | "Positive Number\n", 583 | "Huge number!\n" 584 | ] 585 | } 586 | ], 587 | "source": [ 588 | "# indentation is MANDATORY\n", 589 | "# blocks are closed by indentation level\n", 590 | "if x > 0:\n", 591 | " print('Positive Number')\n", 592 | " if x >= 100:\n", 593 | " print('Huge number!')" 594 | ] 595 | }, 596 | { 597 | "cell_type": "markdown", 598 | "metadata": {}, 599 | "source": [ 600 | "## More Flow Control ##" 601 | ] 602 | }, 603 | { 604 | "cell_type": "code", 605 | "execution_count": 26, 606 | "metadata": { 607 | "collapsed": false 608 | }, 609 | "outputs": [ 610 | { 611 | "name": "stdout", 612 | "output_type": "stream", 613 | "text": [ 614 | "10\n" 615 | ] 616 | } 617 | ], 618 | "source": [ 619 | "# make a loop \n", 620 | "count = 0\n", 621 | "while count < 10:\n", 622 | " # bad way\n", 623 | " # count = count + 1\n", 624 | " # better way\n", 625 | " count += 1\n", 626 | "print(count)" 627 | ] 628 | }, 629 | { 630 | "cell_type": "code", 631 | "execution_count": 29, 632 | "metadata": { 633 | "collapsed": false 634 | }, 635 | "outputs": [ 636 | { 637 | "name": "stdout", 638 | "output_type": "stream", 639 | "text": [ 640 | "0\n", 641 | "1\n", 642 | "2\n", 643 | "3\n", 644 | "4\n" 645 | ] 646 | } 647 | ], 648 | "source": [ 649 | "# use range\n", 650 | "for i in range(5):\n", 651 | " print(i)" 652 | ] 653 | }, 654 | { 655 | "cell_type": "markdown", 656 | "metadata": {}, 657 | "source": [ 658 | "__Important point__: in python, we always count from 0!" 659 | ] 660 | }, 661 | { 662 | "cell_type": "code", 663 | "execution_count": 30, 664 | "metadata": { 665 | "collapsed": false 666 | }, 667 | "outputs": [ 668 | { 669 | "data": { 670 | "text/plain": [ 671 | "type" 672 | ] 673 | }, 674 | "execution_count": 30, 675 | "metadata": {}, 676 | "output_type": "execute_result" 677 | } 678 | ], 679 | "source": [ 680 | "# what is range?\n", 681 | "type(range)" 682 | ] 683 | }, 684 | { 685 | "cell_type": "code", 686 | "execution_count": 33, 687 | "metadata": { 688 | "collapsed": true 689 | }, 690 | "outputs": [], 691 | "source": [ 692 | "range?" 693 | ] 694 | }, 695 | { 696 | "cell_type": "code", 697 | "execution_count": 37, 698 | "metadata": { 699 | "collapsed": false 700 | }, 701 | "outputs": [ 702 | { 703 | "name": "stdout", 704 | "output_type": "stream", 705 | "text": [ 706 | "dog 3\n", 707 | "cat 3\n", 708 | "fish 4\n" 709 | ] 710 | } 711 | ], 712 | "source": [ 713 | "# iterate over a list we make up\n", 714 | "for pet in ['dog', 'cat', 'fish']:\n", 715 | " print(pet, len(pet))" 716 | ] 717 | }, 718 | { 719 | "cell_type": "markdown", 720 | "metadata": {}, 721 | "source": [ 722 | "What is the thing in brackets? __A list!__ Lists are one of the core python data structures.\n", 723 | "\n", 724 | "## Lists ##" 725 | ] 726 | }, 727 | { 728 | "cell_type": "code", 729 | "execution_count": 64, 730 | "metadata": { 731 | "collapsed": false 732 | }, 733 | "outputs": [ 734 | { 735 | "data": { 736 | "text/plain": [ 737 | "list" 738 | ] 739 | }, 740 | "execution_count": 64, 741 | "metadata": {}, 742 | "output_type": "execute_result" 743 | } 744 | ], 745 | "source": [ 746 | "l = ['dog', 'cat', 'fish']\n", 747 | "type(l)" 748 | ] 749 | }, 750 | { 751 | "cell_type": "code", 752 | "execution_count": 39, 753 | "metadata": { 754 | "collapsed": false 755 | }, 756 | "outputs": [ 757 | { 758 | "data": { 759 | "text/plain": [ 760 | "['cat', 'dog', 'fish']" 761 | ] 762 | }, 763 | "execution_count": 39, 764 | "metadata": {}, 765 | "output_type": "execute_result" 766 | } 767 | ], 768 | "source": [ 769 | "# list have lots of methods\n", 770 | "l.sort()\n", 771 | "l" 772 | ] 773 | }, 774 | { 775 | "cell_type": "code", 776 | "execution_count": 46, 777 | "metadata": { 778 | "collapsed": false 779 | }, 780 | "outputs": [ 781 | { 782 | "data": { 783 | "text/plain": [ 784 | "[0, 1, 2, 3, 4]" 785 | ] 786 | }, 787 | "execution_count": 46, 788 | "metadata": {}, 789 | "output_type": "execute_result" 790 | } 791 | ], 792 | "source": [ 793 | "# we can convert a range to a list\n", 794 | "r = list(range(5))\n", 795 | "r" 796 | ] 797 | }, 798 | { 799 | "cell_type": "code", 800 | "execution_count": 47, 801 | "metadata": { 802 | "collapsed": false 803 | }, 804 | "outputs": [ 805 | { 806 | "name": "stdout", 807 | "output_type": "stream", 808 | "text": [ 809 | "p: 4\n", 810 | "r: [0, 1, 2, 3]\n", 811 | "p: 3\n", 812 | "r: [0, 1, 2]\n", 813 | "p: 2\n", 814 | "r: [0, 1]\n", 815 | "p: 1\n", 816 | "r: [0]\n", 817 | "p: 0\n", 818 | "r: []\n" 819 | ] 820 | } 821 | ], 822 | "source": [ 823 | "while r:\n", 824 | " p = r.pop()\n", 825 | " print('p:', p)\n", 826 | " print('r:', r)" 827 | ] 828 | }, 829 | { 830 | "cell_type": "markdown", 831 | "metadata": {}, 832 | "source": [ 833 | "There are many different ways to interact with lists. Exploring them is part of the fun of python.\n", 834 | "\n", 835 | "__list.append(x)__ Add an item to the end of the list. Equivalent to a[len(a):] = [x].\n", 836 | "\n", 837 | "__list.extend(L)__ \n", 838 | "Extend the list by appending all the items in the given list. Equivalent to a[len(a):] = L.\n", 839 | "\n", 840 | "__list.insert(i, x)__ Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).\n", 841 | "\n", 842 | "__list.remove(x)__ Remove the first item from the list whose value is x. It is an error if there is no such item.\n", 843 | "\n", 844 | "__list.pop([i])__ Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)\n", 845 | "\n", 846 | "__list.clear()__ Remove all items from the list. Equivalent to del a[:].\n", 847 | "\n", 848 | "__list.index(x)__ Return the index in the list of the first item whose value is x. It is an error if there is no such item.\n", 849 | "\n", 850 | "__list.count(x)__ Return the number of times x appears in the list.\n", 851 | "\n", 852 | "__list.sort()__ Sort the items of the list in place.\n", 853 | "\n", 854 | "__list.reverse()__ Reverse the elements of the list in place.\n", 855 | "\n", 856 | "__list.copy()__ Return a shallow copy of the list. Equivalent to a[:].\n", 857 | "\n", 858 | "\n", 859 | "Don't assume you know how list operations work!" 860 | ] 861 | }, 862 | { 863 | "cell_type": "code", 864 | "execution_count": 52, 865 | "metadata": { 866 | "collapsed": false 867 | }, 868 | "outputs": [ 869 | { 870 | "data": { 871 | "text/plain": [ 872 | "[0, 1, 2, 3, 4, 10, 11, 12, 13, 14]" 873 | ] 874 | }, 875 | "execution_count": 52, 876 | "metadata": {}, 877 | "output_type": "execute_result" 878 | } 879 | ], 880 | "source": [ 881 | "# \"add\" two lists\n", 882 | "x = list(range(5))\n", 883 | "y = list(range(10,15))\n", 884 | "z = x + y\n", 885 | "z" 886 | ] 887 | }, 888 | { 889 | "cell_type": "code", 890 | "execution_count": 55, 891 | "metadata": { 892 | "collapsed": false 893 | }, 894 | "outputs": [ 895 | { 896 | "name": "stdout", 897 | "output_type": "stream", 898 | "text": [ 899 | "first 0\n", 900 | "last 14\n", 901 | "first 3 [0, 1, 2]\n", 902 | "last 3 [12, 13, 14]\n", 903 | "middle, skipping every other item [10, 12, 14]\n" 904 | ] 905 | } 906 | ], 907 | "source": [ 908 | "# access items from a list\n", 909 | "print('first', z[0])\n", 910 | "print('last', z[-1])\n", 911 | "print('first 3', z[:3])\n", 912 | "print('last 3', z[-3:])\n", 913 | "print('middle, skipping every other item', z[5:10:2])" 914 | ] 915 | }, 916 | { 917 | "cell_type": "markdown", 918 | "metadata": {}, 919 | "source": [ 920 | "__MEMORIZE THIS SYNTAX!__ It is central to so much of python and often proves confusing for users coming from other languages.\n", 921 | "\n", 922 | "In terms of set notation, python indexing is _left inclusive_, _right exclusive_. If you remember this, you will never go wrong." 923 | ] 924 | }, 925 | { 926 | "cell_type": "code", 927 | "execution_count": 60, 928 | "metadata": { 929 | "collapsed": false 930 | }, 931 | "outputs": [ 932 | { 933 | "ename": "IndexError", 934 | "evalue": "list index out of range", 935 | "output_type": "error", 936 | "traceback": [ 937 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 938 | "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", 939 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# that means we get an error from the following\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mN\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mz\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mz\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mN\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 940 | "\u001b[0;31mIndexError\u001b[0m: list index out of range" 941 | ] 942 | } 943 | ], 944 | "source": [ 945 | "# that means we get an error from the following\n", 946 | "N = len(z)\n", 947 | "z[N]" 948 | ] 949 | }, 950 | { 951 | "cell_type": "code", 952 | "execution_count": 63, 953 | "metadata": { 954 | "collapsed": false 955 | }, 956 | "outputs": [ 957 | { 958 | "name": "stdout", 959 | "output_type": "stream", 960 | "text": [ 961 | "Ryan\n" 962 | ] 963 | } 964 | ], 965 | "source": [ 966 | "# this index notation also applies to strings\n", 967 | "name = 'Ryan Abernathey'\n", 968 | "print(name[:4])" 969 | ] 970 | }, 971 | { 972 | "cell_type": "code", 973 | "execution_count": 57, 974 | "metadata": { 975 | "collapsed": false 976 | }, 977 | "outputs": [ 978 | { 979 | "data": { 980 | "text/plain": [ 981 | "False" 982 | ] 983 | }, 984 | "execution_count": 57, 985 | "metadata": {}, 986 | "output_type": "execute_result" 987 | } 988 | ], 989 | "source": [ 990 | "# you can also test for the presence of items in a list\n", 991 | "5 in z" 992 | ] 993 | }, 994 | { 995 | "cell_type": "markdown", 996 | "metadata": {}, 997 | "source": [ 998 | "Lists are not meant for math! They don't have a datatype." 999 | ] 1000 | }, 1001 | { 1002 | "cell_type": "code", 1003 | "execution_count": 58, 1004 | "metadata": { 1005 | "collapsed": false 1006 | }, 1007 | "outputs": [ 1008 | { 1009 | "data": { 1010 | "text/plain": [ 1011 | "[0, 1, 2, 3, 'fish', 10, 11, 12, 13, 14]" 1012 | ] 1013 | }, 1014 | "execution_count": 58, 1015 | "metadata": {}, 1016 | "output_type": "execute_result" 1017 | } 1018 | ], 1019 | "source": [ 1020 | "z[4] = 'fish'\n", 1021 | "z" 1022 | ] 1023 | }, 1024 | { 1025 | "cell_type": "markdown", 1026 | "metadata": {}, 1027 | "source": [ 1028 | "Python is full of tricks for iterating and working with lists" 1029 | ] 1030 | }, 1031 | { 1032 | "cell_type": "code", 1033 | "execution_count": 59, 1034 | "metadata": { 1035 | "collapsed": false 1036 | }, 1037 | "outputs": [ 1038 | { 1039 | "data": { 1040 | "text/plain": [ 1041 | "[0, 1, 4, 9, 16]" 1042 | ] 1043 | }, 1044 | "execution_count": 59, 1045 | "metadata": {}, 1046 | "output_type": "execute_result" 1047 | } 1048 | ], 1049 | "source": [ 1050 | "# a cool python trick: list comprehension\n", 1051 | "squares = [n**2 for n in range(5)]\n", 1052 | "squares" 1053 | ] 1054 | }, 1055 | { 1056 | "cell_type": "code", 1057 | "execution_count": 75, 1058 | "metadata": { 1059 | "collapsed": false 1060 | }, 1061 | "outputs": [ 1062 | { 1063 | "name": "stdout", 1064 | "output_type": "stream", 1065 | "text": [ 1066 | "first: 0 second: 10\n", 1067 | "first: 1 second: 11\n", 1068 | "first: 2 second: 12\n", 1069 | "first: 3 second: 13\n", 1070 | "first: 4 second: 14\n" 1071 | ] 1072 | } 1073 | ], 1074 | "source": [ 1075 | "# iterate over two lists together uzing zip\n", 1076 | "for item1, item2 in zip(x,y):\n", 1077 | " print('first:', item1, 'second:', item2)" 1078 | ] 1079 | }, 1080 | { 1081 | "cell_type": "markdown", 1082 | "metadata": {}, 1083 | "source": [ 1084 | "## Other Data Structures ##\n", 1085 | "\n", 1086 | "We are almost there. We have the building blocks we need to do basic programming. But python has some other data structures we need to learn about.\n", 1087 | "\n", 1088 | "## Tuples ##\n", 1089 | "\n", 1090 | "Tuples are similar to lists, but they are _immutable_—they can't be extended or modified. What is the point of this? Generally speaking: to pack together inhomogeneous data. Tuples can then be unpacked and distributed by other parts of your code.\n", 1091 | "\n", 1092 | "Tuples may seem confusing at first, but with time you will come to appreciate them." 1093 | ] 1094 | }, 1095 | { 1096 | "cell_type": "code", 1097 | "execution_count": 69, 1098 | "metadata": { 1099 | "collapsed": false 1100 | }, 1101 | "outputs": [ 1102 | { 1103 | "data": { 1104 | "text/plain": [ 1105 | "tuple" 1106 | ] 1107 | }, 1108 | "execution_count": 69, 1109 | "metadata": {}, 1110 | "output_type": "execute_result" 1111 | } 1112 | ], 1113 | "source": [ 1114 | "# tuples are created with parentheses, or just commas\n", 1115 | "a = ('Ryan', 33, True)\n", 1116 | "b = 'Takaya', 25, False\n", 1117 | "type(b)" 1118 | ] 1119 | }, 1120 | { 1121 | "cell_type": "code", 1122 | "execution_count": 70, 1123 | "metadata": { 1124 | "collapsed": false 1125 | }, 1126 | "outputs": [ 1127 | { 1128 | "name": "stdout", 1129 | "output_type": "stream", 1130 | "text": [ 1131 | "33\n" 1132 | ] 1133 | } 1134 | ], 1135 | "source": [ 1136 | "# can be indexed like arrays\n", 1137 | "print(a[1]) # not the first element!" 1138 | ] 1139 | }, 1140 | { 1141 | "cell_type": "code", 1142 | "execution_count": 71, 1143 | "metadata": { 1144 | "collapsed": true 1145 | }, 1146 | "outputs": [], 1147 | "source": [ 1148 | "# and they can be unpacked\n", 1149 | "name, age, status = a" 1150 | ] 1151 | }, 1152 | { 1153 | "cell_type": "markdown", 1154 | "metadata": {}, 1155 | "source": [ 1156 | "## Dictionaries ##\n", 1157 | "\n", 1158 | "This is an extremely useful data structure. It maps __keys__ to __values__.\n", 1159 | "\n", 1160 | "Dictionaries are unordered!" 1161 | ] 1162 | }, 1163 | { 1164 | "cell_type": "code", 1165 | "execution_count": 76, 1166 | "metadata": { 1167 | "collapsed": false 1168 | }, 1169 | "outputs": [ 1170 | { 1171 | "data": { 1172 | "text/plain": [ 1173 | "{'age': 25, 'name': 'Takaya'}" 1174 | ] 1175 | }, 1176 | "execution_count": 76, 1177 | "metadata": {}, 1178 | "output_type": "execute_result" 1179 | } 1180 | ], 1181 | "source": [ 1182 | "# different ways to create dictionaries\n", 1183 | "d = {'name': 'Ryan', 'age': 33}\n", 1184 | "e = dict(name='Takaya', age=25)\n", 1185 | "e" 1186 | ] 1187 | }, 1188 | { 1189 | "cell_type": "code", 1190 | "execution_count": 82, 1191 | "metadata": { 1192 | "collapsed": false 1193 | }, 1194 | "outputs": [ 1195 | { 1196 | "data": { 1197 | "text/plain": [ 1198 | "'Ryan'" 1199 | ] 1200 | }, 1201 | "execution_count": 82, 1202 | "metadata": {}, 1203 | "output_type": "execute_result" 1204 | } 1205 | ], 1206 | "source": [ 1207 | "# access a value\n", 1208 | "d['name']" 1209 | ] 1210 | }, 1211 | { 1212 | "cell_type": "markdown", 1213 | "metadata": {}, 1214 | "source": [ 1215 | "Square brackets ``[...]`` are python for \"get item\" in many different contexts." 1216 | ] 1217 | }, 1218 | { 1219 | "cell_type": "code", 1220 | "execution_count": 79, 1221 | "metadata": { 1222 | "collapsed": false 1223 | }, 1224 | "outputs": [ 1225 | { 1226 | "name": "stdout", 1227 | "output_type": "stream", 1228 | "text": [ 1229 | "True\n", 1230 | "False\n" 1231 | ] 1232 | } 1233 | ], 1234 | "source": [ 1235 | "# test for the presence of a key\n", 1236 | "print('age' in d)\n", 1237 | "print('height' in e)" 1238 | ] 1239 | }, 1240 | { 1241 | "cell_type": "code", 1242 | "execution_count": 83, 1243 | "metadata": { 1244 | "collapsed": false 1245 | }, 1246 | "outputs": [ 1247 | { 1248 | "ename": "KeyError", 1249 | "evalue": "'height'", 1250 | "output_type": "error", 1251 | "traceback": [ 1252 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 1253 | "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", 1254 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# try to access a non-existant key\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0md\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'height'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 1255 | "\u001b[0;31mKeyError\u001b[0m: 'height'" 1256 | ] 1257 | } 1258 | ], 1259 | "source": [ 1260 | "# try to access a non-existant key\n", 1261 | "d['height']" 1262 | ] 1263 | }, 1264 | { 1265 | "cell_type": "code", 1266 | "execution_count": 84, 1267 | "metadata": { 1268 | "collapsed": false 1269 | }, 1270 | "outputs": [ 1271 | { 1272 | "data": { 1273 | "text/plain": [ 1274 | "{'age': 33, 'height': (5, 11), 'name': 'Ryan'}" 1275 | ] 1276 | }, 1277 | "execution_count": 84, 1278 | "metadata": {}, 1279 | "output_type": "execute_result" 1280 | } 1281 | ], 1282 | "source": [ 1283 | "# add a new key\n", 1284 | "d['height'] = (5,11) # a tuple\n", 1285 | "d" 1286 | ] 1287 | }, 1288 | { 1289 | "cell_type": "code", 1290 | "execution_count": 86, 1291 | "metadata": { 1292 | "collapsed": false 1293 | }, 1294 | "outputs": [ 1295 | { 1296 | "data": { 1297 | "text/plain": [ 1298 | "{99: 'ninety nine', 'age': 33, 'name': 'Ryan', 'height': (5, 11)}" 1299 | ] 1300 | }, 1301 | "execution_count": 86, 1302 | "metadata": {}, 1303 | "output_type": "execute_result" 1304 | } 1305 | ], 1306 | "source": [ 1307 | "# keys don't have to be strings\n", 1308 | "d[99] = 'ninety nine'\n", 1309 | "d" 1310 | ] 1311 | }, 1312 | { 1313 | "cell_type": "code", 1314 | "execution_count": 87, 1315 | "metadata": { 1316 | "collapsed": false 1317 | }, 1318 | "outputs": [ 1319 | { 1320 | "name": "stdout", 1321 | "output_type": "stream", 1322 | "text": [ 1323 | "99 ninety nine\n", 1324 | "age 33\n", 1325 | "name Ryan\n", 1326 | "height (5, 11)\n" 1327 | ] 1328 | } 1329 | ], 1330 | "source": [ 1331 | "# iterate over keys\n", 1332 | "for k in d:\n", 1333 | " print(k, d[k])" 1334 | ] 1335 | }, 1336 | { 1337 | "cell_type": "code", 1338 | "execution_count": 88, 1339 | "metadata": { 1340 | "collapsed": false 1341 | }, 1342 | "outputs": [ 1343 | { 1344 | "name": "stdout", 1345 | "output_type": "stream", 1346 | "text": [ 1347 | "99 ninety nine\n", 1348 | "age 33\n", 1349 | "name Ryan\n", 1350 | "height (5, 11)\n" 1351 | ] 1352 | } 1353 | ], 1354 | "source": [ 1355 | "# better way\n", 1356 | "### python 2\n", 1357 | "### for key, val in d.iteritems()\n", 1358 | "for key, val in d.items():\n", 1359 | " print(key, val)" 1360 | ] 1361 | }, 1362 | { 1363 | "cell_type": "markdown", 1364 | "metadata": {}, 1365 | "source": [ 1366 | "## Functions ##\n", 1367 | "\n", 1368 | "Functions are a central part of advanced python programming. You should try to write and use your own functions as often as possible." 1369 | ] 1370 | }, 1371 | { 1372 | "cell_type": "code", 1373 | "execution_count": 121, 1374 | "metadata": { 1375 | "collapsed": false 1376 | }, 1377 | "outputs": [], 1378 | "source": [ 1379 | "# define a function\n", 1380 | "def say_hello():\n", 1381 | " \"\"\"Return the word hello.\"\"\"\n", 1382 | " return 'Hello'" 1383 | ] 1384 | }, 1385 | { 1386 | "cell_type": "code", 1387 | "execution_count": 122, 1388 | "metadata": { 1389 | "collapsed": false 1390 | }, 1391 | "outputs": [ 1392 | { 1393 | "data": { 1394 | "text/plain": [ 1395 | "function" 1396 | ] 1397 | }, 1398 | "execution_count": 122, 1399 | "metadata": {}, 1400 | "output_type": "execute_result" 1401 | } 1402 | ], 1403 | "source": [ 1404 | "# functions are also objects\n", 1405 | "type(say_hello)" 1406 | ] 1407 | }, 1408 | { 1409 | "cell_type": "code", 1410 | "execution_count": 124, 1411 | "metadata": { 1412 | "collapsed": false 1413 | }, 1414 | "outputs": [], 1415 | "source": [ 1416 | "# this doesnt call\n", 1417 | "say_hello?" 1418 | ] 1419 | }, 1420 | { 1421 | "cell_type": "code", 1422 | "execution_count": 119, 1423 | "metadata": { 1424 | "collapsed": false 1425 | }, 1426 | "outputs": [ 1427 | { 1428 | "data": { 1429 | "text/plain": [ 1430 | "'Hello'" 1431 | ] 1432 | }, 1433 | "execution_count": 119, 1434 | "metadata": {}, 1435 | "output_type": "execute_result" 1436 | } 1437 | ], 1438 | "source": [ 1439 | "# this does\n", 1440 | "say_hello()" 1441 | ] 1442 | }, 1443 | { 1444 | "cell_type": "code", 1445 | "execution_count": 113, 1446 | "metadata": { 1447 | "collapsed": false 1448 | }, 1449 | "outputs": [ 1450 | { 1451 | "data": { 1452 | "text/plain": [ 1453 | "'Hello'" 1454 | ] 1455 | }, 1456 | "execution_count": 113, 1457 | "metadata": {}, 1458 | "output_type": "execute_result" 1459 | } 1460 | ], 1461 | "source": [ 1462 | "# assign the result to something\n", 1463 | "res = say_hello()\n", 1464 | "res" 1465 | ] 1466 | }, 1467 | { 1468 | "cell_type": "code", 1469 | "execution_count": 125, 1470 | "metadata": { 1471 | "collapsed": true 1472 | }, 1473 | "outputs": [], 1474 | "source": [ 1475 | "# take some arguments\n", 1476 | "def say_hello_to(name):\n", 1477 | " \"\"\"Return a greeting to `name`\"\"\"\n", 1478 | " return 'Hello ' + name" 1479 | ] 1480 | }, 1481 | { 1482 | "cell_type": "code", 1483 | "execution_count": 126, 1484 | "metadata": { 1485 | "collapsed": false 1486 | }, 1487 | "outputs": [ 1488 | { 1489 | "data": { 1490 | "text/plain": [ 1491 | "'Hello World'" 1492 | ] 1493 | }, 1494 | "execution_count": 126, 1495 | "metadata": {}, 1496 | "output_type": "execute_result" 1497 | } 1498 | ], 1499 | "source": [ 1500 | "# intended usage\n", 1501 | "say_hello_to('World')" 1502 | ] 1503 | }, 1504 | { 1505 | "cell_type": "code", 1506 | "execution_count": 127, 1507 | "metadata": { 1508 | "collapsed": false 1509 | }, 1510 | "outputs": [ 1511 | { 1512 | "ename": "TypeError", 1513 | "evalue": "Can't convert 'int' object to str implicitly", 1514 | "output_type": "error", 1515 | "traceback": [ 1516 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 1517 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 1518 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msay_hello_to\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", 1519 | "\u001b[0;32m\u001b[0m in \u001b[0;36msay_hello_to\u001b[0;34m(name)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0msay_hello_to\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\"\"\"Return a greeting to `name`\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0;34m'Hello '\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 1520 | "\u001b[0;31mTypeError\u001b[0m: Can't convert 'int' object to str implicitly" 1521 | ] 1522 | } 1523 | ], 1524 | "source": [ 1525 | "say_hello_to(10)" 1526 | ] 1527 | }, 1528 | { 1529 | "cell_type": "code", 1530 | "execution_count": 131, 1531 | "metadata": { 1532 | "collapsed": true 1533 | }, 1534 | "outputs": [], 1535 | "source": [ 1536 | "# redefine the function\n", 1537 | "def say_hello_to(name):\n", 1538 | " \"\"\"Return a greeting to `name`\"\"\"\n", 1539 | " return 'Hello ' + str(name)" 1540 | ] 1541 | }, 1542 | { 1543 | "cell_type": "code", 1544 | "execution_count": 132, 1545 | "metadata": { 1546 | "collapsed": false 1547 | }, 1548 | "outputs": [ 1549 | { 1550 | "data": { 1551 | "text/plain": [ 1552 | "'Hello 10'" 1553 | ] 1554 | }, 1555 | "execution_count": 132, 1556 | "metadata": {}, 1557 | "output_type": "execute_result" 1558 | } 1559 | ], 1560 | "source": [ 1561 | "say_hello_to(10)" 1562 | ] 1563 | }, 1564 | { 1565 | "cell_type": "code", 1566 | "execution_count": 129, 1567 | "metadata": { 1568 | "collapsed": false 1569 | }, 1570 | "outputs": [], 1571 | "source": [ 1572 | "# take an optional keyword argument\n", 1573 | "def say_hello_or_hola(name, spanish=False):\n", 1574 | " \"\"\"Say hello in multiple languages.\"\"\"\n", 1575 | " if spanish:\n", 1576 | " greeting = 'Hola '\n", 1577 | " else:\n", 1578 | " greeting = 'Hello '\n", 1579 | " return greeting + name" 1580 | ] 1581 | }, 1582 | { 1583 | "cell_type": "code", 1584 | "execution_count": 130, 1585 | "metadata": { 1586 | "collapsed": false 1587 | }, 1588 | "outputs": [ 1589 | { 1590 | "name": "stdout", 1591 | "output_type": "stream", 1592 | "text": [ 1593 | "Hello Ryan\n", 1594 | "Hola Juan\n" 1595 | ] 1596 | } 1597 | ], 1598 | "source": [ 1599 | "print(say_hello_or_hola('Ryan'))\n", 1600 | "print(say_hello_or_hola('Juan', spanish=True))\n" 1601 | ] 1602 | }, 1603 | { 1604 | "cell_type": "code", 1605 | "execution_count": 136, 1606 | "metadata": { 1607 | "collapsed": true 1608 | }, 1609 | "outputs": [], 1610 | "source": [ 1611 | "# flexible number of arguments\n", 1612 | "def say_hello_to_everyone(*args):\n", 1613 | " return ['hello ' + str(a) for a in args]" 1614 | ] 1615 | }, 1616 | { 1617 | "cell_type": "code", 1618 | "execution_count": 137, 1619 | "metadata": { 1620 | "collapsed": false 1621 | }, 1622 | "outputs": [ 1623 | { 1624 | "data": { 1625 | "text/plain": [ 1626 | "['hello Ryan', 'hello Juan', 'hello Takaya']" 1627 | ] 1628 | }, 1629 | "execution_count": 137, 1630 | "metadata": {}, 1631 | "output_type": "execute_result" 1632 | } 1633 | ], 1634 | "source": [ 1635 | "say_hello_to_everyone('Ryan', 'Juan', 'Takaya')" 1636 | ] 1637 | }, 1638 | { 1639 | "cell_type": "markdown", 1640 | "metadata": {}, 1641 | "source": [ 1642 | "We could spend the rest of the day talking about functions, but we have to move on." 1643 | ] 1644 | }, 1645 | { 1646 | "cell_type": "markdown", 1647 | "metadata": {}, 1648 | "source": [ 1649 | "# Individual Exercises #" 1650 | ] 1651 | }, 1652 | { 1653 | "cell_type": "markdown", 1654 | "metadata": {}, 1655 | "source": [ 1656 | "## Fibonacci Sequence ##\n", 1657 | "\n", 1658 | "The Fibonacci sequence is the 1,1,2,3,5,8..., the sum of each number with the preceding one. Write a function to compute the Fibonacci sequence of length n. (Hint, use some list methods.)" 1659 | ] 1660 | }, 1661 | { 1662 | "cell_type": "code", 1663 | "execution_count": 142, 1664 | "metadata": { 1665 | "collapsed": true 1666 | }, 1667 | "outputs": [], 1668 | "source": [ 1669 | "def fib(n):\n", 1670 | " l = [1,1]\n", 1671 | " for i in range(n-2):\n", 1672 | " l.append(l[-1] + l[-2])\n", 1673 | " return l" 1674 | ] 1675 | }, 1676 | { 1677 | "cell_type": "code", 1678 | "execution_count": 145, 1679 | "metadata": { 1680 | "collapsed": false 1681 | }, 1682 | "outputs": [ 1683 | { 1684 | "data": { 1685 | "text/plain": [ 1686 | "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]" 1687 | ] 1688 | }, 1689 | "execution_count": 145, 1690 | "metadata": {}, 1691 | "output_type": "execute_result" 1692 | } 1693 | ], 1694 | "source": [ 1695 | "fib(10)" 1696 | ] 1697 | }, 1698 | { 1699 | "cell_type": "markdown", 1700 | "metadata": {}, 1701 | "source": [ 1702 | "## Add and Multiply Lists Item by Item ##\n", 1703 | "\n", 1704 | "Write functions to add and multiply each item in a list." 1705 | ] 1706 | }, 1707 | { 1708 | "cell_type": "code", 1709 | "execution_count": 146, 1710 | "metadata": { 1711 | "collapsed": true 1712 | }, 1713 | "outputs": [], 1714 | "source": [ 1715 | "def add(x,y):\n", 1716 | " return [a + b for a,b in zip(x,y)]\n", 1717 | "\n", 1718 | "def multiply(x,y):\n", 1719 | " return [a * b for a,b in zip(x,y)]" 1720 | ] 1721 | }, 1722 | { 1723 | "cell_type": "code", 1724 | "execution_count": 147, 1725 | "metadata": { 1726 | "collapsed": false 1727 | }, 1728 | "outputs": [ 1729 | { 1730 | "data": { 1731 | "text/plain": [ 1732 | "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]" 1733 | ] 1734 | }, 1735 | "execution_count": 147, 1736 | "metadata": {}, 1737 | "output_type": "execute_result" 1738 | } 1739 | ], 1740 | "source": [ 1741 | "add(range(10), range(10))" 1742 | ] 1743 | }, 1744 | { 1745 | "cell_type": "code", 1746 | "execution_count": 148, 1747 | "metadata": { 1748 | "collapsed": false 1749 | }, 1750 | "outputs": [ 1751 | { 1752 | "data": { 1753 | "text/plain": [ 1754 | "[0, 3, 4, 15, 12]" 1755 | ] 1756 | }, 1757 | "execution_count": 148, 1758 | "metadata": {}, 1759 | "output_type": "execute_result" 1760 | } 1761 | ], 1762 | "source": [ 1763 | "multiply(range(5), [9,3,2,5,3])" 1764 | ] 1765 | }, 1766 | { 1767 | "cell_type": "code", 1768 | "execution_count": 150, 1769 | "metadata": { 1770 | "collapsed": false 1771 | }, 1772 | "outputs": [ 1773 | { 1774 | "name": "stdout", 1775 | "output_type": "stream", 1776 | "text": [ 1777 | "100 loops, best of 3: 12.7 ms per loop\n" 1778 | ] 1779 | } 1780 | ], 1781 | "source": [ 1782 | "N = 100000\n", 1783 | "%timeit multiply(range(N), range(N))" 1784 | ] 1785 | }, 1786 | { 1787 | "cell_type": "code", 1788 | "execution_count": 151, 1789 | "metadata": { 1790 | "collapsed": false 1791 | }, 1792 | "outputs": [ 1793 | { 1794 | "name": "stdout", 1795 | "output_type": "stream", 1796 | "text": [ 1797 | "The slowest run took 7.60 times longer than the fastest. This could mean that an intermediate result is being cached \n", 1798 | "10000 loops, best of 3: 173 µs per loop\n" 1799 | ] 1800 | } 1801 | ], 1802 | "source": [ 1803 | "import numpy as np\n", 1804 | "%timeit np.arange(N) * np.arange(N)" 1805 | ] 1806 | }, 1807 | { 1808 | "cell_type": "markdown", 1809 | "metadata": {}, 1810 | "source": [ 1811 | "On to numpy" 1812 | ] 1813 | }, 1814 | { 1815 | "cell_type": "code", 1816 | "execution_count": null, 1817 | "metadata": { 1818 | "collapsed": true 1819 | }, 1820 | "outputs": [], 1821 | "source": [] 1822 | } 1823 | ], 1824 | "metadata": { 1825 | "kernelspec": { 1826 | "display_name": "Python 3", 1827 | "language": "python", 1828 | "name": "python3" 1829 | }, 1830 | "language_info": { 1831 | "codemirror_mode": { 1832 | "name": "ipython", 1833 | "version": 3 1834 | }, 1835 | "file_extension": ".py", 1836 | "mimetype": "text/x-python", 1837 | "name": "python", 1838 | "nbconvert_exporter": "python", 1839 | "pygments_lexer": "ipython3", 1840 | "version": "3.4.3" 1841 | } 1842 | }, 1843 | "nbformat": 4, 1844 | "nbformat_minor": 0 1845 | } 1846 | --------------------------------------------------------------------------------