Skip to content

Commit

Permalink
Review of day 2 material
Browse files Browse the repository at this point in the history
  • Loading branch information
MaybeJustJames committed Sep 2, 2021
1 parent 980e484 commit 3a3342c
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 183 deletions.
13 changes: 7 additions & 6 deletions 05_Lists_Tuples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"No! It's sorting on the first value in each nasted list, we want it to sort on the second! For this, the `sorted()` function accepts an argument called, `key` which is a function that accepts each element of the list and returns the value to sort on. So the key function in our case should be:"
"No! It's sorting on the first value in each nested list, we want it to sort on the second! For this, the `sorted()` function accepts an argument called, `key` which is a function that accepts an element of the list and returns the value to sort on. So the key function in our case should be:"
]
},
{
Expand All @@ -480,7 +480,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can use this to sort out list..."
"Now we can use this to sort our list..."
]
},
{
Expand All @@ -496,7 +496,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Great! This is exactly what we wanted! ... But we're not finished yet. We didn't need to _re-invent what already exists_! `key_second_element_` is just an indexing operation. So the `operator` library that we imported ealier has a function to do just that, it's called `itemgetter`. Let's play with it then try sorting our list with it again..."
"Great! This is exactly what we wanted! ... But we're not finished yet. We didn't need to _re-invent what already exists_! `key_second_element` is just an indexing operation. So the `operator` library that we imported ealier has a function to do just that, it's called `itemgetter`. Let's play with it then try sorting our list with it again..."
]
},
{
Expand Down Expand Up @@ -727,7 +727,7 @@
"metadata": {},
"outputs": [],
"source": [
"list(reversed(\"This is a sentence.\"))"
"list(\"This is a sentence.\"[::-1])"
]
},
{
Expand Down Expand Up @@ -815,7 +815,7 @@
"metadata": {},
"source": [
"## 6. Chapter Review\n",
"In this chapter, you learned how to store a collection of values in a `list`, or a `tuple` or a `str`. You are now able to access values within a *list*, *tuple*, or *str* and you know how to operate on these sequences.\n",
"In this chapter, you learned how to store a collection of values in a `list`, or a `tuple` or a `str`. You are now able to access and operate on values in a *list*, *tuple*, or *str* collection.\n",
"\n",
"The _order_ of values in these collections is well defined. *Lists* are different from *Tuples* insofar as they can be changed over time, whereas a tuples and strings are immutable once created. All of these collections can have multiple identical values, but strings can only contain characters.\n",
"\n",
Expand Down Expand Up @@ -870,7 +870,8 @@
" <summary>Answer</summary>\n",
" Yes, you can call the <code>sorted()</code> function\n",
" to get a sorted list of their elements.\n",
"</details>\n"
"</details>\n",
"\n"
]
},
{
Expand Down
40 changes: 18 additions & 22 deletions 06_Loops.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"metadata": {},
"outputs": [],
"source": [
"numbers = [1, 2, 3, 4, 5]\n",
"numbers = [1, 2, 3, 4]\n",
"\n",
"for number in numbers: # Read as \"for each value (called number) in numbers...\"\n",
" print(number)\n",
Expand All @@ -49,7 +49,7 @@
"metadata": {},
"outputs": [],
"source": [
"numbers = [1, 2, 3, 4, 5]\n",
"numbers = [1, 2, 3, 4]\n",
"\n",
"number = numbers[0] # Set number to the first value in numbers\n",
"print(number)\n",
Expand Down Expand Up @@ -234,7 +234,12 @@
"outputs": [],
"source": [
"def myfactorial(number):\n",
" _"
" _\n",
"\n",
"assert myfactorial(0) == 1, f\"Expected 1, got: {myfactorial(0)}\"\n",
"assert myfactorial(1) == 1, f\"Expected 1, got: {myfactorial(1)}\"\n",
"assert myfactorial(2) == 2, f\"Expected 2, got: {myfactorial(2)}\"\n",
"assert myfactorial(5) == 120, f\"Expected 120, got: {myfactorial(5)}\""
]
},
{
Expand All @@ -245,8 +250,8 @@
"### Exercise 6-4: Summing numbers\n",
"Below is the definition of a function to sum numbers from 1 to the argument. You have 2 tasks:\n",
"\n",
"1. Identify and fix the bug using techniques you have explored so far.\n",
"1. Find the Python standard library function that does the same or similar thing."
"1. Identify and fix the bug(s) using techniques you have explored so far.\n",
"1. Find the Python standard library function that does the same or similar thing (Hint: you can look through [this list](https://docs.python.org/3/library/functions.html))."
]
},
{
Expand Down Expand Up @@ -282,10 +287,7 @@
"\"sample1,0.5\"\n",
"```\n",
"\n",
"You have 2 tasks:\n",
"\n",
"1. Identify and fix the bug using techniques you have explored so far.\n",
"1. Find the Python standard library function that does the same or similar thing."
"Your task is to identify and fix the bug(s) using techniques you have explored so far. The Python function to do this looks like, `','.join(strings)`."
]
},
{
Expand Down Expand Up @@ -337,7 +339,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -364,15 +366,15 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def sequence_identity(seqA, seqB):\n",
" size = min(len(seqA), len(seqB))\n",
" mismatches = 0\n",
" for a, b in zip(seqA, seqB):\n",
" mismatches += (a != b)\n",
" for col in zip(seqA, seqB):\n",
" mismatches += (col[0] != col[1])\n",
"\n",
" return 1.0 - (mismatches / size)\n",
"\n",
Expand All @@ -388,7 +390,7 @@
"---\n",
"### Exercise 6-6: Hamming distance\n",
"\n",
"The Hamming distance between two strings of equal length is the number of positions at which the corresponding character are different. In a more general context, the Hamming distance is one of several string metrics for measuring the _distance_ between two sequences. \n",
"The Hamming distance between two strings of equal length is the number of positions at which the corresponding characters are different. In a more general context, the Hamming distance is one of several string metrics for measuring the _distance_ between two sequences. \n",
"\n",
"For example, the Hamming distance between:\n",
"\n",
Expand Down Expand Up @@ -461,7 +463,7 @@
"\n",
"In this chapter you've learned how to ask the computer to repeat a block of code for you using a `for`-each loop. You also learned about 2 useful Python standard library functions:\n",
"`range()` and `zip()`. This chapter you also finally played a satisfying game of Fizz Buzz. You also critiqued your solution which, like critiquing documents written using a natural\n",
"languagem, can be useful to improve your skills.\n",
"language, can be useful to improve your skills.\n",
"\n",
"In the next chapter we will move beyond frivolous games of Fizz Buzz onto a more serious project.\n",
"\n",
Expand Down Expand Up @@ -495,13 +497,7 @@
"1. What operation **cannot** be performed in the block of code being looped over?\n",
"<details>\n",
" <summary>Answer</summary>\n",
" None! All Python operations can be performed in a loop.\n",
"</details>\n",
"\n",
"1. What does the <code>key</code> argument to <code>sorted()</code> do?\n",
"<details>\n",
" <summary>Answer</summary>\n",
" Returns the value to sort on from each element of the collection being sorted.\n",
" None! All Python operations can be performed in a loop. But <b>be careful</b>: NEVER mofify the list it'er looping over.\n",
"</details>"
]
},
Expand Down
41 changes: 7 additions & 34 deletions 07_Dictionaries.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"{ \"BEL\": 23.4, \"RUS\": 5.5, ...}"
]
},
{
"cell_type": "markdown",
Expand All @@ -144,7 +146,7 @@
"---\n",
"\n",
"### Exercise 7-2: Amino-acid sequence\n",
"Write a function that takes a sequence of amino-acids represented by their single letter code. Return a list of three-letter-codes. For example, given the input: `\"EIKGGQ\"` return `['Glu', 'Ile', 'Lys', 'Gly', 'Gly', 'Gln']`"
"Write a function that takes a sequence of amino-acids represented by their single letter code. Return a list of three-letter-codes. For example, given the input: `\"EIKGGQ\"` return `['Glu', 'Ile', 'Lys', 'Gly', 'Gly', 'Gln']`. You should use the `three_letter_codes` dictionary we defined earlier."
]
},
{
Expand Down Expand Up @@ -193,7 +195,7 @@
"metadata": {},
"outputs": [],
"source": [
"for key, value in three_letter_codes.items():\n",
"for (key, value) in three_letter_codes.items():\n",
" print(key, value)"
]
},
Expand Down Expand Up @@ -248,29 +250,6 @@
"assert frequencies(sequence) == expected, f\"Unexpected {frequencies(sequence)}\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"### Exercise 7-4: Density\n",
"Write a function that transforms the result of your `frequencies()` function above by modifying the dictionary so that its values are a dictionary containing 2 keys: the count as before,\n",
"and also a fraction of the length of the sequence. e.g. if the letter 'S' appears 50% of the time it's value in the new dictionary will be `{\"count\":92, \"fraction\":0.5}`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def amino_acid_fractions(freqs):\n",
" ...\n",
"\n",
"amino_acid_fractions(frequencies(sequence))"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -328,13 +307,13 @@
"\n",
"### Review Questions\n",
"\n",
"1. How is an empty dictionary represented?\n",
"1. How is an empty dictionary written?\n",
"<details>\n",
" <summary>Answer</summary>\n",
" <code>{}</code> or <code>dict()</code>\n",
"</details>\n",
"\n",
"1. Can a dictionary be a value in a dictionary?\n",
"1. Can any data type be a value in a dictionary?\n",
"<details>\n",
" <summary>Answer</summary>\n",
" Yes!\n",
Expand All @@ -352,12 +331,6 @@
" Yes!\n",
"</details>\n",
"\n",
"1. How could you associate 1 key with multiple values?\n",
"<details>\n",
" <summary>Answer</summary>\n",
" Use a collection as the value.\n",
"</details>\n",
"\n",
"1. How can you access a key you're not sure is in the dictionary?\n",
"<details>\n",
" <summary>Answer</summary>\n",
Expand Down
49 changes: 16 additions & 33 deletions lesson_plans/lesson_plan_day2.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

