Skip to content

Commit 3be84ce

Browse files
nathaliaggmaxim-belkin
authored andcommitted
04-lists.md: improve introduction
Pull Request: #567
1 parent ea8d883 commit 3be84ce

File tree

3 files changed

+39
-47
lines changed

3 files changed

+39
-47
lines changed

_episodes/04-lists.md

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,17 @@ list[2:9]), in the same way as strings and arrays."
2020
- "Strings are immutable (i.e., the characters in them cannot be changed)."
2121
---
2222

23-
Similar to a string that can contain many characters, a list is a container that can store many
24-
values. Unlike NumPy arrays, lists are built into the language (so we don't have to load a library
25-
to use them).
23+
In the previous episode, we analyzed a single file with inflammation data. Our goal, however, is to
24+
process all inflammation data we've got, which means that we still have eleven more files to go!
25+
26+
The natural first step is to collect the names of all the files that we have to process. In Python,
27+
a list is a way to store multiple values together. In this episode, we will learn how to store
28+
multiple values in a list as well as how to work with lists.
29+
30+
## Python lists
31+
32+
Unlike NumPy arrays, lists are built into the language so we don't have to load a library
33+
to use them.
2634
We create a list by putting values inside square brackets and separating the values with commas:
2735

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

60-
If we loop over a list, the loop variable is assigned to its elements one at a time:
61-
62-
~~~
63-
for number in odds:
64-
print(number)
65-
~~~
66-
{: .language-python}
67-
68-
~~~
69-
1
70-
3
71-
5
72-
7
73-
~~~
74-
{: .output}
75-
7668
There is one important difference between lists and strings:
7769
we can change the values in a list,
7870
but we cannot change individual characters in a string.
@@ -308,33 +300,6 @@ odds: [1, 3, 5, 7]
308300
~~~
309301
{: .output}
310302
311-
> ## Turn a String Into a List
312-
>
313-
> Use a for-loop to convert the string "hello" into a list of letters:
314-
>
315-
> ~~~
316-
> ['h', 'e', 'l', 'l', 'o']
317-
> ~~~
318-
> {: .language-python}
319-
>
320-
> Hint: You can create an empty list like this:
321-
>
322-
> ~~~
323-
> my_list = []
324-
> ~~~
325-
> {: .language-python}
326-
>
327-
> > ## Solution
328-
> > ~~~
329-
> > my_list = []
330-
> > for char in 'hello':
331-
> > my_list.append(char)
332-
> > print(my_list)
333-
> > ~~~
334-
> > {: .language-python}
335-
> {: .solution}
336-
{: .challenge}
337-
338303
Subsets of lists and strings can be accessed by specifying ranges of values in brackets,
339304
similar to how we accessed ranges of positions in a NumPy array.
340305
This is commonly referred to as "slicing" the list/string.

_episodes/05-loop.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ IndexError: list index out of range
9898
~~~
9999
{: .error}
100100

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

103103
~~~
104104
odds = [1, 3, 5, 7]

_extras/extra_exercises.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,33 @@ or not (yet) added to the main lesson.
4949
> {: .solution}
5050
{: .challenge}
5151
52+
> ## Turn a String into a List
53+
>
54+
> Use a for-loop to convert the string "hello" into a list of letters:
55+
>
56+
> ~~~
57+
> ["h", "e", "l", "l", "o"]
58+
> ~~~
59+
> {: .language-python}
60+
>
61+
> Hint: You can create an empty list like this:
62+
>
63+
> ~~~
64+
> my_list = []
65+
> ~~~
66+
> {: .language-python}
67+
>
68+
> > ## Solution
69+
> > ~~~
70+
> > my_list = []
71+
> > for char in "hello":
72+
> > my_list.append(char)
73+
> > print(my_list)
74+
> > ~~~
75+
> > {: .language-python}
76+
> {: .solution}
77+
{: .challenge}
78+
5279
> ## Fixing and Testing
5380
> From: "Defensive Programming"
5481
>

0 commit comments

Comments
 (0)