Created
June 24, 2014 01:11
-
-
Save sjpfenninger/0b96957f27e2a61123ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"metadata": {}, | |
"cell_type": "heading", | |
"source": "Data types", | |
"level": 1 | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "heading", | |
"source": "Integers and floating point numbers", | |
"level": 2 | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_int = 1", | |
"prompt_number": 1, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "type(my_int)", | |
"prompt_number": 2, | |
"outputs": [ | |
{ | |
"text": "int", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 2 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_float = 1.5", | |
"prompt_number": 3, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "type(my_float)", | |
"prompt_number": 4, | |
"outputs": [ | |
{ | |
"text": "float", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 4 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Careful: if you divide an `int` by an `int`, an `int` will result (in Python 2 -- in Python 3, you will get a `float`)" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "1 / 2", | |
"prompt_number": 5, | |
"outputs": [ | |
{ | |
"text": "0", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 5 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "If one of the two is a `float`, the result is a `float` too:" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "1.0 / 2", | |
"prompt_number": 6, | |
"outputs": [ | |
{ | |
"text": "0.5", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 6 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "x = 1\ny = 2", | |
"prompt_number": 7, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "You can force once of the two to a `float` like this:" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "float(x) / y", | |
"prompt_number": 8, | |
"outputs": [ | |
{ | |
"text": "0.5", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 8 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "heading", | |
"source": "Strings", | |
"level": 2 | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_string = 'hello'", | |
"prompt_number": 9, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "type(my_string)", | |
"prompt_number": 10, | |
"outputs": [ | |
{ | |
"text": "str", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 10 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Strings are _immutable_, that is, they cannot be changed once created.\n\nTo \"change\" a string, a new string has to be created:" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_string.upper()", | |
"prompt_number": 11, | |
"outputs": [ | |
{ | |
"text": "'HELLO'", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 11 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_string", | |
"prompt_number": 12, | |
"outputs": [ | |
{ | |
"text": "'hello'", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 12 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_string = my_string.upper()", | |
"prompt_number": 13, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_string", | |
"prompt_number": 14, | |
"outputs": [ | |
{ | |
"text": "'HELLO'", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 14 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "heading", | |
"source": "Lists and tuples", | |
"level": 2 | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Lists are ordered containers and can contain objects of any type." | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_list = [1, 2, 3, 4]", | |
"prompt_number": 15, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "another_list = ['tic', 'tac', 'toe']", | |
"prompt_number": 16, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "mixed_list = [1, 'tic', 1.5, my_list]", | |
"prompt_number": 17, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Lists are zero-indexed:" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_list[0] # This is the zero-th (first) element in my_list", | |
"prompt_number": 18, | |
"outputs": [ | |
{ | |
"text": "1", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 18 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "another_list[1]", | |
"prompt_number": 19, | |
"outputs": [ | |
{ | |
"text": "'tac'", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 19 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Lists can be sliced:" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "mixed_list[1:3]", | |
"prompt_number": 20, | |
"outputs": [ | |
{ | |
"text": "['tic', 1.5]", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 20 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "List define useful methods, for example to add an item to the end of the list:" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_list.append(5)", | |
"prompt_number": 21, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_list", | |
"prompt_number": 22, | |
"outputs": [ | |
{ | |
"text": "[1, 2, 3, 4, 5]", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 22 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "__Tuples__ are like lists, except that they cannot be changed once created (i.e. they are immutable).\n\nInstead of using square brackets `[]` to create a list, we use regular brackets `()` to create a tuple." | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_list = [1, 2, 3, 4]\n\nmy_tuple = (1, 2, 3, 4)", | |
"prompt_number": 23, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "We cannot append to a tuple, because it cannot be changed:" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_tuple.append(5)", | |
"prompt_number": 24, | |
"outputs": [ | |
{ | |
"output_type": "pyerr", | |
"ename": "AttributeError", | |
"evalue": "'tuple' object has no attribute 'append'", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-24-f2f40152e20d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmy_tuple\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" | |
] | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "heading", | |
"source": "Dictionaries", | |
"level": 2 | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Dictionaries store key-value pairs. The key can be anything that is immutable (i.e. cannot be changed)." | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_dict = {'x': 0.1, 'y': 1.0, 'z': 0.5}", | |
"prompt_number": 25, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_dict['x']", | |
"prompt_number": 26, | |
"outputs": [ | |
{ | |
"text": "0.1", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 26 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Dictionaries define useful methods, for example to get a list of all keys:" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_dict.keys()", | |
"prompt_number": 27, | |
"outputs": [ | |
{ | |
"text": "['y', 'x', 'z']", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 27 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Tuples can be keys of a dictionary (but not lists, because they are not immutable):" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_other_dict = {(1, 1): 'a', (1, 2): 'b', (2, 1): 'c'}", | |
"prompt_number": 28, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_other_dict[(1, 2)]", | |
"prompt_number": 29, | |
"outputs": [ | |
{ | |
"text": "'b'", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 29 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_other_dict.keys() # The list of keys is a list of tuples", | |
"prompt_number": 30, | |
"outputs": [ | |
{ | |
"text": "[(1, 2), (1, 1), (2, 1)]", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 30 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "heading", | |
"source": "Sets", | |
"level": 2 | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Sets are unordered collections of unique items" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_set = {1, 2, 3}", | |
"prompt_number": 31, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_set", | |
"prompt_number": 32, | |
"outputs": [ | |
{ | |
"text": "{1, 2, 3}", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 32 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_set.add(1)", | |
"prompt_number": 33, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_set", | |
"prompt_number": 34, | |
"outputs": [ | |
{ | |
"text": "{1, 2, 3}", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 34 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Sets define set-theoretic operations like `union` and `intersection`." | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_other_set = {2, 3, 4, 5}\n\nmy_set.union(my_other_set)", | |
"prompt_number": 35, | |
"outputs": [ | |
{ | |
"text": "{1, 2, 3, 4, 5}", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 35 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "heading", | |
"source": "Numpy arrays", | |
"level": 2 | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Numpy arrays behave a bit like lists, but can be n-dimensional, and can do much more than lists (e.g. linear algebra)." | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "shape = [10, 3]", | |
"prompt_number": 36, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "import numpy as np", | |
"prompt_number": 37, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "np.ones(shape)", | |
"prompt_number": 38, | |
"outputs": [ | |
{ | |
"text": "array([[ 1., 1., 1.],\n [ 1., 1., 1.],\n [ 1., 1., 1.],\n [ 1., 1., 1.],\n [ 1., 1., 1.],\n [ 1., 1., 1.],\n [ 1., 1., 1.],\n [ 1., 1., 1.],\n [ 1., 1., 1.],\n [ 1., 1., 1.]])", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 38 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "random_array = np.random.random(shape)", | |
"prompt_number": 39, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "random_array", | |
"prompt_number": 40, | |
"outputs": [ | |
{ | |
"text": "array([[ 0.81558278, 0.28267059, 0.03731748],\n [ 0.84822616, 0.45256917, 0.96159072],\n [ 0.58127966, 0.03889386, 0.04851698],\n [ 0.03606012, 0.14678589, 0.54226015],\n [ 0.93301754, 0.43294688, 0.18199496],\n [ 0.62606184, 0.0745122 , 0.63058669],\n [ 0.02635198, 0.59102882, 0.93651475],\n [ 0.81019223, 0.0833387 , 0.76386045],\n [ 0.26544052, 0.26141225, 0.4512097 ],\n [ 0.70762875, 0.14864612, 0.36470545]])", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 40 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Accessing items in an array works just like in lists, except that more dimensions are possible:" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "random_array[0, 1] # 0th row, 1st column", | |
"prompt_number": 41, | |
"outputs": [ | |
{ | |
"text": "0.28267059224803359", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 41 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "random_array[0, :] # Get 0th row across all columns", | |
"prompt_number": 42, | |
"outputs": [ | |
{ | |
"text": "array([ 0.81558278, 0.28267059, 0.03731748])", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 42 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "heading", | |
"source": "Other data types", | |
"level": 2 | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "See https://docs.python.org/2/library/datatypes.html" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "heading", | |
"source": "Control structures", | |
"level": 1 | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "my_list = ['a', 'b', 'c']", | |
"prompt_number": 43, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "for i in my_list:\n print i", | |
"prompt_number": 46, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "a\nb\nc\n", | |
"stream": "stdout" | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "i # Note that i remains defined after the loop", | |
"prompt_number": 47, | |
"outputs": [ | |
{ | |
"text": "'c'", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 47 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Another way to iterate through the list (this time iterating through the indices and the items at the same time):" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "for index, item in enumerate(my_list):\n print index, ' ', item", | |
"prompt_number": 48, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "0 a\n1 b\n2 c\n", | |
"stream": "stdout" | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Another approach would be with a while loop, but this is more wordy and less idiomatic:" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "i = 0\nwhile i < len(my_list):\n print my_list[i]\n i += 1", | |
"prompt_number": 49, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "a\nb\nc\n", | |
"stream": "stdout" | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "heading", | |
"source": "Speed comparison", | |
"level": 2 | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "long_list = range(100000)", | |
"prompt_number": 50, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "long_list[0:10]", | |
"prompt_number": 51, | |
"outputs": [ | |
{ | |
"text": "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 51 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "%%timeit\n\nfor index, value in enumerate(long_list):\n long_list[index] = long_list[index] + 1", | |
"prompt_number": 52, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "100 loops, best of 3: 18 ms per loop\n", | |
"stream": "stdout" | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "long_array = np.arange(100000)", | |
"prompt_number": 53, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "%%timeit\n\nlong_array1 = long_array + 1", | |
"prompt_number": 54, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "10000 loops, best of 3: 74.5 µs per loop\n", | |
"stream": "stdout" | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Or get 100000 random numbers:" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "%%timeit\n\nrand_array = np.random.random(100000)", | |
"prompt_number": 55, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "1000 loops, best of 3: 1.14 ms per loop\n", | |
"stream": "stdout" | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "`np.dot` for matrix multiplication:" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "np.dot(np.random.random([3, 2]), np.random.random(2))", | |
"prompt_number": 56, | |
"outputs": [ | |
{ | |
"text": "array([ 0.66785091, 0.55662799, 0.29531817])", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 56 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "heading", | |
"source": "Defining functions", | |
"level": 1 | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "def add(a, b):\n c = a + b\n return c", | |
"prompt_number": 57, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "add(10, 20)", | |
"prompt_number": 58, | |
"outputs": [ | |
{ | |
"text": "30", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 58 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "c # c is only defined inside the function", | |
"prompt_number": 59, | |
"outputs": [ | |
{ | |
"output_type": "pyerr", | |
"ename": "NameError", | |
"evalue": "name 'c' is not defined", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-59-64be77f1f9b3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mc\u001b[0m \u001b[0;31m# c is only defined inside the function\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mNameError\u001b[0m: name 'c' is not defined" | |
] | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "A more advanced function example where one argument (`b`) is optional and gets a default value:" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "import random", | |
"prompt_number": 60, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "def add(a, b=None):\n \"\"\"Returns the sum of `a` and `b`.\n \n If `b` is not given, is is set to a random number between 0\n and 10.\n \n \"\"\"\n if not b:\n b = random.randint(0, 10)\n c = a + b\n return c", | |
"prompt_number": 61, | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "add(1)", | |
"prompt_number": 62, | |
"outputs": [ | |
{ | |
"text": "11", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 62 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "add(1, 10)", | |
"prompt_number": 63, | |
"outputs": [ | |
{ | |
"text": "11", | |
"output_type": "pyout", | |
"metadata": {}, | |
"prompt_number": 63 | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "add(b=10) # This does not work because b is optional, but a must be given", | |
"prompt_number": 64, | |
"outputs": [ | |
{ | |
"output_type": "pyerr", | |
"ename": "TypeError", | |
"evalue": "add() takes at least 1 argument (1 given)", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-64-445ffe25f0be>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0madd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# This does not work because b is optional, but a must be given\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mTypeError\u001b[0m: add() takes at least 1 argument (1 given)" | |
] | |
} | |
], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "code", | |
"input": "", | |
"outputs": [], | |
"language": "python", | |
"trusted": true, | |
"collapsed": false | |
} | |
], | |
"metadata": {} | |
} | |
], | |
"metadata": { | |
"name": "", | |
"signature": "sha256:97cb6615c697fdee3d5056b220c51c09fd5faa42aacd1422cdb5efe02aded526" | |
}, | |
"nbformat": 3 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment