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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>2. Lists and Dictionaries</h1>\n",
"<h2>10/02/23</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>2.0 Last Time...</h2>\n",
"\n",
"<ul>\n",
" <li>Python has a variety of data types, many of which we saw today!</li>\n",
" <li>An <b>integer</b> variable is a number with no decimal point.</li>\n",
" <li>A <b>floating point</b> variable is a number with a decimal point</li>\n",
" <li>A <b>string</b> variable is text.</li>\n",
" <li>A <b>Boolean</b> variable can take on the values <b>True</b> and <b>False</b>.</li>\n",
" <li>A <b>NoneType</b> variable can take on the value <b>None</b>.</li>\n",
" <li>Python uses dynamic typing, which means you don't need to define a variable's type when you first use that variable.</li>\n",
" <li>The type() command lets you see what type a given variable is. int(), float(), and str() can be used to convert to different variable types.</li>\n",
" <li>Combining a float and an integer results in a float.</li>\n",
" <li>Combining a string and an integer or float results in a TypeError.</li>\n",
" <li>Floats are often prone to rounding errors.</li>\n",
" <li><b>and</b> and <b>or</b> can be used to combine Boolean variables.</li>\n",
"</ul>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>2.1 Lists and Tuples</h2>\n",
"\n",
"A <b>list</b> is simply an ordered sequence of values, similar to arrays in other programming languages. Each element of a list can be assigned to anything (you can have a mix of variable types within a given list), including another list.\n",
"\n",
"Lists are defined using square brackets (\"[]\"), and individual elements have commas between them.\n",
"\n",
"Counting in Python starts with <b>0</b>. This can lead to some confusion, but always remember that the first element in a list is actually element 0, the second is actually element 1, and so on.\n",
"\n",
"You can also count from the end, so that element <b>-1</b> is actually the last element in a list, <b>-2</b> is the second-last element, and so on.\n",
"\n",
"Finally, to find the length of a list, use the len() command."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create a complicated list that has a list as one of its elements.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# As a side note, if you want to split a long line of code across\n",
"# multiple lines, just use a backslash with nothing after it:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Find the length of this list.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# To get a value from a list within a list:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Likewise, you can make use of the reverse numbering:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A list can be sliced to just obtain a subsection using a colon to separate the lower and upper limits. The lower limit of the range is <b>inclusive</b> and the upper range is <b>exclusive</b>, so that a [1:3] subset will consist of the second and third elements in a list."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Values within a list can be changed; lists are <b>mutable</b>. Changing by assignment is the most basic way to do this."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Let's change 'hello' to 'goodbye' in the original list.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lists in python actually have some built-in functions called <b>methods</b> that can change the properties of the list. We'll talk about these more when we get into object-oriented programming later in this course, but for now, we'll consider <b>insert</b>, <b>remove</b> and <b>append</b>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 'Insert' places the requested element into the list \n",
"# before the specified element of the list.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 'Remove' gets rid of the first occurrence of the specified element.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Append adds the specified element to the end of the list.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To an extent, you can treat an individual string as a list."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A <b>tuple</b> is simply a list that cannot be changed (i.e., <b>immutable</b>), and an error will be returned by Python if you do attempt to change an element within a tuple. Defining a tuple is exactly the same as defining a list, except that you use parentheses instead of square brackets."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create a simple tuple:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# But note that you cannot change a value within a tuple.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>2.2 Dictionaries</h2>\n",
"\n",
"A dictionary, as opposed to a list/tuple, is an <b>unordered</b> list: elements are referenced by <b>keys</b>, not position. A key can be anything, as can the value it refers to.\n",
"\n",
"A dictionary is delimited by curly braces (\"{}\"), and each element is a key:value pair separated by a colon. In order to refer to a particular element in a dictionary, we proceed much as we do with a list, except using a key instead of an element address."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Let's create a dictionary similar to our list from earlier.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Here, our keys are 'value_a','value_b',and 'value_c', and we can refer to \n",
"# each of them accordingly.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Just like lists, dictionaries have <b>methods</b> - and again, we'll go over this in more detail as we get to the object-oriented programming part of this course, but for now, consider <b>keys</b> and <b>values</b>."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# \"Keys\" returns a list of all the keys of a.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# \"Values\" returns a list of all the values of a.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# To check whether a particular key is used in a dictionary, use the following\n",
"# syntax:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that <b>keys</b> and <b>values</b> will return key:value pairs in no particular order. To make sure one corresponds to the other, you can first sort the dictionary into a desired order using something like <b>sorted</b>, which we'll explore soon."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>TAKE-HOME POINTS</h1>\n",
"\n",
"<ul>\n",
" <li>A list is a <b>mutable</b>, <b>ordered</b> sequence of elements.</li>\n",
" <li>Python counts starting with 0, not 1.</li>\n",
" <li>You can also count backwards from the end of a list using negative numbers.</li>\n",
" <li>A list can be sliced, where the slice is defined by a range that has an inclusive lower bound and an exclusive upper bound.</li>\n",
" <li>Values in a list can be changed by assignment or by particular methods.</li>\n",
" <li>A tuple is an <b>immutable</b>, <b>ordered</b> sequence of elements.</li>\n",
" <li>A dictionary is a <b>mutable</b>, <b>unordered</b> sequence of elements consisting of key:value pairs.</li>\n",
"</ul>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|