Skip to content

Commit

Permalink
Video review
Browse files Browse the repository at this point in the history
  • Loading branch information
s2t2 committed Aug 30, 2024
1 parent 0905ec5 commit f2e82e1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 15 deletions.
5 changes: 2 additions & 3 deletions docs/_quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,14 @@ book:
chapters:
- href: notes/data-processing/for-loops.qmd
text: "List Iteration and Looping"
- href: notes/data-processing/sorting.qmd
text: "List Sorting"
- href: notes/data-processing/mapping.qmd
text: "List Mapping"
- href: notes/data-processing/filtering.qmd
text: "List Filtering"
- href: notes/data-processing/list-comprehensions.qmd
text: "List Comprehensions"

- href: notes/data-processing/sorting.qmd
text: "List Sorting"

- part: "Data Visualization"
chapters:
Expand Down
22 changes: 18 additions & 4 deletions docs/notes/data-processing/for-loops.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ print(symbols)

We saw how we can print and access all items at once. And we saw we can use an index reference to access a specific item. But what if we want to access each item individually, however many there are?

We can use a **\"for\" loop** to access each item one at a time:
We can use a **\"for\" loop** to access each item, one at a time:

```{python}
print("TOP")
Expand All @@ -33,7 +33,21 @@ for item in symbols:
print("BOTTOM")
```

When we use a \"for\" loop, we have to fill in some slots. We have no choice but to put the list we want to loop through in the second slot (after the `in`). But we have an absolute arbitrary choice of what variable name to use to reference each item (after the `for`). Whatever variable name we choose (e.g. `x`), we must also reference that variable within the scope of the loop:
When we use a \"for\" loop, we have to fill in some slots. To understand the slots, see the following pseudocode:

```python
for _________ in _________:
_________
```


```python
for VARIABLE_REFERENCE_FOR_EACH_ITEM in EXISTING_LIST:
DO_SOMETHING_WITH_THAT_ITEM
```


We have no choice but to put the list we want to loop through in the second slot (after the `in`). But we have an absolute arbitrary choice of what variable name to use to reference each item (after the `for`). Whatever variable name we choose (e.g. `x`), we must also reference that variable within the scope of the loop:

```{python}
print("TOP")
Expand All @@ -46,7 +60,7 @@ for x in symbols:
print("BOTTOM")
```

In practice, if we have a list of items plural (`symbols`), we could call each item the singular version (`symbol`) to make our code readable:
As a best practice, if we have a list of items plural (e.g. `symbols`), we could call each item the singular version (e.g. `symbol`) to make our code readable:

```{python}
print("TOP")
Expand All @@ -59,4 +73,4 @@ for symbol in symbols:
print("BOTTOM")
```

Loops are essential and foundational. They will form the basis of more advanced operations, such as [mapping](./mapping.qmd) and [filtering](./filtering.qmd).
The \"for\" loop is an essential and foundational technique, which will form the basis of more advanced operations, such as [mapping](./mapping.qmd) and [filtering](./filtering.qmd).
2 changes: 1 addition & 1 deletion docs/notes/dataviz/overview.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ print(viewers)
```{python}
from plotly.express import bar
fig = bar(x=dates, y=prices, height=350,
fig = bar(x=genres, y=viewers, height=350,
title="Viewership by Genre",
labels={"x": "Genre", "y": "Viewers"}
)
Expand Down
2 changes: 1 addition & 1 deletion docs/notes/python-lang/basic-datatypes/strings.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Multiline string, uses triple quotes (`"""`) on the extremities:

```{python}
message = """
This is a menu for our trading platform.
This is a menu for our application.
To get started, follow these instructions:
Expand Down
13 changes: 8 additions & 5 deletions docs/notes/python-lang/control-flow/conditional-logic.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ Observe that the "else" clause is optional, and will represent a catch-all condi
```{python}
print("TOP")
if 2 + 2 == 4:
print("THEY ARE EQUAL")
print("MORE STUFF")
if 2 + 2 == 5:
print("THEY ARE EQUAL") # NEVER REACHED
print("MORE STUFF") # NEVER REACHED
else:
print("THEY ARE NOT EQUAL") # NEVER REACHED
print("THEY ARE NOT EQUAL")
print("BOTTOM")
```

To implement multiple conditions, we can optionally include any number of "elif" clauses. They must come AFTER the "if", and BEFORE the "else":
To implement multiple conditions, we can optionally include any number of "elif" clauses. They must come AFTER the "if", and BEFORE the "else" (if there is one present):

```{python}
print("TOP")
Expand All @@ -78,6 +78,9 @@ else:
print("BOTTOM")
```

One way you can think about the "elif" clause is that it is like an "else" clause, but with an associated condition.


Observe that if more than one condition evaluates to being true, whichever is listed FIRST will be reached:

```{python}
Expand Down
11 changes: 10 additions & 1 deletion docs/notes/python-lang/control-flow/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ Understanding how indentation controls scope helps us write clean, readable code



Control flow structures in Python include [conditionals](conditional-logic.qmd) (\"if\" statements), [custom functions](custom-functions.qmd), [error handling](errors.qmd), and loops (\"for" loops vs [\"while\" loops](while-loops.qmd)). We will study most of these structures in this chapter, then we will return later to cover [\"for\" loops](../../data-processing/for-loops.qmd) after studying the [`list` datatype](../container-datatypes/lists.qmd) in more detail.
Control flow structures in Python include:

+ [Conditionals](conditional-logic.qmd) (\"If\" Statements)
+ [Custom Functions](custom-functions.qmd)
+ [Error Handling](errors.qmd)
+ Loops:
+ [\"For" loops](../../data-processing/for-loops.qmd)
+ [\"While\" loops](while-loops.qmd)

We will study most of these structures in this section, then we will return later to cover \"for\" loops after studying the [`list` datatype](../container-datatypes/lists.qmd) in more detail.

0 comments on commit f2e82e1

Please sign in to comment.