**SAY**: Using values like a number, text, an image, etc are great and useful. But often we need to collect many of these values
together. For example, to compute statistics on experimental observations, we need a collection of multiple observations.
Looking at each observation individual is not useful in this case.
Looking at each observation individually is not useful in this case.

**SAY**: Python gives us a number of ways to store collections of values together. Each with their own characteristics. In this
session we will look at the **List**, **Tuple**, and **String** collections and apply them to improving our Fizz Buzz
Expand Down Expand Up @@ -85,7 +85,7 @@

**CMD**: Describe indexing and slicing with negative indexes using examples.

**CMD**: Demonstrate the slicing shorthand for "begginning" and "end".
**CMD**: Demonstrate the slicing shorthand for "beginning" and "end".

**CMD**: Demo the "step" value in slicing.

Expand All @@ -96,7 +96,7 @@
**EXERCISE** 5-2

**SAY**: The last attempt we made at writing a function to play Fizz Buzz wasn't very interesting. In order to play to larger numbers
we would have to type out a longer and longer list. This negates the power of your computer if you can't get to to compute
we would have to type out longer and longer lists. This negates the power of your computer if you can't get to to compute
for you. So let's have another attempt using slicing. This time well replace the right numbers with either "Fizz" or "Buzz".

**EXERCISE** 5-3
Expand Down Expand Up @@ -128,11 +128,13 @@
the `operator` library. As the course progresses you will see more of the standard library and the huge array of
facilities it provides, however we encourage you to explore and see what useful functionality exists. Let's begin now to
try to understand what these functions and operators do...

