Skip to content

Commit

Permalink
some cleaning for the new semester
Browse files Browse the repository at this point in the history
  • Loading branch information
zingale committed Jan 21, 2025
1 parent 5ae2317 commit 79d822c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 120 deletions.
147 changes: 30 additions & 117 deletions content/01-python/w1-python-datatypes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,40 @@
"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",
"need to become familiar with as we use the language.\n",
"\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",
Expand Down Expand Up @@ -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",
"```"
]
},
{
Expand Down Expand Up @@ -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",
"```"
]
},
{
Expand Down Expand Up @@ -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:"
]
},
Expand Down Expand Up @@ -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": {},
Expand Down Expand Up @@ -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": {},
Expand Down Expand Up @@ -1437,7 +1350,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
"version": "3.13.1"
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions content/01-python/w2-python-advanced-datatypes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"````"
]
Expand Down Expand Up @@ -1033,7 +1033,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.1"
"version": "3.13.1"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion content/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ html:
use_repository_button: true
extra_footer: |
<p>
&copy; 2023-2024; CC-BY-NC-SA 4.0
&copy; 2023-2025; CC-BY-NC-SA 4.0
<p>
sphinx:
Expand Down

0 comments on commit 79d822c

Please sign in to comment.