Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Extended Data Types\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Agenda\n",
"\n",
"* Lists (repetition)\n",
"* Tuples\n",
"* Sets\n",
"* Dictionaries"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" ## Introduction\n",
"\n",
"* We already know lists\n",
"* Lists are only one of 4 built-in data types in Phython that store collections of data:\n",
" * Lists\n",
" * Tuples\n",
" * Sets\n",
" * Dictionaries\n",
"* Collections are used to store multiple items in a single variable.\n",
"* Working with collections is something that makes Python extremly powerful\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Recap of what we know about Lists\n",
"\n",
"1. Lists are created with square brackets ```[ ]```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"list = [1,2,5,3]\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. List items are ordered, changeable, and allow duplicate values\n",
" * Orders: Items have a defined order, and that order will not change unless we add or remove items\n",
" * Changable: We can change, add, and remove items in a list after it has been created\n",
"3. We access list items by thier index (the position in the list, starting with 0)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
]
}
],
"source": [
"list = [1,2,3,3,2]\n",
"print(list[3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Accessing List Items\n",
"\n",
"Besides accessing list items with ```[index]``` Python offers some additional options:\n",
"\n",
"1. Negative Indexing: ```[-1]``` refers to the last item, ```[-2]``` refers to the second last item etc.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"d\n",
"c\n"
]
}
],
"source": [
"my_list = ['a','b','c','d']\n",
"print(my_list[-1])\n",
"print(my_list[-2])\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Instead of selecting a single item we can also select a subset of the list with the schema ```start-index:end-index```"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['b', 'c']\n"
]
}
],
"source": [
"my_list = ['a','b','c','d']\n",
"print(my_list[1:3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## List Functions\n",
"\n",
"We have already seen the most important list methods (functions). Here is a complete table:\n",
"\n",
"| Method | Description |\n",
"| :-------- | :--------------------------------------------------------------------------- |\n",
"| append() | Adds an element at the end of the list |\n",
"| clear() | Removes all the elements from the list |\n",
"| copy() | Returns a copy of the list |\n",
"| count() | Returns the number of elements with the specified value |\n",
"| extend() | Add the elements of a list (or any iterable), to the end of the current list |\n",
"| index() | Returns the index of the first element with the specified value |\n",
"| insert() | Adds an element at the specified position |\n",
"| pop() | Removes the element at the specified position |\n",
"| remove() | Removes the item with the specified value |\n",
"| reverse() | Reverses the order of the list |\n",
"| sort() | Sorts the list |\n",
"\n",
"See https://www.w3schools.com/python/python_lists_methods.asp for more details."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tuples\n",
"\n",
"* A tuple is a collection which is ordered and **unchangeable**.\n",
"* Tuples are written with round brackets.\n",
"* Tuples are orded and allow duplicate values.\n",
"* Items of tuples are accessed exactly the same way as lists with ```[]```"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"my_tuple = (\"mia\", \"mio\", 1)\n",
"print(my_tuple[2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Tuples only know the methods ```count()```and ```index()```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sets\n",
"* A set is a collection which is unordered, and unindexed and its itmes are unchangeable.\n",
" * Once a set is created, you cannot change its items, but you can add new items.\n",
" * Dublicate items will be treated as one item\n",
"* Like lists and tuples, sets can contain multiple data types\n",
"* You cannot access items in a set by referring to an index or a key (since they have no order)\n",
"* Sets are written with curly brackets."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"3\n",
"5\n",
"False\n"
]
}
],
"source": [
"my_set = {1, 5, 3, 1} # last value will be ignored\n",
"my_set.add(1) # will be ignored\n",
"for x in my_set:\n",
" print(x)\n",
"print(2 in my_set)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Sets are highly useful to efficiently remove duplicate values from a collection like a list and to perform common math operations like unions and intersections."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Dictionaries"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}