-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyListstest.txt
More file actions
383 lines (253 loc) · 12.2 KB
/
PyListstest.txt
File metadata and controls
383 lines (253 loc) · 12.2 KB
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
381
382
383
Lists
Lists are another type of object in Python. They are used to store an indexed list of items.
A list is created using square brackets with commas separating items.
The certain item in the list can be accessed by using its index in square brackets.
For example:
words = ["Hello", "world", "!"]
print(words[0])
print(words[1])
print(words[2])
Result:
>>>
Hello
world
!
>>>
The first list item's index is 0, rather than 1, as might be expected.
Lists
An empty list is created with an empty pair of square brackets.
empty_list = []
print(empty_list)
Result:
>>>
[]
>>>
Most of the time, a comma won't follow the last item in a list. However,
it is perfectly valid to place one there, and it is encouraged in some cases.
Lists
Typically, a list will contain items of a single item type, but it is also
possible to include several different types.
Lists can also be nested within other lists.
number = 3
things = ["string", 0, [1, 2, number], 4.56]
print(things[1])
print(things[2])
print(things[2][2])
Result:
>>>
0
[1, 2, 3]
3
>>>
Lists of lists are often used to represent 2D grids, as Python lacks
the multidimensional arrays that would be used for this in other languages.
Lists
Indexing out of the bounds of possible list values causes an IndexError.
Some types, such as strings, can be indexed like lists. Indexing strings
behaves as though you are indexing a list containing each character in the
string.
For other types, such as integers, indexing them isn't possible, and it causes
a TypeError.
str = "Hello world!"
print(str[6])
Result:
>>>
w
>>>
List Operations
The item at a certain index in a list can be reassigned.
For example:
nums = [7, 7, 7, 7, 7]
nums[2] = 5
print(nums)
Result:
>>>
[7, 7, 5, 7, 7]
>>>
List Operations
Lists can be added and multiplied in the same way as strings.
For example:
nums = [1, 2, 3]
print(nums + [4, 5, 6])
print(nums * 3)
Result:
>>>
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>>
Lists and strings are similar in many ways -
strings can be thought of as
lists of characters that can't be changed.
3/4
List Operations
To check if an item is in a list, the in operator can be used.
It returns True if the item occurs one or more times in the list,
and False if it doesn't.
words = ["spam", "egg", "spam", "sausage"]
print("spam" in words)
print("egg" in words)
print("tomato" in words)
Try It Yourself
Result:
>>>
True
True
False
>>>
The in operator is also used to determine whether or not a string is a
substring of another string.
print("wor" in "words")
print("or" in "words")
print("nor" in "words")
Result:
>>>
True
True
False
>>>
List Operations
To check if an item is not in a list,
you can use the not operator in one of the following ways:
nums = [1, 2, 3]
print(not 4 in nums)
print(4 not in nums)
print(not 3 in nums)
print(3 not in nums)
print("55" not in "5566")
print("55" not in "6666")
print("on" not in "goon")
print("so" not in "goon")
Result:
>>>
True
True
False
False
False
True
False
True
>>>
List Functions
Another way of altering lists is using
the append method. This adds an item to the end of an existing list.
nums = [1, 2, 3]
nums.append(4)
print(nums)
nums.append(5)
print(nums)
nums.append(6,)
print(nums)
words = ["hello"]
words.append("world")
print(words)
print(words[1])
Result:
>>>
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
['hello', 'world']
world
>>>
The dot before append is there because it is a method of the
list class.
Methods will be explained in a later lesson.
List Functions
To get the number of items in a list, you can use the len function.
nums = [1, 3, 5, 2, 4]
print(len(nums))
letters = ["a", "b", "c"]
letters.append("d")
print(len(letters))
Result:
>>>
5
4
>>>
Unlike append, len is a normal function, rather than a method.
This means it is written before the list it is being called on,
without a dot.
List Functions
The insert method is similar to append, except that it allows you to
insert a new item at any position in the list, as opposed to just at the end.
words = ["Python", "fun"]
index = 1
words.insert(index, "is")
print(words)
words = ["CPython", "fun"]
words.insert(3, "is")
print(words)
words.insert(2, "C/C++")
print(words)
words.insert(index, "and")
print(words)
nums = [9, 8, 7, 6, 5]
nums.append(4)
nums.insert(2, 11)
print(len(nums))
Result:
>>>
['Python', 'is', 'fun']
['CPython', 'fun', 'is']
['CPython', 'fun', 'C/C++', 'is']
['CPython', 'and', 'fun', 'C/C++', 'is']
7
>>>
List Functions
The index method finds the first occurrence of a list item and returns its index.
If the item isn't in the list, it raises a ValueError.
letters = ['p', 'q', 'r', 's', 'p', 'u']
print(letters.index('r'))
print(letters.index('p'))
print(letters.index('z'))
Result:
>>>
2
0
ValueError: 'z' is not in list
>>>
There are a few more useful functions and methods for lists.
max(list): Returns the list item with the maximum value
min(list): Returns the list item with minimum value
list.count(obj): Returns a count of how many times an item occurs in a list
list.remove(obj): Removes an object from a list
list.reverse(): Reverses objects in a list
letters = ['p', 'q', 'r', 's', 'p', 'u', 9, "9", 0, .0, 7.0, 7, "7"]
print(letters.index('r'))
print(letters.index('p'))
print(letters.index('9'))
print(letters)
print(letters.count(7))
print(letters.remove(0))
print(letters)
print(letters.reverse())
print(letters)
nums=[1,2,0,.0,7,7.0,4/2]
print(nums)
print(max(nums))
print(nums)
print(min(nums))
Result:
>>>
2
0
7
['p', 'q', 'r', 's', 'p', 'u', 9, '9', 0, 0.0, 7.0, 7, '7']
2
None
['p', 'q', 'r', 's', 'p', 'u', 9, '9', 0.0, 7.0, 7, '7']
None
['7', 7, 7.0, 0.0, '9', 9, 'u', 'p', 's', 'r', 'q', 'p']
[1, 2, 0, 0.0, 7, 7.0, 2.0]
7
[1, 2, 0, 0.0, 7, 7.0, 2.0]
0
>>>
list = [1, 1, 2, 3, 5, 8, 13]
print(list[list[4]])
Result
>>>
8
>>>