Skip to content

Commit

Permalink
For loops
Browse files Browse the repository at this point in the history
  • Loading branch information
s2t2 committed Sep 11, 2024
1 parent c8bdb6a commit a4a93be
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions docs/notes/data-processing/for-loops.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ We saw how we can print and access all items at once. And we saw we can use an i
We can use a **\"for\" loop** to access each item, one at a time:

```{python}
print("TOP")
symbols = ["MSFT", "AAPL", "GOOGL", "AMZN", "NFLX"]
for item in symbols:
print("--------")
Expand All @@ -42,19 +42,19 @@ for _________ in _________:


```python
for VARIABLE_REFERENCE_FOR_EACH_ITEM in EXISTING_LIST:
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")
symbols = ["MSFT", "AAPL", "GOOGL", "AMZN", "NFLX"]
for x in symbols:
for x in symbols: # IF WE USE X HERE IN THE FIRST SLOT...
print("--------")
print(x)
print(x) # ... WE HAVE TO REFERENCE X HERE AS WELL
# NOTHING ELSE, GO TO NEXT ITEM
print("BOTTOM")
Expand All @@ -63,7 +63,7 @@ print("BOTTOM")
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")
symbols = ["MSFT", "AAPL", "GOOGL", "AMZN", "NFLX"]
for symbol in symbols:
print("--------")
Expand Down

0 comments on commit a4a93be

Please sign in to comment.