Skip to content

Commit 4011709

Browse files
feat: add loops and sequences lectures (freeCodeCamp#61294)
Co-authored-by: Dario-DC <[email protected]>
1 parent 7e4150f commit 4011709

File tree

9 files changed

+1117
-108
lines changed

9 files changed

+1117
-108
lines changed

curriculum/challenges/english/25-front-end-development/lecture-working-with-loops-and-sequences/67fe8399c41a212871025f96.md

Lines changed: 170 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,175 @@
11
---
22
id: 67fe8399c41a212871025f96
33
title: What Are Lists and How Do They Work?
4-
challengeType: 11
5-
videoId: nVAaxZ34khk
4+
challengeType: 19
65
dashedName: what-are-lists-and-how-do-they-work
76
---
87

98
# --description--
109

11-
Watch the video or read the transcript and answer the questions below.
10+
Over the next few lectures we are going to learn about lists, tuples, and ranges, which are three basic sequence types used in Python.
11+
12+
The list data type is an ordered sequence of elements that can be comprised of strings, numbers, or even other lists. Similar to JavaScript, lists are mutable and use zero-based indexing, meaning that the first element of the list is at index zero.
13+
14+
Here is the basic syntax for a list:
15+
16+
```py
17+
cities = ['Los Angeles', 'London', 'Tokyo']
18+
```
19+
20+
To access an element from the `cities` list, you can reference its index number in the sequence. Here is an example of accessing the first element of the `cities` list:
21+
22+
```py
23+
cities = ['Los Angeles', 'London', 'Tokyo']
24+
cities[0] # 'Los Angeles'
25+
```
26+
27+
Negative indexing is used to access elements starting from the end of the list instead of the beginning at index `0`. To access the last element of any list, you can use `-1` like this:
28+
29+
```py
30+
cities = ['Los Angeles', 'London', 'Tokyo']
31+
cities[-1] # 'Tokyo'
32+
```
33+
34+
Another way to create a list is to use the `list()` constructor. The `list()` constructor is used to convert an iterable into a list like this:
35+
36+
```py
37+
developer = 'Jessica'
38+
list(developer) # ['J', 'e', 's', 's', 'i', 'c', 'a']
39+
```
40+
41+
An iterable is a special type of object that can be looped over one item at a time. You will learn more about loops in Python later on.
42+
43+
To get the total number of elements in a list, you can use the `len()` function like this:
44+
45+
```py
46+
numbers = [1, 2, 3, 4, 5]
47+
len(numbers) # 5
48+
```
49+
50+
If you wanted to update a value at a particular index, you can do something like this:
51+
52+
```py
53+
programming_languages = ['Python', 'Java', 'C++', 'Rust']
54+
programming_languages[0] = 'JavaScript'
55+
print(programming_languages) # ['JavaScript', 'Java', 'C++', 'Rust']
56+
```
57+
58+
Since lists are mutable, you can update any element in the list as long as you pass in a valid index number. If you pass in an index (either positive or negative) that is out of bounds for the list, then you will receive an `IndexError`:
59+
60+
```py
61+
programming_languages = ['Python', 'Java', 'C++', 'Rust']
62+
programming_languages[10] = 'JavaScript'
63+
64+
"""
65+
Traceback (most recent call last):
66+
File "<stdin>", line 2, in <module>
67+
IndexError: list assignment index out of range
68+
"""
69+
```
70+
71+
If you want to remove an element from a list you can use the `del` keyword like this:
72+
73+
```py
74+
developer = ['Jane Doe', 23, 'Python Developer']
75+
del developer[1]
76+
print(developer) # ['Jane Doe', 'Python Developer']
77+
```
78+
79+
Sometimes it is helpful to check if an element is inside the list. To do that, you can use the `in` keyword like this:
80+
81+
```py
82+
programming_languages = ['Python', 'Java', 'C++', 'Rust']
83+
84+
'Rust' in programming_languages # True
85+
'JavaScript' in programming_languages # False
86+
```
87+
88+
Sometimes it is common to have lists nested inside of other lists like this:
89+
90+
```py
91+
developer = ['Alice', 25, ['Python', 'Rust', 'C++']]
92+
```
93+
94+
In this example, we have one nested list containing three popular programming languages. To access the nested list, you will need to access it using index `2` since lists are zero based indexed:
95+
96+
```py
97+
developer = ['Alice', 25, ['Python', 'Rust', 'C++']]
98+
developer[2] # ['Python', 'Rust', 'C++']
99+
```
100+
101+
Then to access the second language from that nested list, you will need to access it using index `1` like this:
102+
103+
```py
104+
developer = ['Alice', 25, ['Python', 'Rust', 'C++']]
105+
developer[2][1] # 'Rust'
106+
```
107+
108+
Another common technique used with lists is unpacking values.
109+
110+
Unpacking values from a list is a technique used to assign values from a list to new variables. Here is an example of unpacking a `developer` list into new variables called `name`, `age` and `job`.
111+
112+
```py
113+
developer = ['Alice', 34, 'Rust Developer']
114+
name, age, job = developer
115+
116+
print(name) # 'Alice'
117+
print(age) # 34
118+
print(job) # 'Rust Developer'
119+
```
120+
121+
Here, `name` has the value `'Alice'`, `age` has the value `34`, and `job` has the value `'Rust Developer'`.
122+
123+
If you need to collect any remaining elements from a list, you can use the asterisk (`*`) operator like this:
124+
125+
```py
126+
developer = ['Alice', 34, 'Rust Developer']
127+
name, *rest = developer
128+
129+
print(name) # 'Alice'
130+
print(rest) # [34, 'Rust Developer']
131+
```
132+
133+
In this example, `name` will still have the value `'Alice'`, and `rest` is a list of two items: the number `34` and the string `'Rust Developer'`.
134+
135+
If the numbers of variables on the left side of the assignment operator doesn't match the total numbers of items in the list, then you will receive a `ValueError`:
136+
137+
```py
138+
developer = ['Alice', 34, 'Rust Developer']
139+
name, age, job, city = developer
140+
141+
"""
142+
Traceback (most recent call last):
143+
File "<stdin>", line 2, in <module>
144+
ValueError: not enough values to unpack (expected 4, got 3)
145+
"""
146+
```
147+
148+
The last concept we will look at is the slice operator (`:`). Similar to strings, you can access portions of a list by using the slice operator like this:
149+
150+
```py
151+
desserts = ['Cake', 'Cookies', 'Ice Cream', 'Pie', 'Brownies']
152+
desserts[1:4] # ['Cookies', 'Ice Cream', 'Pie']
153+
```
154+
155+
In this example, the start index is `1` since that points to the second item in the list. Then we use the slice operator followed by an end index of `4`, which includes everything up to (but not including), the item at that index.
156+
157+
Another thing you can do with the slice operator `:` is specify a step interval which determines how much to increment between the indices. Let's say you had a list of numbers like this:
158+
159+
```py
160+
numbers = [1, 2, 3, 4, 5, 6]
161+
```
162+
163+
If you wanted to extract a list of just even numbers, you can use the slicing operator like this:
164+
165+
```py
166+
numbers = [1, 2, 3, 4, 5, 6]
167+
numbers[1::2] # [2, 4, 6]
168+
```
169+
170+
The first even number is at index `1`, so that will be the start index. Since we want to go through the end of the list, then we omit the end index. Lastly, we specify `2` for the optional step interval so it will only increment by `2` instead of the default `1`.
171+
172+
Lists are a useful and flexible data structure that you will use a lot in your Python programs. In the next lecture, you will learn about common methods that you can use with lists.
12173

13174
# --questions--
14175

@@ -22,7 +183,7 @@ They are two-based indexed.
22183

23184
### --feedback--
24185

25-
Refer to the beginning of the video for the answer.
186+
Refer to the beginning of the lecture for the answer.
26187

27188
---
28189

@@ -34,15 +195,15 @@ They are only used inside of classes.
34195

35196
### --feedback--
36197

37-
Refer to the beginning of the video for the answer.
198+
Refer to the beginning of the lecture for the answer.
38199

39200
---
40201

41202
They are rarely used in Python programs.
42203

43204
### --feedback--
44205

45-
Refer to the beginning of the video for the answer.
206+
Refer to the beginning of the lecture for the answer.
46207

47208
## --video-solution--
48209

@@ -66,23 +227,23 @@ cities = ['Los Angeles', 'London', 'Tokyo']
66227

67228
### --feedback--
68229

69-
Refer to the beginning of the video for the answer.
230+
Refer to the beginning of the lecture for the answer.
70231

71232
---
72233

73234
`cities[0]`
74235

75236
### --feedback--
76237

77-
Refer to the beginning of the video for the answer.
238+
Refer to the beginning of the lecture for the answer.
78239

79240
---
80241

81242
`cities[-1]`
82243

83244
### --feedback--
84245

85-
Refer to the beginning of the video for the answer.
246+
Refer to the beginning of the lecture for the answer.
86247

87248
## --video-solution--
88249

0 commit comments

Comments
 (0)