From fd28fa3fde40c0195ad3be02e5872b9ec7e92223 Mon Sep 17 00:00:00 2001 From: ducoterra Date: Tue, 17 Nov 2020 19:26:20 -0500 Subject: [PATCH] begin day 3 --- docs/day3.md | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ docs/index.md | 4 ++- mkdocs.yml | 1 + 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 docs/day3.md diff --git a/docs/day3.md b/docs/day3.md new file mode 100644 index 0000000..c8f9bdd --- /dev/null +++ b/docs/day3.md @@ -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. \ No newline at end of file diff --git a/docs/index.md b/docs/index.md index f0f4e22..8366b99 100644 --- a/docs/index.md +++ b/docs/index.md @@ -21,4 +21,6 @@ - Back to the basics: and, or, not, if - Building a Terrible Weather App -- Pulling it all together with some Django \ No newline at end of file +- Pulling it all together with some Django + +### [Day 3](day3.md): for \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index 420c2b1..94766e1 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -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: