Skip to content

Commit

Permalink
Merge pull request #32 from fhdsl/S4
Browse files Browse the repository at this point in the history
w2 lesson planning
  • Loading branch information
caalo authored Sep 25, 2024
2 parents 2a4938e + 9e8bb16 commit 62d041b
Show file tree
Hide file tree
Showing 82 changed files with 6,704 additions and 38 deletions.
2 changes: 1 addition & 1 deletion 01-intro-to-computing.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Now, we will get to the basics of programming grammar.

- **Expressions** are be built out of **operations** or **functions**.

- Functions and operations take in **data types**, do something with them, and return another data type.
- Functions and operations take in **data types** as inputs, do something with them, and **return** another data type as ouput.

- We can combine multiple expressions together to form more complex expressions: an expression can have other expressions nested inside it.

Expand Down
45 changes: 28 additions & 17 deletions 02-data-structures.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,37 @@ If you want to access the second and third elements of `chrNum`:
chrNum[1:3]
```

If you want to access everything but the first three elements of `chrNum`:
Another way of accessing the first 3 elements of `chrNum`:

```{python}
chrNum[3:]
chrNum[:3]
```

Here, the stop index number was not specificed. When the start or stop index is *not* specified, it implies that you are subsetting starting the from the beginning of the list or subsetting to the end of the list, respectively:
Here, the start index number was not specified. When the start or stop index is *not* specified, it implies that you are subsetting starting the from the beginning of the list or subsetting to the end of the list, respectively. Here's another example, using negative indicies to count from 3 elements from the end of the list:

```{python}
chrNum[:3]
chrNum[3:]
chrNum[-3:]
```

There are other popular uses of the slice operator `:`, such as negative indicies to count from the end of a list, or subsetting with a fixed increment. You can find more discussion of list slicing [here](https://wesmckinney.com/book/python-builtin#list_slicing).
You can find more discussion of list slicing, using negative indicies and incremental slicing, [here](https://towardsdatascience.com/the-basics-of-indexing-and-slicing-python-lists-2d12c90a94cf).

## Objects in Python

The list data structure has an organization and functionality that metaphorically represents a pen-and-paper list in our physical world. Like a physical object, we have examined:

- What does it contain (in terms of data)?

- What can it do (in terms of operations and functions)?
- What can it do (in terms of functions)?

And if it "makes sense" to us, then it is well-designed.

The list data structure we have been working with is an example of an **Object**. The definition of an object allows us to ask the questions above: *what does it contain, and what can it do?* It is an organizational tool for a collection of data and functions that we can relate to, like a physical object. Formally, an object contains the following:

- **Value** that holds the essential data for the object.

- **Attributes** that store additional data for the object.
- **Attributes** that hold subset or additional data for the object.

- Functions called **Methods** that can be used on the object.
- Functions called **Methods** that automatically takes the object as input.

This organizing structure on an object applies to pretty much all Python data types and data structures.

Expand All @@ -112,6 +111,14 @@ Here are some more examples of methods with lists:
| [`chrNum.sort()`](https://docs.python.org/3/tutorial/datastructures.html) | list `chrNum` | Sorts `chrNum` by ascending order. | None (but `chrNum` is modified!) |
| [`chrNum.reverse()`](https://docs.python.org/3/tutorial/datastructures.html) | list `chrNum` | Reverses the order of `chrNum`. | None (but `chrNum` is modified!) |

## Methods vs Functions

**Methods** *have to* take in the object of interest as an input: `chrNum.count(2)` automatically treat `chrNum` as an input. Methods are built for a specific Object type.

**Functions** do not have an implied input: `len(chrNum)` requires specifying a list in the input.

Otherwise, there is no strong distinction between the two.

## Dataframes

A Dataframe is a two-dimensional data structure that stores data like a spreadsheet does.
Expand All @@ -131,11 +138,11 @@ There is a similar function [`pd.read_excel()`](https://pandas.pydata.org/docs/r

Let's investigate the Dataframe as an object:

- What does a Dataframe contain (in terms of data)?
- What does a Dataframe contain (values, attributes)?

- What can a Dataframe do (in terms of operations and functions)?
- What can a Dataframe do (methods)?

### What does a Dataframe contain (in terms of data)?
## What does a Dataframe contain?

We first take a look at the contents:

Expand All @@ -145,6 +152,10 @@ metadata

It looks like there are 1864 rows and 30 columns in this Dataframe, and when we display it it shows some of the data.

```{python}
metadata
```

We can look at specific columns by looking at **attributes** via the dot operation. We can also look at the columns via the bracket operation.

```{python}
Expand All @@ -164,7 +175,7 @@ The number of rows and columns are also stored as an attribute:
metadata.shape
```

### What can a Dataframe do (in terms of operations and functions)?
## What can a Dataframe do?

We can use the [`.head()`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.head.html) and [`.tail()`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.tail.html) methods to look at the first few rows and last few rows of `metadata`, respectively:

Expand All @@ -175,11 +186,11 @@ metadata.tail()

Both of these functions (without input arguments) are considered as **methods**: they are functions that does something with the Dataframe you are using it on. You should think about `metadata.head()` as a function that takes in `metadata` as an input. If we had another Dataframe called `my_data` and you want to use the same function, you will have to say `my_data.head()`.

#### Subsetting Dataframes
## Subsetting Dataframes

Perhaps the most important operation you will can do with Dataframes is subsetting them. There are two ways to do it. The first way is to subset by numerical indicies, exactly like how we did for lists.

You will use the [`iloc`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html) and bracket operations, and you give two slices: one for the row, and one for the column.
You will use the [`iloc`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html) attribute and bracket operations, and you give two slices: one for the row, and one for the column.

Let's start with a small dataframe to see how it works before returning to `metadata`:

Expand All @@ -194,9 +205,9 @@ Here is how the dataframe looks like with the row and column index numbers:

![](images/pandas_subset_0.png)

Subset the second to fourth rows, and the first two columns:
Subset the first fourth rows, and the first two columns:

![](images/pandas_subset_1.png)
![](images/pandas subset_1.png)

Now, back to `metadata` dataframe:

Expand Down
Binary file added images/list_subset_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/list_subset_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pandas subset_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed images/pandas_subset_1.png
Binary file not shown.
65 changes: 65 additions & 0 deletions scrap.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
title: "Untitled"
format: revealjs
editor: visual
---

## Grammar Structure 3: Evaluation of Functions

A function has a **function name**, **input** **arguments**, and **returns** a data type.

::: callout-tip
## Execution rule for functions:

A function's input arguments, when there's more than one, can be specified by:

- The order the input given: `pow(2, 3)` is different than `pow(3, 2)`.

- The name of the input argument: `pow(base=2, exp=3)`.

If the arguments contain expressions, evaluate those expressions first!
:::

## Examples

```{python}
pow(2, 3)
```

```{python}
pow(base=2, exp=3)
```

```{python}
pow(exp=3, base=2)
```

. . .

```{python}
max(len("hello"), 4)
```

## Learning a new function

1. Function documentation

```
?pow
pow(base, exp, mod=None)
Equivalent to base**exp with 2 arguments or base**exp % mod with 3 arguments
Some types, such as ints, are able to use a more efficient algorithm when
invoked using the three argument form.
```

. . .

2. Documents page of a Python Module

[Base Python documentation of `pow`](https://docs.python.org/3/library/functions.html#pow)

. . .

3. Forums vetted by experts, such as [SlackOverflow](https://stackoverflow.com/questions/tagged/pow+python)
Binary file added slides/images/list_subset_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 28 additions & 20 deletions slides/lesson1_slides.html
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ <h2>Grammar Structure 1: Evaluation of Expressions</h2>
</div>
<div class="fragment">
<ul>
<li>Functions and operations take in <strong>data types</strong>, do something with them, and <strong>return</strong> another data type.</li>
<li>Functions and operations take in <strong>data types</strong> as inputs, do something with them, and <strong>return</strong> another data type as ouput.</li>
</ul>
</div>
<div class="fragment">
Expand Down Expand Up @@ -713,8 +713,8 @@ <h2>Data types</h2>
</tbody>
</table>
</section>
<section id="grammar-structure-2-storing-data-types-in-the-environment" class="slide level2">
<h2>Grammar Structure 2: Storing data types in the environment</h2>
<section id="grammar-structure-2-storing-data-types-as-variables-in-the-environment" class="slide level2">
<h2>Grammar Structure 2: Storing data types as variables in the environment</h2>
<div class="fragment">
<p>To build up a computer program, we need to store our returned data type from our expression somewhere for downstream use.</p>
<div class="cell" data-execution_count="9">
Expand All @@ -736,7 +736,7 @@ <h2>Grammar Structure 2: Storing data types in the environment</h2>
<div class="callout-content">
<p>Evaluate the expression to the right of <code>=</code>.</p>
<p>Bind variable to the left of <code>=</code> to the resulting value.</p>
<p>The variable is stored in the environment.</p>
<p>The variable is stored in the <strong>environment</strong>.</p>
</div>
</div>
</div>
Expand Down Expand Up @@ -809,12 +809,19 @@ <h2>More examples</h2>
</div>
<div class="fragment">
<div class="cell" data-execution_count="14">
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb26-1"><a href="#cb26-1"></a>age <span class="op">=</span> <span class="dv">35</span></span>
<span id="cb26-2"><a href="#cb26-2"></a>score <span class="op">=</span> <span class="bu">max</span>(age, <span class="bu">pow</span>(<span class="dv">2</span>, <span class="dv">3</span>), <span class="dv">20</span>)</span>
<span id="cb26-3"><a href="#cb26-3"></a><span class="bu">dir</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb26-1"><a href="#cb26-1"></a>age <span class="op">=</span> <span class="dv">35</span> <span class="op">-</span> <span class="dv">5</span></span>
<span id="cb26-2"><a href="#cb26-2"></a>score <span class="op">=</span> <span class="bu">max</span>(age, <span class="bu">pow</span>(<span class="dv">2</span>, <span class="dv">3</span>), <span class="dv">20</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell fragment" data-execution_count="14">
<div class="cell-output cell-output-display" data-execution_count="14">

</div>
</div>
<div class="fragment">
<div class="cell" data-execution_count="15">
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb27-1"><a href="#cb27-1"></a><span class="bu">dir</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell fragment" data-execution_count="15">
<div class="cell-output cell-output-display" data-execution_count="15">
<pre><code>['In',
'Out',
'_',
Expand Down Expand Up @@ -843,6 +850,7 @@ <h2>More examples</h2>
'_i12',
'_i13',
'_i14',
'_i15',
'_i2',
'_i3',
'_i4',
Expand Down Expand Up @@ -870,31 +878,31 @@ <h2>More examples</h2>
</section>
<section id="lists" class="slide level2">
<h2>Lists</h2>
<p><strong>List</strong> is a data structure that stores many elements of various data type, and the order its elements are stored matters. Each element of a List contains a single data type, or single data structure.</p>
<p><strong>List</strong> is a data structure that stores many elements of various data type, and the order its elements are stored matters. Each <em>element</em> of a List contains a single data type, or single data structure.</p>
<p>You can create a List via the bracket `[ ]` operator:</p>
<div class="cell" data-execution_count="15">
<div class="sourceCode cell-code" id="cb28"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb28-1"><a href="#cb28-1"></a>chrs <span class="op">=</span> [<span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">2</span>, <span class="dv">1</span>, <span class="dv">2</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell" data-execution_count="16">
<div class="sourceCode cell-code" id="cb29"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb29-1"><a href="#cb29-1"></a>chrs <span class="op">=</span> [<span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">2</span>, <span class="dv">1</span>, <span class="dv">2</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell fragment" data-execution_count="15">
<div class="cell fragment" data-execution_count="16">

</div>
<div class="fragment">
<p>Then, you can access the elements of a list via its “index number”, starting at 0.</p>
<div class="cell" data-execution_count="16">
<div class="sourceCode cell-code" id="cb29"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb29-1"><a href="#cb29-1"></a>chrs[<span class="dv">0</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell" data-execution_count="17">
<div class="sourceCode cell-code" id="cb30"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb30-1"><a href="#cb30-1"></a>chrs[<span class="dv">0</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell fragment" data-execution_count="16">
<div class="cell-output cell-output-display" data-execution_count="16">
<div class="cell fragment" data-execution_count="17">
<div class="cell-output cell-output-display" data-execution_count="17">
<pre><code>2</code></pre>
</div>
</div>
</div>
<div class="fragment">
<div class="cell" data-execution_count="17">
<div class="sourceCode cell-code" id="cb31"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb31-1"><a href="#cb31-1"></a>chrs[<span class="dv">1</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell" data-execution_count="18">
<div class="sourceCode cell-code" id="cb32"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb32-1"><a href="#cb32-1"></a>chrs[<span class="dv">1</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell fragment" data-execution_count="17">
<div class="cell-output cell-output-display" data-execution_count="17">
<div class="cell fragment" data-execution_count="18">
<div class="cell-output cell-output-display" data-execution_count="18">
<pre><code>3</code></pre>
</div>
</div>
Expand Down
Loading

0 comments on commit 62d041b

Please sign in to comment.