From 75a3fc497e467af97b436e0a44dae78705c5bcc8 Mon Sep 17 00:00:00 2001 From: Hans Fangohr Date: Thu, 15 Dec 2022 16:50:51 +0000 Subject: [PATCH 1/7] reviewed python basics --- getting-started/python-basics.ipynb | 443 ++++++++++++++++------------ 1 file changed, 254 insertions(+), 189 deletions(-) diff --git a/getting-started/python-basics.ipynb b/getting-started/python-basics.ipynb index c40baf0..e275c28 100644 --- a/getting-started/python-basics.ipynb +++ b/getting-started/python-basics.ipynb @@ -6,11 +6,16 @@ "source": [ "# Python basics\n", "\n", - "The purpose of this tutorial is to introduce some basic Python syntax, which can help understand ubermag simulations.\n", + "The purpose of this tutorial is to introduce some basic Python syntax, which can help understand Ubermag simulations.\n", "\n", - "## Variables\n", - "\n", - "Python is a dynamic language. That means that no type is associated to the variable at the definition and can be changed at any point by assigning a value of different type." + "## Variables" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Variables (such as the object with name `a` in the examples below) are created through assignment of a value. We can check the type of the variable using the `type()` function:" ] }, { @@ -51,7 +56,7 @@ } ], "source": [ - "a = 3.14 # decimal point present\n", + "a = 3.14 # decimal point present makes this a float type\n", "type(a)" ] }, @@ -72,7 +77,7 @@ } ], "source": [ - "a = 'Python'\n", + "a = 'Python' # single or double quotes define a string\n", "type(a)" ] }, @@ -122,7 +127,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#### Large/small values, e.g. $a = 2.1 \\times 10^{6}$" + "Large/small values, e.g. $a = 2.1 \\times 10^{6}$" ] }, { @@ -150,7 +155,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Basic operations\n", + "## Basic arithmetic operations\n", "\n", "#### 1. Addition $c = a + b$" ] @@ -170,7 +175,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "To output the value:" + "To inspect an object, we can just type its name: " ] }, { @@ -190,14 +195,14 @@ } ], "source": [ - "c # outputs the variable value" + "c # returns the variable value" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Similarly, `print()` function can be used." + "The `print()` function can be used to send the to the standard output (typically the display):" ] }, { @@ -302,7 +307,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In Python 3, if we divide two `int` variables, we are going to get `float`;" + "In Python 3, if we divide two `int` variables, we are going to get `float`:" ] }, { @@ -391,7 +396,7 @@ { "data": { "text/plain": [ - "2.65358979335273e-06" + "2.6535897933527304e-06" ] }, "execution_count": 16, @@ -400,7 +405,7 @@ } ], "source": [ - "theta = 3.14159 # theta = pi\n", + "theta = 3.14159 # theta is approximately pi\n", "math.sin(theta)" ] }, @@ -489,7 +494,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Lists and tuples\n", + "## Sequences: lists and tuples\n", "\n", "A collection of values can be represented using lists and tuples." ] @@ -608,7 +613,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Alternatively $-1$ can be used as an index to get the last element" + "Alternatively `-1` can be used as an index to get the last element" ] }, { @@ -635,7 +640,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#### Length (the number of elements)" + "#### Length (the number of elements in a sequence)" ] }, { @@ -701,7 +706,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "Input \u001b[0;32mIn [29]\u001b[0m, in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0m b[\u001b[38;5;241m2\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m3\u001b[39m\n", + "Cell \u001b[0;32mIn[29], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m b[\u001b[38;5;241m2\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m3\u001b[39m\n", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } @@ -797,13 +802,148 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Dictionaries" + "## Sequences: numpy arrays\n", + "\n", + "Another sequence type that is used a lot in computational work is the `array` type from the `numpy` package. We can create a numpy array from other sequences, such as a list:" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np # by convention `np` is used as the alias for numpy" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 5, 2, 10, 100])" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c = np.array([5, 2, 10, 100])\n", + "c" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "numpy.ndarray" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(c)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The `array` data type has been designed for numeric work. It is fast in execution and allows to carry out the same operation on all elements of the array at the same time:" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 15, 6, 30, 300])" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "3*c" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 2.23606798, 1.41421356, 3.16227766, 10. ])" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.sqrt(c)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "117" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c.sum()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The Ubermag modules, such as `discretisedfield` often return numpy arrays." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Dictionaries\n", + "\n", + "Dictionaries map keys (such as `region1`) to values (such as `1e-12`):" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, "outputs": [ { "data": { @@ -811,7 +951,7 @@ "{'region1': 1e-12, 'region797': 5e-11}" ] }, - "execution_count": 33, + "execution_count": 39, "metadata": {}, "output_type": "execute_result" } @@ -825,12 +965,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Indexing" + "Indexing a dictionary by " ] }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 40, "metadata": {}, "outputs": [ { @@ -839,7 +979,7 @@ "1e-12" ] }, - "execution_count": 34, + "execution_count": 40, "metadata": {}, "output_type": "execute_result" } @@ -852,14 +992,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Selection\n", + "## Conditional execution\n", "\n", - "All lines belonging to a certain branch must be indented." + "All lines belonging to one execution branch must be indented." ] }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 41, "metadata": {}, "outputs": [ { @@ -875,7 +1015,7 @@ "6" ] }, - "execution_count": 35, + "execution_count": 41, "metadata": {}, "output_type": "execute_result" } @@ -885,7 +1025,7 @@ "b = 4\n", "\n", "if a == 5 and b < 10:\n", - " # indented lines!\n", + " # indented lines\n", " print(\"I'm in!\") # single and double quotes\n", " a += 1 # a = a + 1\n", " \n", @@ -894,7 +1034,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 42, "metadata": {}, "outputs": [ { @@ -923,7 +1063,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 43, "metadata": {}, "outputs": [ { @@ -944,7 +1084,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 44, "metadata": {}, "outputs": [ { @@ -966,7 +1106,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 45, "metadata": {}, "outputs": [ { @@ -994,7 +1134,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 46, "metadata": {}, "outputs": [ { @@ -1003,7 +1143,7 @@ "10" ] }, - "execution_count": 40, + "execution_count": 46, "metadata": {}, "output_type": "execute_result" } @@ -1011,14 +1151,14 @@ "source": [ "def area(a, b):\n", " # indented\n", - " return a*b # return should not be confused with print!\n", + " return a * b \n", "\n", "area(5, 2)" ] }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 47, "metadata": {}, "outputs": [ { @@ -1027,7 +1167,7 @@ "6" ] }, - "execution_count": 41, + "execution_count": 47, "metadata": {}, "output_type": "execute_result" } @@ -1036,7 +1176,7 @@ "def sum_of_elements(a):\n", " s = 0\n", " for i in a:\n", - " s += i\n", + " s = s + i\n", " \n", " return s\n", "\n", @@ -1047,12 +1187,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#### Keyword arguments" + "#### Default values for arguments" ] }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 48, "metadata": {}, "outputs": [ { @@ -1061,7 +1201,7 @@ "6" ] }, - "execution_count": 42, + "execution_count": 48, "metadata": {}, "output_type": "execute_result" } @@ -1075,7 +1215,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 49, "metadata": {}, "outputs": [ { @@ -1084,7 +1224,7 @@ "6" ] }, - "execution_count": 43, + "execution_count": 49, "metadata": {}, "output_type": "execute_result" } @@ -1100,12 +1240,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Imports" + "## Accessing modules through `import`" ] }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 50, "metadata": {}, "outputs": [ { @@ -1114,7 +1254,7 @@ "3.141592653589793" ] }, - "execution_count": 44, + "execution_count": 50, "metadata": {}, "output_type": "execute_result" } @@ -1134,7 +1274,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 51, "metadata": {}, "outputs": [ { @@ -1143,7 +1283,7 @@ "3.141592653589793" ] }, - "execution_count": 45, + "execution_count": 51, "metadata": {}, "output_type": "execute_result" } @@ -1151,7 +1291,7 @@ "source": [ "import numpy as np\n", "\n", - "np.pi # alias is used" + "np.pi" ] }, { @@ -1165,7 +1305,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 52, "metadata": { "tags": [ "raises-exception" @@ -1177,7 +1317,7 @@ "evalue": "invalid syntax (1951381238.py, line 1)", "output_type": "error", "traceback": [ - "\u001b[0;36m Input \u001b[0;32mIn [46]\u001b[0;36m\u001b[0m\n\u001b[0;31m def speed(s, t)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + "\u001b[0;36m Cell \u001b[0;32mIn[52], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m def speed(s, t)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], @@ -1190,12 +1330,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#### 2. No indentation" + "#### 2. Lack of indentation" ] }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 53, "metadata": { "tags": [ "raises-exception" @@ -1207,7 +1347,7 @@ "evalue": "expected an indented block (3863715070.py, line 2)", "output_type": "error", "traceback": [ - "\u001b[0;36m Input \u001b[0;32mIn [47]\u001b[0;36m\u001b[0m\n\u001b[0;31m return s/t\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" + "\u001b[0;36m Cell \u001b[0;32mIn[53], line 2\u001b[0;36m\u001b[0m\n\u001b[0;31m return s/t\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" ] } ], @@ -1220,12 +1360,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#### 3. Mixing types" + "#### 3. Mixing incompatible types" ] }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 54, "metadata": { "tags": [ "raises-exception" @@ -1239,7 +1379,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "Input \u001b[0;32mIn [48]\u001b[0m, in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m a \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m10\u001b[39m\n\u001b[1;32m 2\u001b[0m b \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124ma\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m----> 3\u001b[0m \u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\n", + "Cell \u001b[0;32mIn[54], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m a \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m10\u001b[39m\n\u001b[1;32m 2\u001b[0m b \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124ma\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m----> 3\u001b[0m \u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\n", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" ] } @@ -1259,7 +1399,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 55, "metadata": { "tags": [ "raises-exception" @@ -1273,7 +1413,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "Input \u001b[0;32mIn [49]\u001b[0m, in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmy_var\u001b[49m \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m5\u001b[39m\n", + "Cell \u001b[0;32mIn[55], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmy_var\u001b[49m \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m5\u001b[39m\n", "\u001b[0;31mNameError\u001b[0m: name 'my_var' is not defined" ] } @@ -1291,7 +1431,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 56, "metadata": { "tags": [ "raises-exception" @@ -1305,14 +1445,14 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "Input \u001b[0;32mIn [50]\u001b[0m, in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mscipy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mscp\u001b[39;00m \u001b[38;5;66;03m# typo\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[43mscpy\u001b[49m\u001b[38;5;241m.\u001b[39mfft\u001b[38;5;241m.\u001b[39mfft()\n", + "Cell \u001b[0;32mIn[56], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mscipy\u001b[39;00m \n\u001b[0;32m----> 2\u001b[0m \u001b[43mscpy\u001b[49m\u001b[38;5;241m.\u001b[39mfft\u001b[38;5;241m.\u001b[39mfft()\n", "\u001b[0;31mNameError\u001b[0m: name 'scpy' is not defined" ] } ], "source": [ - "import scipy as scp # typo\n", - "scpy.fft.fft()" + "import scipy \n", + "scpy.fft.fft() # typo: scpy instead of scipy" ] }, { @@ -1326,7 +1466,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 57, "metadata": {}, "outputs": [], "source": [ @@ -1342,7 +1482,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 58, "metadata": {}, "outputs": [ { @@ -1351,7 +1491,7 @@ "'ubermag'" ] }, - "execution_count": 52, + "execution_count": 58, "metadata": {}, "output_type": "execute_result" } @@ -1360,148 +1500,73 @@ "my_object.lower()" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can see all methods and attributes of an object using the `dir()` function:" + ] + }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting help\n", "\n", - "In Jupyter notebook, it is often enough to append a question mark to the function name." + "In Jupyter notebooks, it is often enough to append a question mark to the function name:" ] }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 59, "metadata": { "tags": [ "nbval-ignore-output" ] }, + "outputs": [], + "source": [ + "import math\n", + "math.sqrt?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Alternatively, use the `help()` function to get more information about an object or method:" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "\u001b[0;31mSignature:\u001b[0m\n", - "\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n", - "\u001b[0;34m\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", - "\u001b[0;34m\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", - "\u001b[0;34m\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", - "\u001b[0;34m\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", - "\u001b[0;34m\u001b[0m \u001b[0mkeepdims\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0mno\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", - "\u001b[0;34m\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", - "\u001b[0;34m\u001b[0m \u001b[0mwhere\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0mno\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", - "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mDocstring:\u001b[0m\n", - "Compute the arithmetic mean along the specified axis.\n", - "\n", - "Returns the average of the array elements. The average is taken over\n", - "the flattened array by default, otherwise over the specified axis.\n", - "`float64` intermediate and return values are used for integer inputs.\n", - "\n", - "Parameters\n", - "----------\n", - "a : array_like\n", - " Array containing numbers whose mean is desired. If `a` is not an\n", - " array, a conversion is attempted.\n", - "axis : None or int or tuple of ints, optional\n", - " Axis or axes along which the means are computed. The default is to\n", - " compute the mean of the flattened array.\n", - "\n", - " .. versionadded:: 1.7.0\n", - "\n", - " If this is a tuple of ints, a mean is performed over multiple axes,\n", - " instead of a single axis or all the axes as before.\n", - "dtype : data-type, optional\n", - " Type to use in computing the mean. For integer inputs, the default\n", - " is `float64`; for floating point inputs, it is the same as the\n", - " input dtype.\n", - "out : ndarray, optional\n", - " Alternate output array in which to place the result. The default\n", - " is ``None``; if provided, it must have the same shape as the\n", - " expected output, but the type will be cast if necessary.\n", - " See :ref:`ufuncs-output-type` for more details.\n", - "\n", - "keepdims : bool, optional\n", - " If this is set to True, the axes which are reduced are left\n", - " in the result as dimensions with size one. With this option,\n", - " the result will broadcast correctly against the input array.\n", - "\n", - " If the default value is passed, then `keepdims` will not be\n", - " passed through to the `mean` method of sub-classes of\n", - " `ndarray`, however any non-default value will be. If the\n", - " sub-class' method does not implement `keepdims` any\n", - " exceptions will be raised.\n", - "\n", - "where : array_like of bool, optional\n", - " Elements to include in the mean. See `~numpy.ufunc.reduce` for details.\n", - "\n", - " .. versionadded:: 1.20.0\n", - "\n", - "Returns\n", - "-------\n", - "m : ndarray, see dtype parameter above\n", - " If `out=None`, returns a new array containing the mean values,\n", - " otherwise a reference to the output array is returned.\n", - "\n", - "See Also\n", - "--------\n", - "average : Weighted average\n", - "std, var, nanmean, nanstd, nanvar\n", - "\n", - "Notes\n", - "-----\n", - "The arithmetic mean is the sum of the elements along the axis divided\n", - "by the number of elements.\n", - "\n", - "Note that for floating-point input, the mean is computed using the\n", - "same precision the input has. Depending on the input data, this can\n", - "cause the results to be inaccurate, especially for `float32` (see\n", - "example below). Specifying a higher-precision accumulator using the\n", - "`dtype` keyword can alleviate this issue.\n", - "\n", - "By default, `float16` results are computed using `float32` intermediates\n", - "for extra precision.\n", - "\n", - "Examples\n", - "--------\n", - ">>> a = np.array([[1, 2], [3, 4]])\n", - ">>> np.mean(a)\n", - "2.5\n", - ">>> np.mean(a, axis=0)\n", - "array([2., 3.])\n", - ">>> np.mean(a, axis=1)\n", - "array([1.5, 3.5])\n", - "\n", - "In single precision, `mean` can be inaccurate:\n", - "\n", - ">>> a = np.zeros((2, 512*512), dtype=np.float32)\n", - ">>> a[0, :] = 1.0\n", - ">>> a[1, :] = 0.1\n", - ">>> np.mean(a)\n", - "0.54999924\n", - "\n", - "Computing the mean in float64 is more accurate:\n", - "\n", - ">>> np.mean(a, dtype=np.float64)\n", - "0.55000000074505806 # may vary\n", - "\n", - "Specifying a where argument:\n", - ">>> a = np.array([[5, 9, 13], [14, 10, 12], [11, 15, 19]])\n", - ">>> np.mean(a)\n", - "12.0\n", - ">>> np.mean(a, where=[[True], [False], [False]])\n", - "9.0\n", - "\u001b[0;31mFile:\u001b[0m /opt/miniconda3/envs/ubermagdev/lib/python3.8/site-packages/numpy/core/fromnumeric.py\n", - "\u001b[0;31mType:\u001b[0m function\n" - ] - }, - "metadata": {}, - "output_type": "display_data" + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on built-in function sqrt in module math:\n", + "\n", + "sqrt(x, /)\n", + " Return the square root of x.\n", + "\n" + ] } ], "source": [ - "import numpy as np\n", - "np.mean?" + "help(math.sqrt)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Further reading\n", + "\n", + "- A more detailed [Introduction to Python for scientists and engineers is available](https://github.com/fangohr/introduction-to-python-for-computational-science-and-engineering/blob/master/Readme.md)\n", + "\n", + "- More generic documentation and tutorials can be found on the Python home page at https://www.python.org/" ] } ], @@ -1521,7 +1586,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.12" + "version": "3.9.15" }, "widgets": { "application/vnd.jupyter.widget-state+json": { From cbcc1463b0c0a52255ba8c31522849f2e8519576 Mon Sep 17 00:00:00 2001 From: Hans Fangohr Date: Thu, 15 Dec 2022 20:15:10 +0100 Subject: [PATCH 2/7] Update getting-started/python-basics.ipynb Co-authored-by: Martin Lang <67915889+lang-m@users.noreply.github.com> --- getting-started/python-basics.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/getting-started/python-basics.ipynb b/getting-started/python-basics.ipynb index e275c28..e314b6d 100644 --- a/getting-started/python-basics.ipynb +++ b/getting-started/python-basics.ipynb @@ -804,7 +804,7 @@ "source": [ "## Sequences: numpy arrays\n", "\n", - "Another sequence type that is used a lot in computational work is the `array` type from the `numpy` package. We can create a numpy array from other sequences, such as a list:" + "Another sequence type that is used a lot in computational work is the `ndarray` type (n-dimensional array) from the `numpy` package. We can create a numpy array from other sequences, such as a list:" ] }, { From f0d494dfb96ec23edea1e7b8101a866c50dc7dd4 Mon Sep 17 00:00:00 2001 From: Hans Fangohr Date: Thu, 15 Dec 2022 20:15:43 +0100 Subject: [PATCH 3/7] array -> ndarray Co-authored-by: Martin Lang <67915889+lang-m@users.noreply.github.com> --- getting-started/python-basics.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/getting-started/python-basics.ipynb b/getting-started/python-basics.ipynb index e314b6d..750a6df 100644 --- a/getting-started/python-basics.ipynb +++ b/getting-started/python-basics.ipynb @@ -861,7 +861,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The `array` data type has been designed for numeric work. It is fast in execution and allows to carry out the same operation on all elements of the array at the same time:" + "The `ndarray` data type has been designed for numeric work. It is fast in execution and allows to carry out the same operation on all elements of the array at the same time:" ] }, { From 306ce87b0a64feebfd84979644b9cde4d536af9e Mon Sep 17 00:00:00 2001 From: Hans Fangohr Date: Thu, 15 Dec 2022 20:16:10 +0100 Subject: [PATCH 4/7] Explain np.sqrt Co-authored-by: Martin Lang <67915889+lang-m@users.noreply.github.com> --- getting-started/python-basics.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/getting-started/python-basics.ipynb b/getting-started/python-basics.ipynb index 750a6df..ac98dfb 100644 --- a/getting-started/python-basics.ipynb +++ b/getting-started/python-basics.ipynb @@ -901,7 +901,7 @@ } ], "source": [ - "np.sqrt(c)" + "np.sqrt(c)" # element-wise square root ] }, { From 8c4e458b9be98ce4be112baa674268c63063cdd9 Mon Sep 17 00:00:00 2001 From: Hans Fangohr Date: Thu, 15 Dec 2022 20:16:33 +0100 Subject: [PATCH 5/7] Update getting-started/python-basics.ipynb Co-authored-by: Martin Lang <67915889+lang-m@users.noreply.github.com> --- getting-started/python-basics.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/getting-started/python-basics.ipynb b/getting-started/python-basics.ipynb index ac98dfb..651da4a 100644 --- a/getting-started/python-basics.ipynb +++ b/getting-started/python-basics.ipynb @@ -965,7 +965,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Indexing a dictionary by " + "Accessing an element in a dictionary " ] }, { From 3516f733170fbbf4ed552989884b2ae2930127f1 Mon Sep 17 00:00:00 2001 From: Hans Fangohr Date: Thu, 15 Dec 2022 21:40:47 +0000 Subject: [PATCH 6/7] fix json syntax --- getting-started/python-basics.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/getting-started/python-basics.ipynb b/getting-started/python-basics.ipynb index 651da4a..292efe2 100644 --- a/getting-started/python-basics.ipynb +++ b/getting-started/python-basics.ipynb @@ -901,7 +901,7 @@ } ], "source": [ - "np.sqrt(c)" # element-wise square root + "np.sqrt(c) # element-wise square root" ] }, { From 95ebcd7ea53dfaaeaa7cd8a6eb9dc2f524b3f038 Mon Sep 17 00:00:00 2001 From: Hans Fangohr Date: Thu, 15 Dec 2022 21:44:49 +0000 Subject: [PATCH 7/7] address feedback from @lang-m --- getting-started/python-basics.ipynb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/getting-started/python-basics.ipynb b/getting-started/python-basics.ipynb index 292efe2..6d1c498 100644 --- a/getting-started/python-basics.ipynb +++ b/getting-started/python-basics.ipynb @@ -175,7 +175,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "To inspect an object, we can just type its name: " + "To inspect an object, we can just type its name if it is in the last line of a notebook cell: " ] }, { @@ -195,14 +195,14 @@ } ], "source": [ - "c # returns the variable value" + "c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "The `print()` function can be used to send the to the standard output (typically the display):" + "The `print()` function can be used to send information to the standard output (typically the display):" ] }, { @@ -928,7 +928,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The Ubermag modules, such as `discretisedfield` often return numpy arrays." + "The Ubermag modules, such as `discretisedfield`, often return numpy arrays." ] }, {