Skip to content

Commit

Permalink
04-lists.md: improve introduction
Browse files Browse the repository at this point in the history
Pull Request: #567
  • Loading branch information
nathaliagg authored and maxim-belkin committed Apr 6, 2021
1 parent 4812ff1 commit 8ac0d10
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 47 deletions.
27 changes: 27 additions & 0 deletions _extras/extra_exercises.md
Expand Up @@ -49,6 +49,33 @@ or not (yet) added to the main lesson.
> {: .solution}
{: .challenge}

> ## Turn a String into a List
>
> Use a for-loop to convert the string "hello" into a list of letters:
>
> ~~~
> ["h", "e", "l", "l", "o"]
> ~~~
> {: .language-python}
>
> Hint: You can create an empty list like this:
>
> ~~~
> my_list = []
> ~~~
> {: .language-python}
>
> > ## Solution
> > ~~~
> > my_list = []
> > for char in "hello":
> > my_list.append(char)
> > print(my_list)
> > ~~~
> > {: .language-python}
> {: .solution}
{: .challenge}

> ## Fixing and Testing
> From: "Defensive Programming"
>
Expand Down
57 changes: 11 additions & 46 deletions episodes/04-lists.md
Expand Up @@ -20,9 +20,17 @@ list[2:9]), in the same way as strings and arrays."
- "Strings are immutable (i.e., the characters in them cannot be changed)."
---

Similar to a string that can contain many characters, a list is a container that can store many
values. Unlike NumPy arrays, lists are built into the language (so we don't have to load a library
to use them).
In the previous episode, we analyzed a single file with inflammation data. Our goal, however, is to
process all inflammation data we've got, 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 don't have to load a library
to use them.
We create a list by putting values inside square brackets and separating the values with commas:

~~~
Expand Down Expand Up @@ -57,22 +65,6 @@ Yes, we can use negative numbers as indices in Python. When we do so, the index
last element in the list, `-2` the second to last, and so on.
Because of this, `odds[3]` and `odds[-1]` point to the same element here.

If we loop over a list, the loop variable is assigned to its elements one at a time:

~~~
for number in odds:
print(number)
~~~
{: .language-python}

~~~
1
3
5
7
~~~
{: .output}

There is one important difference between lists and strings:
we can change the values in a list,
but we cannot change individual characters in a string.
Expand Down Expand Up @@ -308,33 +300,6 @@ odds: [1, 3, 5, 7]
~~~
{: .output}

> ## Turn a String Into a List
>
> Use a for-loop to convert the string "hello" into a list of letters:
>
> ~~~
> ['h', 'e', 'l', 'l', 'o']
> ~~~
> {: .language-python}
>
> Hint: You can create an empty list like this:
>
> ~~~
> my_list = []
> ~~~
> {: .language-python}
>
> > ## Solution
> > ~~~
> > my_list = []
> > for char in 'hello':
> > my_list.append(char)
> > print(my_list)
> > ~~~
> > {: .language-python}
> {: .solution}
{: .challenge}

Subsets of lists and strings can be accessed by specifying ranges of values in brackets,
similar to how we accessed ranges of positions in a NumPy array.
This is commonly referred to as "slicing" the list/string.
Expand Down
2 changes: 1 addition & 1 deletion episodes/05-loop.md
Expand Up @@ -98,7 +98,7 @@ IndexError: list index out of range
~~~
{: .error}

Here's a better approach:
Here's a better approach: a [for loop]({{ page.root }}/reference/#for-loop)

~~~
odds = [1, 3, 5, 7]
Expand Down

0 comments on commit 8ac0d10

Please sign in to comment.