Skip to content
This repository has been archived by the owner on Oct 9, 2024. It is now read-only.

Commit

Permalink
First pass edit of lessons.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonkirbysimonkirby committed Nov 3, 2022
1 parent b77c1c1 commit 9b5c961
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 324 deletions.
26 changes: 12 additions & 14 deletions _episodes/02_fundamentals.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ Any Python interpreter can be used as a calculator:

This is great but not very interesting.
To do anything useful with data, we need to assign its value to a _variable_.
In Python, we can [assign]({{ page.root }}/reference.html#assign) a value to a
[variable]({{ page.root }}/reference.html#variable), using the equals sign `=`.
In Python, we can assign a value to a variable, using the equals sign `=`.
For example, we can track the weight of a patient who weighs 60 kilograms by
assigning the value `60` to a variable `weight_kg`:

Expand All @@ -56,7 +55,7 @@ In Python, variable names:

- can include letters, digits, and underscores
- cannot start with a digit
- are [case sensitive]({{ page.root }}/reference.html#case-sensitive).
- are case-sensitive.

This means that, for example:
- `weight0` is a valid variable name, whereas `0weight` is not
Expand Down Expand Up @@ -107,7 +106,7 @@ patient_id = 'inflam_' + patient_id
## Built-in Python functions

To carry out common tasks with data and variables in Python,
the language provides us with several built-in [functions]({{ page.root }}/reference.html#function).
the language provides us with several built-in functions.
To display information to the screen, we use the `print` function:

~~~
Expand Down Expand Up @@ -142,8 +141,7 @@ inflam_001 weight in kilograms: 60.3
{: .output}

We can also call a function inside of another
[function call]({{ page.root }}/reference.html#function-call).
For example, Python has a built-in function called `type` that tells you a value's data type:
function call. For example, Python has a built-in function called `type` that tells you a value's data type:

~~~
print(type(60.3))
Expand Down Expand Up @@ -194,8 +192,8 @@ weight in kilograms is now: 65.0
~~~
{: .output}

> ## Variables as Sticky Notes
>
## Variables as Sticky Notes

> A variable in Python is analogous to a sticky note with a name written on it:
> assigning a value to a variable is like putting that sticky note on a particular value.
>
Expand Down Expand Up @@ -246,8 +244,8 @@ stuck on it](../fig/python-sticky-note-variables-03.svg)
{: .callout}
> ## Check Your Understanding
>
## Check Your Understanding
> What values do the variables `mass` and `age` have after each of the following statements?
> Test your answer by executing the lines.
>
Expand All @@ -270,8 +268,8 @@ stuck on it](../fig/python-sticky-note-variables-03.svg)
> {: .solution}
{: .challenge}
> ## Sorting Out References
>
## Sorting Out References
> Python allows you to assign multiple values to multiple variables in one line by separating
> the variables and values with commas. What does the following program print out?
>
Expand All @@ -290,8 +288,8 @@ stuck on it](../fig/python-sticky-note-variables-03.svg)
> {: .solution}
{: .challenge}
> ## Seeing Data Types
>
## Seeing Data Types
> What are the data types of the following variables?
>
> ~~~
Expand Down
29 changes: 7 additions & 22 deletions _episodes/03_lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ At the end of this lesson you will be able to:
- Append values to an existing list
- Create and manipulate nested lists

## Keypoints
## Key points

- "`[value1, value2, value3, ...]` creates a list."
- "Lists can contain any Python object, including lists (i.e., list of lists)."
Expand All @@ -26,25 +26,10 @@ list[2:9]), in the same way as strings and arrays."
- "Lists are mutable (i.e., their values can be changed in place)."
- "Strings are immutable (i.e., the characters in them cannot be changed)."

## Introduction

In the previous episode, we analyzed a single file of clinical trial inflammation data. However,
after finding some peculiar and potentially suspicious trends in the trial data we ask
Dr. Maverick if they have performed any other clinical trials. Surprisingly, they say that they
have and provide us with 11 more CSV files for a further 11 clinical trials they have undertaken
since the initial trial.

Our goal now is to process all the inflammation data we have, which means that we still have
eleven more files to go!

The natural first step is to collect the names of all the files that we have to process. In Python,
a list is a way to store multiple values together. In this episode, we will learn how to store
multiple values in a list as well as how to work with lists.

## Python lists

Unlike NumPy arrays, lists are built into the language so we do not have to load a library
to use them.
Lists are a data structure in Python that can contain a changeable (or mutable) sequence of elements. These elements can be values or other variables.

We create a list by putting values inside square brackets and separating the values with commas:

~~~
Expand Down Expand Up @@ -119,11 +104,11 @@ TypeError: 'str' object does not support item assignment

does not.

> ## Ch-Ch-Ch-Ch-Changes
>
> Data which can be modified in place is called [mutable]({{ page.root }}/reference.html#mutable),
## Ch-Ch-Ch-Ch-Changes

> Data which can be modified in place is called mutable,
> while data which cannot be modified is called
> [immutable]({{ page.root }}/reference.html#immutable).
> immutable.
> Strings and numbers are immutable. This does not mean that variables with string or number values
> are constants, but when we want to change the value of a string or number variable, we can only
> replace the old value with a completely new value.
Expand Down
171 changes: 11 additions & 160 deletions _episodes/05_control_flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ At the end of this lesson you will be able to:
- Explain the basic conditional statements, such as `if`, `else`, `and`, `not`, and `or`
- Write some simple expressions using these statements

## Representing a problem computationally



## Keypoints
## Key points
- "Use `if condition` to start a conditional statement, `elif condition` to
provide additional tests, and `else` to provide a default."
- "The bodies of the branches of conditional statements must be indented."
Expand Down Expand Up @@ -104,8 +100,8 @@ else:
Note that to test for equality we use a double equals sign `==`
rather than a single equals sign `=` which is used to assign values.

> ## Comparing in Python
>
## Comparing in Python

> Along with the `>` and `==` operators we have already used for comparing values in our
> conditionals, there are a few more options to know about:
>
Expand Down Expand Up @@ -249,8 +245,8 @@ but we could also imagine not using the `else` catch-all
so that messages are only printed when something is wrong,
freeing us from having to manually examine every plot for features we've seen before.

> ## How Many Paths?
>
## How Many Paths?

> Consider this code:
>
> ~~~
Expand All @@ -277,8 +273,8 @@ freeing us from having to manually examine every plot for features we've seen be
> {: .solution}
{: .challenge}
> ## What Is Truth?
>
## What Is Truth?
> `True` and `False` booleans are not the only values in Python that are true and false.
> In fact, *any* value can be used in an `if` or `elif`.
> After reading and running the code below,
Expand All @@ -301,8 +297,8 @@ freeing us from having to manually examine every plot for features we've seen be
> {: .language-python}
{: .challenge}
> ## That's Not Not What I Meant
>
## That's Not Not What I Meant
> Sometimes it is useful to check whether some condition is not true.
> The Boolean operator `not` can do this explicitly.
> After reading and running the code below,
Expand All @@ -320,8 +316,8 @@ freeing us from having to manually examine every plot for features we've seen be
> {: .language-python}
{: .challenge}
> ## Close Enough
>
## Close Enough
> Write some conditions that print `True` if the variable `a` is within 10% of the variable `b`
> and `False` otherwise.
> Compare your implementation with your partner's:
Expand Down Expand Up @@ -363,148 +359,3 @@ freeing us from having to manually examine every plot for features we've seen be
> > have string representations which can be printed.
> {: .solution}
{: .challenge}
> ## In-Place Operators
>
> Python (and most other languages in the C family) provides
> [in-place operators]({{ page.root }}/reference.html#in-place-operators)
> that work like this:
>
> ~~~
> x = 1 # original value
> x += 1 # add one to x, assigning result back to x
> x *= 3 # multiply x by 3
> print(x)
> ~~~
> {: .language-python}
>
> ~~~
> 6
> ~~~
> {: .output}
>
> Write some code that sums the positive and negative numbers in a list separately,
> using in-place operators.
> Do you think the result is more or less readable
> than writing the same without in-place operators?
>
> > ## Solution
> > ~~~
> > positive_sum = 0
> > negative_sum = 0
> > test_list = [3, 4, 6, 1, -1, -5, 0, 7, -8]
> > for num in test_list:
> > if num > 0:
> > positive_sum += num
> > elif num == 0:
> > pass
> > else:
> > negative_sum += num
> > print(positive_sum, negative_sum)
> > ~~~
> > {: .language-python}
> >
> > Here `pass` means "don't do anything".
> In this particular case, it's not actually needed, since if `num == 0` neither
> > sum needs to change, but it illustrates the use of `elif` and `pass`.
> {: .solution}
{: .challenge}
> ## Sorting a List Into Buckets
>
> In our `data` folder, large data sets are stored in files whose names start with
> "inflammation-" and small data sets -- in files whose names start with "small-". We
> also have some other files that we do not care about at this point. We'd like to break all
> these files into three lists called `large_files`, `small_files`, and `other_files`,
> respectively.
>
> Add code to the template below to do this. Note that the string method
> [`startswith`](https://docs.python.org/3/library/stdtypes.html#str.startswith)
> returns `True` if and only if the string it is called on starts with the string
> passed as an argument, that is:
>
> ~~~
> 'String'.startswith('Str')
> ~~~
> {: .language-python}
> ~~~
> True
> ~~~
> {: .output}
> But
> ~~~
> 'String'.startswith('str')
> ~~~
> {: .language-python}
> ~~~
> False
> ~~~
> {: .output}
>Use the following Python code as your starting point:
> ~~~
> filenames = ['inflammation-01.csv',
> 'myscript.py',
> 'inflammation-02.csv',
> 'small-01.csv',
> 'small-02.csv']
> large_files = []
> small_files = []
> other_files = []
> ~~~
> {: .language-python}
>
> Your solution should:
>
> 1. loop over the names of the files
> 2. figure out which group each filename belongs in
> 3. append the filename to that list
>
> In the end the three lists should be:
>
> ~~~
> large_files = ['inflammation-01.csv', 'inflammation-02.csv']
> small_files = ['small-01.csv', 'small-02.csv']
> other_files = ['myscript.py']
> ~~~
> {: .language-python}
>
> > ## Solution
> > ~~~
> > for filename in filenames:
> > if filename.startswith('inflammation-'):
> > large_files.append(filename)
> > elif filename.startswith('small-'):
> > small_files.append(filename)
> > else:
> > other_files.append(filename)
> >
> > print('large_files:', large_files)
> > print('small_files:', small_files)
> > print('other_files:', other_files)
> > ~~~
> > {: .language-python}
> {: .solution}
{: .challenge}
> ## Counting Vowels
>
> 1. Write a loop that counts the number of vowels in a character string.
> 2. Test it on a few individual words and full sentences.
> 3. Once you are done, compare your solution to your neighbor's.
> Did you make the same decisions about how to handle the letter 'y'
> (which some people think is a vowel, and some do not)?
>
> > ## Solution
> > ~~~
> > vowels = 'aeiouAEIOU'
> > sentence = 'Mary had a little lamb.'
> > count = 0
> > for char in sentence:
> > if char in vowels:
> > count += 1
> >
> > print('The number of vowels in this string is ' + str(count))
> > ~~~
> > {: .language-python}
> {: .solution}
{: .challenge}
Loading

0 comments on commit 9b5c961

Please sign in to comment.