diff --git a/content/01-python/w1-python-datatypes.ipynb b/content/01-python/w1-python-datatypes.ipynb index 666c9709..55d10dde 100644 --- a/content/01-python/w1-python-datatypes.ipynb +++ b/content/01-python/w1-python-datatypes.ipynb @@ -4,8 +4,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Basic Python Datatypes\n", - "\n", + "# Basic Python Datatypes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "Python is a dynamically typed language -- this means that you don't\n", "need to specify ahead of time what kind of data you are going to store\n", "in a variable. Nevertheless, there are some core datatypes that we\n", @@ -13,14 +18,26 @@ "\n", "The first set of datatypes are similar to those found in other\n", "languages (like C/C++ and Fortran): floating point numbers, integers,\n", - "and strings.\n", - "\n", + "and strings." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```{tip}\n", "Floating point is essential for computational science. A great\n", "introduction to floating point and its limitations is: [What every\n", "computer scientist should know about floating-point\n", "arithmetic](http://dl.acm.org/citation.cfm?id=103163) by\n", "D. Goldberg.\n", - "\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "The next set of datatypes are containers. In python, unlike some\n", "languages, these are built into the language and make it very easy to\n", "do complex operations. We'll look at these later.\n", @@ -94,9 +111,10 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Note: integer division is one place where python 2 and python 3 different\n", - " \n", - "In python 3.x, dividing 2 integers results in a float. In python 2.x, dividing 2 integers results in an integer. The latter is consistent with many strongly-typed programming languages (like Fortran or C), since the data-type of the result is the same as the inputs, but the former is more inline with our expectations" + "```{note}\n", + "Integer division is one place where python and other programming languages differ.\n", + "In python, dividing 2 integers results in a float. In C/C++/Fortran, dividing 2 integers results in an integer, so `1/2 = 0`.\n", + "```" ] }, { @@ -385,7 +403,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Note in languages like Fortran and C, you specify the amount of memory an integer can take (usually 2 or 4 bytes). This puts a restriction on the largest size integer that can be represented. Python will adapt the size of the integer so you don't *overflow*" + "```{note}\n", + "In languages like Fortran and C, you specify the amount of memory an integer can take (usually 2 or 4 bytes). This puts a restriction on the largest size integer that can be represented. Python will adapt the size of the integer so you don't *overflow*\n", + "```" ] }, { @@ -488,8 +508,6 @@ "* not every real number will have an exact representation in floating point\n", "* there is a finite precision to numbers -- below this we lose track of differences (this is usually called *roundoff* error)\n", "\n", - "On our course website, I posted a link to a paper, _What every computer scientist should know about floating-point arithmetic_ -- this is a great reference on understanding how a computer stores numbers.\n", - "\n", "Consider the following expression, for example:" ] }, @@ -856,95 +874,6 @@ "help(math.sin)" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## complex numbers" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "python uses '`j`' to denote the imaginary unit" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": { - "tags": [] - }, - "outputs": [ - { - "data": { - "text/plain": [ - "(1+2j)" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "1.0 + 2j" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": { - "tags": [] - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(3+3j)\n", - "(-2+3j)\n" - ] - } - ], - "source": [ - "a = 1j\n", - "b = 3.0 + 2.0j\n", - "print(a + b)\n", - "print(a * b)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "we can use `abs()` to get the magnitude and separately get the real or imaginary parts " - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "tags": [] - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "magnitude: 3.605551275463989\n", - "real part: 0.0\n", - "imag part: 1.0\n" - ] - } - ], - "source": [ - "print(\"magnitude: \", abs(b))\n", - "print(\"real part: \", a.real)\n", - "print(\"imag part: \", a.imag)" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -1340,22 +1269,6 @@ "type(a)" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "As usual, ask for help to learn more:" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": {}, - "outputs": [], - "source": [ - "#help(str)" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -1437,7 +1350,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.6" + "version": "3.13.1" } }, "nbformat": 4, diff --git a/content/01-python/w2-python-advanced-datatypes.ipynb b/content/01-python/w2-python-advanced-datatypes.ipynb index b6f674cf..352c4d1d 100644 --- a/content/01-python/w2-python-advanced-datatypes.ipynb +++ b/content/01-python/w2-python-advanced-datatypes.ipynb @@ -719,7 +719,7 @@ "source": [ "````{admonition} Quick Exercise\n", "\n", - "Create a dictionary where the keys are the string names of the numbers zero to nine and the values are their numeric representation (0, 1, ... , 9)\n", + "Create a dictionary where the keys are the string names of the numbers zero to nine and the values are their numeric representation (0, 1, ... , 5)\n", "\n", "````" ] @@ -1033,7 +1033,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.1" + "version": "3.13.1" } }, "nbformat": 4, diff --git a/content/_config.yml b/content/_config.yml index 2cb67b65..2c0567e0 100644 --- a/content/_config.yml +++ b/content/_config.yml @@ -30,7 +30,7 @@ html: use_repository_button: true extra_footer: |
- © 2023-2024; CC-BY-NC-SA 4.0 + © 2023-2025; CC-BY-NC-SA 4.0
sphinx: