Skip to content

Commit 6b8883e

Browse files
committed
Lists chapter
1 parent efe7851 commit 6b8883e

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ epub:
66
chapters/introduction.md \
77
chapters/installation.md \
88
chapters/basic-datatypes.md \
9+
chapters/lists.md \
910

1011
pdf:
1112
pandoc -o full-speed-python.pdf \
@@ -14,6 +15,7 @@ pdf:
1415
chapters/introduction.md \
1516
chapters/installation.md \
1617
chapters/basic-datatypes.md \
18+
chapters/lists.md \
1719

1820
clean:
1921
rm *.epub *.pdf

chapters/lists.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Lists
2+
3+
Python lists are data structures that group sequences of elements. Lists can have elements of several types and you can also mix different types within the same list although all elements are usually of the same datatype.
4+
5+
Lists are created using square brackets and the elements separated by commas. The elements in a list can be accessed by their positions where 0 is the index of the first element:
6+
7+
>>> l = [1, 2, 3, 4, 5]
8+
>>> l[0]
9+
1
10+
>>> l[1]
11+
2
12+
13+
Can you access the number 4 in the previous list?
14+
15+
Sometimes you want just a small portion of a list, a sublist. Sublists can be retrieved using a technique called *slicing*, which consists on defining the start and end indexes:
16+
17+
>>> l = ['a', 'b', 'c', 'd', 'e']
18+
>>> l[1:3]
19+
['b', 'c']
20+
21+
Finally, arithmetic with lists is also possible, like adding two lists together or repeating the contents of a list.
22+
23+
>>> [1,2] + [3,4]
24+
[1, 2, 3, 4]
25+
>>> [1,2] * 2
26+
[1, 2, 1, 2]
27+
28+
## Exercises with lists
29+
30+
Create a list named "l" with the following values (\[1, 4, 9, 10, 23\]). Using the Python documentation about lists (<https://docs.python.org/3.5/tutorial/introduction.html#lists>) solve the following exercises:
31+
32+
1. Using list slicing get the sublists \[4, 9\] and \[10, 23\].
33+
34+
2. Append the value 90 to the end of the list "l". Check the difference between list concatenation and the "append" method.
35+
36+
3. Calculate the average value of all values on the list. You can use the "sum" and "len" functions.
37+
38+
4. Remove the sublist \[4, 9\].
39+
40+
## List comprehensions
41+
42+
List comprehensions are a concise way to create lists. It consists of square brackets containing an expression followed by the "for" keyword. The result will be a list whose results match the expression. Here’s how to create a list with the squared numbers of another list.
43+
44+
>>> [x*x for x in [0, 1, 2, 3]]
45+
[0, 1, 4, 9]
46+
47+
Given its flexibility, list comprehensions generally make use of the "range" function which returns a range of numbers:
48+
49+
>>> [x*x for x in range(4)]
50+
[0, 1, 4, 9]
51+
52+
Sometimes you may want to filter the elements by a given condition. The "if" keyword can be used in those cases:
53+
54+
>>> [x for x in range(10) if x % 2 == 0]
55+
[0, 2, 4, 6, 8]
56+
57+
The exemple above returns all even values in range 0..10. More about list comprehensions can be found at <https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions>.
58+
59+
## Exercises with list comprehensions
60+
61+
1. Using list comprehensions, create a list with the squares of the first 10 numbers.
62+
63+
2. Using list comprehensions, create a list with the cubes of the first 20 numbers.
64+
65+
3. Create a list comprehension with all the even numbers from 0 to 20, and another one with all the odd numbers.
66+
67+
4. Create a list with the squares of the even numbers from 0 to 20, and sum the list using the "sum" function. The result should be 1140. First create the list using list comprehensions, check the result, then apply the sum to the list comprehension.
68+
69+
5. Make a list comprehension that returns a list with the squares of all even numbers from 0 to 20, but ignore those numbers that are divisible by 3. In other words, each number should be divisible by 2 and not divisible by 3. Search for the "and" keyword in the Python documentation. The resulting list is \[4, 16, 64, 100, 196, 256\].

0 commit comments

Comments
 (0)