begin day 3
This commit is contained in:
67
docs/day3.md
Normal file
67
docs/day3.md
Normal file
@@ -0,0 +1,67 @@
|
||||
# Day 3
|
||||
|
||||
## "For"
|
||||
|
||||
Let's cover our first loop - the "for" loop. In Python the "for" loop is absurdly powerful. Let me show you why.
|
||||
|
||||
### Looping without code
|
||||
|
||||
"For" loops in Python make intuitive sense even without code. I can demonstrate with this basket of fruit:
|
||||
|
||||
```text
|
||||
| |
|
||||
| apple |
|
||||
| banana |
|
||||
\ pear /
|
||||
\_____/
|
||||
```
|
||||
|
||||
For each fruit I want you to take it out of the basket say its name. You start with the apple, since it's at the top, and say "apple". Then you'd take out the banana and say "banana". Finally you'd take our the pear and say "pear".
|
||||
|
||||
In Python our "basket" is a list. Lists look like this:
|
||||
|
||||
```python
|
||||
["apple", "banana", "pear"]
|
||||
```
|
||||
|
||||
They're very similar to tuples. A list is "ordered", the first item in the list will always be the apple - much like the top item in the basket will always be the apple.
|
||||
|
||||
If I asked you to repeat the basket exercise above but with our python list - remove each item and say its name - you'd start with the apple, then remove the banana, and finally remove the pear.
|
||||
|
||||
### Looping with code
|
||||
|
||||
You saying the names of fruit out loud is funny but not very practical. Instead I want you to print the names of the fruit from our basket in the Python interpretor.
|
||||
|
||||
1. Start you interpretor by typing `python` and pressing ++enter++
|
||||
2. In your interpretor type the following:
|
||||
|
||||
```python
|
||||
fruits = ["apple", "banana", "pear"]
|
||||
for fruit in fruits:
|
||||
print(fruit)
|
||||
```
|
||||
|
||||
You should see the fruit print out:
|
||||
|
||||
```python
|
||||
>>> fruits = ["apple", "banana", "pear"]
|
||||
>>> for fruit in fruits:
|
||||
... print(fruit)
|
||||
...
|
||||
apple
|
||||
banana
|
||||
pear
|
||||
>>>
|
||||
```
|
||||
|
||||
you've just used a for loop!
|
||||
|
||||
3. So we can do something with each item in a list. But what if we need the index of each item? Type the following:
|
||||
|
||||
```python
|
||||
fruits = ["apple", "banana", "pear"]
|
||||
for index, fruit in enumerate(fruits):
|
||||
print(f"fruit {fruit} is at index {index}")
|
||||
```
|
||||
|
||||
with `enumerate` we can capture the position of the item and the item itself in the list.
|
||||
@@ -21,4 +21,6 @@
|
||||
|
||||
- Back to the basics: and, or, not, if
|
||||
- Building a Terrible Weather App
|
||||
- Pulling it all together with some Django
|
||||
- Pulling it all together with some Django
|
||||
|
||||
### [Day 3](day3.md): for
|
||||
@@ -4,6 +4,7 @@ nav:
|
||||
- Day 0: day0.md
|
||||
- Day 1: day1.md
|
||||
- Day 2: day2.md
|
||||
- Day 3: day3.md
|
||||
theme:
|
||||
name: material
|
||||
markdown_extensions:
|
||||
|
||||
Reference in New Issue
Block a user