diff --git a/02_PythonBasics.ipynb b/02_PythonBasics.ipynb index e6a9c9d..27bfb1f 100644 --- a/02_PythonBasics.ipynb +++ b/02_PythonBasics.ipynb @@ -183,7 +183,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -402,24 +402,11 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "scrolled": true }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Pi is: 3.14\n", - "a, b, c\n", - "The complex number (1+2j) is formed from the real part 1.0 and the imaginary part 2.0.\n", - "Coordinates: 37.24N, -115.81W\n", - "list element 0: 1; list element 3: cat\n", - "1,234,567,890\n" - ] - } - ], + "outputs": [], "source": [ "pi = 3.14159265\n", "list = [1,2,3,'cat',True,3.1415]\n", @@ -447,22 +434,9 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Pi is: 3.14\n", - "a, b, c\n", - "The complex number (1+2j) is formed from the real part 1.0 and the imaginary part 2.0.\n", - "Coordinates: 37.24N, -115.81W\n", - "list element 0: 1; list element 3: cat\n", - "1,234,567,890\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "pi = 3.14159265\n", "list = [1,2,3,'cat',True,3.1415]\n", @@ -1028,7 +1002,7 @@ "for n in range(5):\n", " print(n) \n", "\n", - "print('\\nrange2') b\n", + "print('\\nrange2') \n", "for n in range(5,10):\n", " print(n) \n", "\n", @@ -1190,7 +1164,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Here `A=[]` first allocates an empty list to `A` when the function is created not when it is run. Now whenever you edit `A` it's storded at the same address so is kept for subsequent calls to the function. So as we have pointed `a` and `c` to the same list editing one edits both.\n", + "Here `A=[]` first allocates an empty list to `A` when the function is created not when it is run. Now whenever you edit `A` it's stored at the same address so is kept for subsequent calls to the function. So as we have pointed `a` and `c` to the same list editing one edits both.\n", "\n", "The following would fix this behaviour:" ] @@ -1338,11 +1312,11 @@ "metadata": {}, "outputs": [], "source": [ - "list = [-1,-2,-3,-4,-5,2,3,4,5,6,7,8]\n", - "list.sort()\n", - "print(list)\n", - "list = sorted(list,key=lambda x: abs(x))\n", - "print(list)" + "list1 = [-1,-2,-3,-4,-5,2,3,4,5,6,7,8]\n", + "list1.sort()\n", + "print(list1)\n", + "list1 = sorted(list1,key=lambda x: abs(x))\n", + "print(list1)" ] }, { diff --git a/04_Advanced_Python.ipynb b/04_Advanced_Python.ipynb index 7cf87d9..c2bab28 100644 --- a/04_Advanced_Python.ipynb +++ b/04_Advanced_Python.ipynb @@ -80,7 +80,7 @@ " return 6.67e-11 * self.mass / dist\n", "\n", " \n", - "earth = CelObj(6e24, [0,0,0], [3e4,0,0])\n", + "earth = CelObj1(6e24, [0,0,0], [3e4,0,0])\n", "print(earth.mass)\n", "print(earth.position)\n", "print(earth.velocity)\n", @@ -504,7 +504,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "So far so trivial. This isn't very generic as we had to specify the arguments for the function. We could generalise it with:|" + "So far so trivial. This isn't very generic as we had to specify the arguments for the function. We could generalise it with:" ] }, { @@ -691,13 +691,58 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Another useful decorator would be one to time how long specific functions take.\n", + "Another useful decorator would be one to time how long specific functions take. In numpy there are some built in decorators like `np.vectorize` which modify simple functions to take vector input. My solution for notebook 3 used the explicit version of it on my function `intmono`:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "...\n", + "\n", + "# Vectorise just converts a scalar function to one that accepts vectors\n", + "# Needed for numpy.fromfunction\n", + "tmp = np.vectorize(intmono)\n", + "\n", + "# Creates an array with entries calculated from the function which takes array indexes as input\n", + "M = np.fromfunction(tmp,(6,6))\n", + "\n", + "..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "here we could have used `@np.vectorize` on the definition of `intmono` instead." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "@np.vectorize\n", + "def intmono(n,m):\n", + " \n", + "...\n", "\n", + "# Creates an array with entries calculated from the function which takes array indexes as input\n", + "M = np.fromfunction(intmono,(6,6))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "### Exercise:\n", "Design a decorator to time how long functions take to run and output it as a string.\n", "You will need commands from the builtin module `time`\n", "\n", - "Another is to `cache` results for a function. This just memorises the output for a specific input so if it's called again the function just returns the answer from memory. This can help with performance but is a bit beyond this course, example given in solutions." + "Another is to `cache` results for a function. This just memorises the output for a specific input so if it's called again the function just returns the answer from memory. This can help with performance but is a bit beyond this course, an example given in solutions." ] }, { @@ -735,7 +780,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.0" + "version": "3.7.8" } }, "nbformat": 4,