**SAY**: Before you run the cells try to guess what the result will be.

**CMD**: Run the example cells.

**SAY**: The `sorted()` function takes 2 optional arguments that customise its behaviour: `key` is a function used to access the key
to sort on. And `reverse` is used to sort in reverse order.
to sort on. And `reverse` is used to sort in reverse order, it should be `True` or `False`.

**CMD**: Read and demo from notebook about the `key` argument.

Expand All @@ -153,7 +155,7 @@
**SAY**: Practically, strings are almost exactly like tuples except you cannot store arbitrary values in strings: no, you can only store
characters. This is just a different way of looking at text data that allows you to see that indexing and slicing also make sense.

**EXERCISE** 5-6
**EXERCISE** 5-6, 5-7

### 11:55

Expand Down Expand Up @@ -183,30 +185,9 @@
Each of our implementations so far has required you, the programmer, to write down the steps for every
single number. We've not used this special power that computers have: to repeat steps.

**SAY**: This is how you might expect to be able to describe the Fizz Buzz game to a computer:

**CMD**: Say this...
```
STEP 1: SET VARIABLE COUNTER TO 1
STEP 2: CHECK IF COUNTER IS A MULTIPLE OF 5 OR 3
STEP 3: IF SO, SAY "FIZZ BUZZ"
STEP 4: CHECK IF COUNTER IS A MULTIPLE OF 3
STEP 5: IF SO, SAY "FIZZ"
STEP 6: CHECK IF COUNTER IS A MULTIPLE OF 5
STEP 7: IF SO, SAY "BUZZ"
STEP 8: CHECK IF WE HAVE FINISHED COUNTING
STEP 9: IF SO, STOP
STEP 10: INCREMENT COUNTER
STEP 11: GO TO STEP 2
```

**SAY**: The only part we're still mmissing is the ability to say, `GO TO STEP 2`. This is what a _loop_ does.
You can think `GO TO STEP 2` as creating a control-flow loop. Steps 2-9 are looped until the stop
condition is met.

**SAY**: The loop construct we'll be looking at with here is known as the _for-each loop_. It's called this
because it allows us to repeat a set of instructions _for each_ member of a collection. For example,
here we have a list of numbers...
**SAY**: The loop construct we'll be looking at here is known as the _for-each loop_. It's called this
because it allows us to repeat a set of instructions _for each_ member of a collection.
For example, here we have a list of numbers...

**CMD**: Explain the syntax of a **for** loop. Then explain the loop by unrolling it.

Expand Down Expand Up @@ -249,9 +230,11 @@ STEP 11: GO TO STEP 2

**SAY** Before we finish up with loops I want to introduce you to one more member of the Python
standard tool belt: "ZIP"! What problem does this solve?
Imagine you have 2 collections with element you wish to process in your loop at the same time...
Imagine you have 2 collections with elements you wish to process in your loop at the same time...

**CMD**: Walk through description in the notebook and sequence identity example.

**CMD**: Walk through description in the notebook and sequence identity example
**CMD**: Explain `None`.

**EXERCISES**: 6-6, 6-7

Expand All @@ -268,7 +251,7 @@ STEP 11: GO TO STEP 2
**CMD**: Topic: dictionaries

**SAY**: Now that we're finished implementing the Fizz Buzz game, we can tackle a more realistic problem.
Here is the scenario: we've been comissioned to produce comparative plots of temerature data for
Here is the scenario: we've been comissioned to produce comparative plots of temperature data for
3 countries and globally from 1901 to 2020: Belgium, Russia, and Australia.

**SAY**: The data we need is publicly available on the internet but from different sources so we will need
Expand Down
5 changes: 2 additions & 3 deletions quizzes/day2_after_lunch.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
title:
- Quiz
title: ""Quiz: after lunch, day 2"
mainfont: "Liberation Mono"
---

Expand All @@ -14,7 +13,7 @@ C. `reversed(my_string)`

D. `my_string.reverse`

# What is the result of this expression?
# What will this statement do?

```python
"test"[0] = 'b'
Expand Down
Loading

0 comments on commit 3a3342c

Please sign in to comment.