Skip to content

Commit 214e52c

Browse files
authored
Merge pull request #3 from SECQUOIA/notebooks-1-5b-improvements
Improve notebooks through 5b
2 parents 7ff5767 + 6bfd546 commit 214e52c

File tree

6 files changed

+2478
-547
lines changed

6 files changed

+2478
-547
lines changed

1-Python_Overview/Python_Intro.ipynb

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
" <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n",
1717
" </a>\n",
1818
" <a href=\"https://secquoia.github.io/\">\n",
19-
" <img src=\"https://img.shields.io/badge/\ud83c\udf32\u269b\ufe0f\ud83c\udf10-SECQUOIA-blue\" alt=\"SECQUOIA\"/>\n",
19+
" <img src=\"https://img.shields.io/badge/🌲⚛️🌐-SECQUOIA-blue\" alt=\"SECQUOIA\"/>\n",
2020
" </a>\n",
2121
"</div>"
2222
]
@@ -241,7 +241,7 @@
241241
"## Python Basics\n",
242242
"\n",
243243
"### Dynamic \"typing\"\n",
244-
"In Python, objects are one thing and their <b>pointers</b> (you might want to call them labels or variables) are another. As I told you above in the case of `a=1`, the integer was created somewhere in the available memory and we attached the pointer `a` to it so that we could use it later. Python automatically \"knew\" that we meant `1` to be an integer by convention. That is, the <b>type</b> of `1` was determined to be an integer on the fly. We call this aspect of a language \"dynamic typing\" and it means that the user doesn\u2019t need to explicitly specify the <b>type</b> of an object. This can be a surprise for someone coming from the C-family or Fortran programming languages. \n",
244+
"In Python, objects are one thing and their <b>pointers</b> (you might want to call them labels or variables) are another. As I told you above in the case of `a=1`, the integer was created somewhere in the available memory and we attached the pointer `a` to it so that we could use it later. Python automatically \"knew\" that we meant `1` to be an integer by convention. That is, the <b>type</b> of `1` was determined to be an integer on the fly. We call this aspect of a language \"dynamic typing\" and it means that the user doesn’t need to explicitly specify the <b>type</b> of an object. This can be a surprise for someone coming from the C-family or Fortran programming languages. \n",
245245
"\n",
246246
"### Pointers vs Objects\n",
247247
"In a dynamically typed language, <b>pointers</b> are labels for objects in memory (like `1` above). They can be plucked off one object and placed on another. For example the two consecutive commands,"
@@ -271,7 +271,7 @@
271271
"metadata": {},
272272
"source": [
273273
"### Strong typing\n",
274-
"Even though Python determines object <b>types</b> dynamically when they are created, the object <b>type</b> is fixed. We call this aspect of a programming language <b>strong typing</b>. In practical terms, all that this means is that Python will throw an error if you try and do something with an object that it is incapable of. Python will not automatically convert <i>unrelated</i> types (e.g., `int` to `str`) just to make your code run. Thus, `a` in the example above might be alternately assigned to an integer or a string, but operations on the object that `a` points to must be consistent with that object. The compiler won\u2019t guess your intentions. For example\n"
274+
"Even though Python determines object <b>types</b> dynamically when they are created, the object <b>type</b> is fixed. We call this aspect of a programming language <b>strong typing</b>. In practical terms, all that this means is that Python will throw an error if you try and do something with an object that it is incapable of. Python will not automatically convert <i>unrelated</i> types (e.g., `int` to `str`) just to make your code run. Thus, `a` in the example above might be alternately assigned to an integer or a string, but operations on the object that `a` points to must be consistent with that object. The compiler won’t guess your intentions. For example\n"
275275
]
276276
},
277277
{
@@ -326,7 +326,7 @@
326326
"cell_type": "markdown",
327327
"metadata": {},
328328
"source": [
329-
"throws an error, because string objects can\u2019t be added to integers. Sometimes Python is forgiving, like if you add an integer and float (we'll return to this), but it is best to be explicit. \n",
329+
"throws an error, because string objects can’t be added to integers. Sometimes Python is forgiving, like if you add an integer and float (we'll return to this), but it is best to be explicit. \n",
330330
"\n",
331331
"### Data types\n",
332332
"We've partly covered this but let's be comprehensive. We'll refer to <b>objects</b> as anything that is stored in memory and <b>data</b> as special types of objects that are single values. Data objects are the most elementary of the objects we will work with in Python and more complicated objects like functions and structures will use data objects as inputs. The basic data types that we commonly work with are `int`, `float`, `string`, `complex`, and `boolean`. Python can handle all of these natively."
@@ -476,10 +476,9 @@
476476
"What are <b>functions</b> and <b>operators</b>? I've snuck these concepts in without alerting you, but I think based on the above examples you might be able to guess what they are. Let's give each a proper definition and further examples.\n",
477477
"\n",
478478
"### Operators ###\n",
479-
"<b>Operators</b> are commands that \u201coperate\u201d on objects to their left and right and return a new object or value. This abstract notion is actually extremely intuitive. For example `+` and `/` are arithmetic operators for addition and division of the objects on either side of the operator. `<` is a logical operator, it returns `True` when the object on the right is larger than the object on the left (Note: logical operators can be used on things that aren't obviously numeric). `is` is another logical operator, it returns `True` when the pointers on the left and right point to the same object, and `False` when they don\u2019t. `is` is similar to `==` (\"equals\"), except that `==` can evaluate to `True` if two different objects evaluate to the same value. <b>Note:</b> using `is` for value comparison is a misuse because many equal values are <i>not</i> the same object (e.g., two different lists with the same contents). Use `==` for value equality and reserve `is` for identity checks like `x is None`. Here are some usage examples\n",
479+
"<b>Operators</b> are commands that “operate” on objects to their left and right and return a new object or value. This abstract notion is actually extremely intuitive. For example `+` and `/` are arithmetic operators for addition and division of the objects on either side of the operator. `<` is a logical operator, it returns `True` when the object on the right is larger than the object on the left (Note: logical operators can be used on things that aren't obviously numeric). `is` is another logical operator, it returns `True` when the pointers on the left and right point to the same object, and `False` when they don’t. `is` is similar to `==` (\"equals\"), except that `==` can evaluate to `True` if two different objects evaluate to the same value. <b>Note:</b> using `is` for value comparison is a misuse because many equal values are <i>not</i> the same object (e.g., two different lists with the same contents). Use `==` for value equality and reserve `is` for identity checks like `x is None`. Here are some usage examples\n",
480480
"\n",
481-
"\n",
482-
" "
481+
"Note: See Python operator reference: https://docs.python.org/3/reference/expressions.html#operator-precedence\n"
483482
]
484483
},
485484
{
@@ -531,7 +530,9 @@
531530
"### Functions ###\n",
532531
"In Python, a `function` is a special type of object that takes other objects as input and does something or optionally returns other object(s) as output(s). `functions` are the work-horse of most programs and they are more intuitive to see and use than this abstract definition makes them appear. At their most essential, `functions` allow you to reuse chunks of code for operation on new inputs or with tweaks to the parameters associated with the reused code. \n",
533532
"\n",
534-
"A `function` is called in Python by using its name followed by parentheses (e.g., `print(1)` calls the `print()` function with the integer `1`). Most, though not all, functions will accept some kind of object as an input. The inputted objects to a function are called its <b>arguments</b>, and these go inside the parentheses. We've seen several examples of `functions` already: `print()`, `int()`, `float()`. These are functions that are standard in Python and can be used by default. `print()` takes in an <b>argument</b> but doesn't <b>return</b> anything. `int()` takes in an object that is convertible to an integer and <b>returns</b> an `int` object. For example: "
533+
"A `function` is called in Python by using its name followed by parentheses (e.g., `print(1)` calls the `print()` function with the integer `1`). Most, though not all, functions will accept some kind of object as an input. The inputted objects to a function are called its <b>arguments</b>, and these go inside the parentheses. We've seen several examples of `functions` already: `print()`, `int()`, `float()`. These are functions that are standard in Python and can be used by default. `print()` takes in an <b>argument</b> but doesn't <b>return</b> anything. `int()` takes in an object that is convertible to an integer and <b>returns</b> an `int` object. For example:\n",
534+
"\n",
535+
"Note: See defining functions: https://docs.python.org/3/reference/compound_stmts.html#function-definitions\n"
535536
]
536537
},
537538
{
@@ -686,7 +687,7 @@
686687
"name": "stdout",
687688
"output_type": "stream",
688689
"text": [
689-
"<function <lambda> at 0x70e79cf37d90>\n",
690+
"<function <lambda> at 0x75bec2f23d90>\n",
690691
"sqroot(100): 10.0\n",
691692
"[0, 2, 4, 6, 8, 10]\n",
692693
"[True, False, True, False, True, False, True, False, True, False, True]\n"
@@ -797,7 +798,7 @@
797798
"\n",
798799
"- <strong>tuple</strong>: tuples are structures that store data in <b>order</b> but are <b>immutable</b>. That is tuples are like lists, except that you can't add or remove from them without creating a whole new object in memory. Sometimes you need things that are immutable (e.g., the next two structures require immutable objects!). Defined using `()` (check examples below).\n",
799800
"\n",
800-
"- <strong>set</strong>: sets are like lists but they are <b>unordered</b> and no duplicates are allowed. Unordered? Yes, sets collect objects but don't keep track of where they were put. On a technical side, it is actually more accurate to say that sets are hyperspecific about where they\u2019ve put things (internally) and so you as the user cannot modify that order. This makes sets very efficient at calculating membership (e.g., is \"1\" in our set?) and intersections (common objects in multiple sets). If you wanted to know if the numbers 7,23,and 108 existed in three lists or three sets, it would be much faster to do the latter. Defined using `{}` or `set(list)` notation. (check examples below).\n",
801+
"- <strong>set</strong>: sets are like lists but they are <b>unordered</b> and no duplicates are allowed. Unordered? Yes, sets collect objects but don't keep track of where they were put. On a technical side, it is actually more accurate to say that sets are hyperspecific about where they’ve put things (internally) and so you as the user cannot modify that order. This makes sets very efficient at calculating membership (e.g., is \"1\" in our set?) and intersections (common objects in multiple sets). If you wanted to know if the numbers 7,23,and 108 existed in three lists or three sets, it would be much faster to do the latter. Defined using `{}` or `set(list)` notation. (check examples below).\n",
801802
"\n",
802803
"- <strong>dictionary</strong>: dictionaries are structures that store objects in pairs. Each pair consists of a <b>key</b> and a <b>value</b>. In analogy, a phone book is a dictionary, where the key is a person's name, and the value their phone number. You could accomplish the same thing using two sets of ordered lists, but dictionaries have the advantage that their keys are stored...as...a...<b>set</b>! Returning to our phonebook example, this would make it really easy to figure out if someone is in the phonebook (otherwise you would need to read every name). It also means that there can't be two identical keys in your dictionary. Defined with `{}` notation with `key:value` pairs. (check examples below)\n",
803804
"\n",
@@ -1032,7 +1033,7 @@
10321033
"\n",
10331034
"- <strong>Example 5</strong>: `in` is an operator that returns a true or false depending on if the object on the left hand side is in the data structure on the right hand side. `in` comparisons can be slow for lists because every object needs to be compared against the thing on the lhs of `in`. Because of the way that sets store objects, `in` can be evaluated very rapidly. \n",
10341035
"\n",
1035-
"- <strong>Example 6</strong>: sets\u00a0can perform set logical operations very effectively (surprised?). If you want to calculate which objects two groups have in common, or which objects in group `a` are not in group `b`, then sets are the data structure that you want to use. In this case we've demonstrated how to calculate the union (set that results from combining unique objects of each) and intersection (set of objects in common to both) of two sets `a` and `b`, using the methods of the same name. \n",
1036+
"- <strong>Example 6</strong>: sets can perform set logical operations very effectively (surprised?). If you want to calculate which objects two groups have in common, or which objects in group `a` are not in group `b`, then sets are the data structure that you want to use. In this case we've demonstrated how to calculate the union (set that results from combining unique objects of each) and intersection (set of objects in common to both) of two sets `a` and `b`, using the methods of the same name. \n",
10361037
"\n",
10371038
"### Dictionary - Examples ###"
10381039
]
@@ -1767,7 +1768,7 @@
17671768
"\n",
17681769
"The above examples show how <b>methods</b> are similar to <b>functions</b> except that by default they have the attributes of the instance passed as inputs (i.e., the string itself in the above examples), and you call them with the `.method(args)` syntax. Similar to functions, methods can return one, many, or no objects. \n",
17691770
"\n",
1770-
"Data structures like `lists` and `dictionaries` are likewise implemented as `classes` within python, and each have their own useful methods that will need to routinely be used. Here are some examples of list methods:\u00a0"
1771+
"Data structures like `lists` and `dictionaries` are likewise implemented as `classes` within python, and each have their own useful methods that will need to routinely be used. Here are some examples of list methods: "
17711772
]
17721773
},
17731774
{
@@ -1855,6 +1856,10 @@
18551856
}
18561857
],
18571858
"source": [
1859+
"import os\n",
1860+
"if not os.path.exists('hamlet.txt'):\n",
1861+
" !wget -q https://raw.githubusercontent.com/SECQUOIA/PU_CHE597_DSinChemE/main/1-Python_Overview/hamlet.txt -O hamlet.txt\n",
1862+
"\n",
18581863
"# Note: the program expects hamlet.txt to be in the folder that this notebook is executed from\n",
18591864
"f = open('hamlet.txt','r')\n",
18601865
"print(f)"
@@ -2742,6 +2747,17 @@
27422747
"print(g.greet())\n",
27432748
"# Expected output: Hello, World!\n"
27442749
]
2750+
},
2751+
{
2752+
"cell_type": "markdown",
2753+
"metadata": {},
2754+
"source": [
2755+
"## References\n",
2756+
"- Python Tutorial: https://docs.python.org/3/tutorial/\n",
2757+
"- Built-in Types: https://docs.python.org/3/library/stdtypes.html\n",
2758+
"- Control Flow: https://docs.python.org/3/tutorial/controlflow.html\n",
2759+
"- Functions: https://docs.python.org/3/tutorial/controlflow.html#defining-functions\n"
2760+
]
27452761
}
27462762
],
27472763
"metadata": {
@@ -2781,4 +2797,4 @@
27812797
},
27822798
"nbformat": 4,
27832799
"nbformat_minor": 0
2784-
}
2800+
}

2-Numpy/Numpy.ipynb

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
" <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n",
1717
" </a>\n",
1818
" <a href=\"https://secquoia.github.io/\">\n",
19-
" <img src=\"https://img.shields.io/badge/\ud83c\udf32\u269b\ufe0f\ud83c\udf10-SECQUOIA-blue\" alt=\"SECQUOIA\"/>\n",
19+
" <img src=\"https://img.shields.io/badge/🌲⚛️🌐-SECQUOIA-blue\" alt=\"SECQUOIA\"/>\n",
2020
" </a>\n",
2121
"</div>"
2222
]
@@ -365,11 +365,10 @@
365365
],
366366
"source": [
367367
"import os\n",
368-
"if os.path.exists('sample.txt'):\n",
369-
" m = np.loadtxt('sample.txt',comments='!',skiprows=1)\n",
370-
" print(m)\n",
371-
"else:\n",
372-
" print('sample.txt not found; skipping loadtxt example')\n"
368+
"if not os.path.exists('sample.txt'):\n",
369+
" !wget -q https://raw.githubusercontent.com/SECQUOIA/PU_CHE597_DSinChemE/main/2-Numpy/sample.txt -O sample.txt\n",
370+
"m = np.loadtxt('sample.txt',comments='!',skiprows=1)\n",
371+
"print(m)\n"
373372
]
374373
},
375374
{
@@ -399,11 +398,10 @@
399398
],
400399
"source": [
401400
"import os\n",
402-
"if os.path.exists('sample2.txt'):\n",
403-
" n = np.genfromtxt('sample2.txt',comments='!',skip_header=1,filling_values=100)\n",
404-
" print(n)\n",
405-
"else:\n",
406-
" print('sample2.txt not found; skipping genfromtxt example')\n"
401+
"if not os.path.exists('sample2.txt'):\n",
402+
" !wget -q https://raw.githubusercontent.com/SECQUOIA/PU_CHE597_DSinChemE/main/2-Numpy/sample2.txt -O sample2.txt\n",
403+
"n = np.genfromtxt('sample2.txt',comments='!',skip_header=1,filling_values=100)\n",
404+
"print(n)\n"
407405
]
408406
},
409407
{
@@ -638,9 +636,9 @@
638636
"name": "stderr",
639637
"output_type": "stream",
640638
"text": [
641-
"/tmp/ipykernel_6198/2143240182.py:7: RuntimeWarning: invalid value encountered in divide\n",
639+
"/tmp/ipykernel_265950/2143240182.py:7: RuntimeWarning: invalid value encountered in divide\n",
642640
" print(\"p/p: {}\".format(p/p))\n",
643-
"/tmp/ipykernel_6198/2143240182.py:8: RuntimeWarning: divide by zero encountered in remainder\n",
641+
"/tmp/ipykernel_265950/2143240182.py:8: RuntimeWarning: divide by zero encountered in remainder\n",
644642
" print(\"p%p: {}\".format(p%p))\n"
645643
]
646644
}
@@ -911,7 +909,9 @@
911909
"\n",
912910
"(sec-np-stats)=\n",
913911
"### Statistics\n",
914-
"Arrays come with built-in methods for calculating means, sums, products, and standard deviations along rows/columns: "
912+
"Arrays come with built-in methods for calculating means, sums, products, and standard deviations along rows/columns:\n",
913+
"\n",
914+
"Note: See statistics routines: https://numpy.org/doc/stable/reference/routines.statistics.html\n"
915915
]
916916
},
917917
{
@@ -1463,6 +1463,19 @@
14631463
" print(b)\n",
14641464
"# Expected output: [0 0 0 1 2]\n"
14651465
]
1466+
},
1467+
{
1468+
"cell_type": "markdown",
1469+
"metadata": {},
1470+
"source": [
1471+
"## References\n",
1472+
"- NumPy User Guide: https://numpy.org/doc/stable/user/index.html\n",
1473+
"- Array creation: https://numpy.org/doc/stable/reference/routines.array-creation.html\n",
1474+
"- Indexing: https://numpy.org/doc/stable/user/basics.indexing.html\n",
1475+
"- Broadcasting: https://numpy.org/doc/stable/user/basics.broadcasting.html\n",
1476+
"- I/O: https://numpy.org/doc/stable/reference/routines.io.html\n",
1477+
"- Statistics: https://numpy.org/doc/stable/reference/routines.statistics.html\n"
1478+
]
14661479
}
14671480
],
14681481
"metadata": {
@@ -1502,4 +1515,4 @@
15021515
},
15031516
"nbformat": 4,
15041517
"nbformat_minor": 0
1505-
}
1518+
}

0 commit comments

Comments
 (0)