Skip to content

Commit 2cda124

Browse files
committed
Update funcWithListsAndLoops.ptx
1 parent 721b74d commit 2cda124

1 file changed

Lines changed: 306 additions & 0 deletions

File tree

pretext/functions/funcWithListsAndLoops.ptx

Lines changed: 306 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,312 @@
6161
</ul>
6262
</p>
6363
</introduction>
64+
65+
<subsection xml:id="flal_list-indexing-and-slicing">
66+
<title>List Indexing and Slicing</title>
67+
<p>A list holds items in order and you can get the value at an index, just like you can with strings. You can also
68+
use negative indices, just like you can with strings. You can use the slice operator <c>[start:end]</c> with lists to get a
69+
new list by copying part of a list, just like you can with strings. The new list will include the item at the start index (inclusive) and
70+
all elements up to the end index - 1. </p>
71+
72+
<exercise label="flal_neg_2_result">
73+
<statement>
74+
<p>What is the last thing that the following code prints?</p>
75+
<program language="python">
76+
<input>
77+
def list_get(lst):
78+
print(lst[0])
79+
print(lst[:2])
80+
return lst[-2]
81+
l = ["hi", 3, 'buy', 4]
82+
print(list_get(l))
83+
</input>
84+
</program>
85+
</statement>
86+
<choices>
87+
<choice>
88+
<statement>
89+
<p>hi</p>
90+
</statement>
91+
<feedback>
92+
<p>This would be true if it was returning the item at index 0 or -4.</p>
93+
</feedback>
94+
</choice>
95+
<choice>
96+
<statement>
97+
<p>3</p>
98+
</statement>
99+
<feedback>
100+
<p>This would be true if it was returning the item at index 1 or -3.</p>
101+
</feedback>
102+
</choice>
103+
<choice correct="yes">
104+
<statement>
105+
<p>buy</p>
106+
</statement>
107+
<feedback>
108+
<p>This is returning the second to the last item, the one at index -2.</p>
109+
</feedback>
110+
</choice>
111+
<choice>
112+
<statement>
113+
<p>4</p>
114+
</statement>
115+
<feedback>
116+
<p>This would be true if it was returning the item at index 3 or -1.</p>
117+
</feedback>
118+
</choice>
119+
<choice>
120+
<statement>
121+
<p>Nothing, there will be an error.</p>
122+
</statement>
123+
<feedback>
124+
<p>This code will run without any errors.</p>
125+
</feedback>
126+
</choice>
127+
</choices>
128+
</exercise>
129+
130+
</subsection>
131+
132+
<subsection xml:id="flal_built-in-functions-that-work-on-lists">
133+
<title>Built-in Functions That Work on Lists</title>
134+
<p>There are several built-in functions in Python that work on lists such as <c>max</c>, <c>min</c>, <c>len</c>, and <c>sum</c>.</p>
135+
<exercise label="flal_ll_ac_list_func">
136+
<statement>
137+
<p>Run this code to see what it prints. You can also step through the code using the "Show Codelens" button. </p>
138+
</statement>
139+
<program xml:id="flal_ll_ac_list_func_editor" interactive="activecode" language="python">
140+
<input>
141+
# function definition
142+
def list_func(my_list):
143+
print("max", max(my_list))
144+
print("min", min(my_list))
145+
print("len", len(my_list))
146+
print("sum", sum(my_list))
147+
148+
# function definition
149+
def main():
150+
list_func([1, 2, 3])
151+
list_func([90, 100])
152+
153+
# function call
154+
main()
155+
</input>
156+
</program>
157+
</exercise>
158+
<exercise label="flal_avg_drop_high_and_low">
159+
<statement>
160+
<p>Write a function <c>avg_with_drop</c> that takes a list, <c>num_list</c> and returns the average of the values in the list, but it does not include the highest or lowest value in the average. For example, <c>avg_with_drop([1,2,3,4])</c> should return <c>2.5</c>.</p>
161+
</statement>
162+
<program xml:id="flal_avg_drop_high_and_low_editor" interactive="activecode" language="python">
163+
<input>
164+
def avg_with_drop(num_list):
165+
166+
====
167+
from unittest.gui import TestCaseGui
168+
169+
class myTests(TestCaseGui):
170+
171+
def testOne(self):
172+
self.assertEqual(avg_with_drop([1,2,3,4]), 2.5, 'avg_with_drop([1,2,3,4])')
173+
self.assertEqual(avg_with_drop([2,4,6,8]), 5, 'avg_with_drop([2,4,6,8])')
174+
self.assertEqual(avg_with_drop([10, 80, 100, 60]), 70, 'avg_with_drop([10, 80, 100, 60])')
175+
self.assertEqual(avg_with_drop([-10, 80, 120, 60]), 70, 'avg_with_drop([-10, 80, 120, 60])')
176+
self.assertEqual(avg_with_drop([5, 10, 15, 20]), 12.5, 'avg_with_drop([5, 10, 15, 20])')
177+
178+
myTests().main()
179+
</input>
180+
</program>
181+
</exercise>
182+
</subsection>
183+
184+
<subsection xml:id="flal_list-methods">
185+
<title>List Methods</title>
186+
<p>Lists are objects of the <c>list</c> class and have methods that operate on list objects using dot notation (<c>name.method()</c>) such as <c>append</c>, <c>pop</c>, and <c>extend</c>.</p>
187+
<exercise label="flal_ll_ac_list_methods">
188+
<statement>
189+
<p>Run this code to see what it prints. You can also step through the code using the "Show Codelens" button.</p>
190+
</statement>
191+
<program xml:id="flal_ll_ac_list_methods_editor" interactive="activecode" language="python">
192+
<input>
193+
# function definition
194+
def list_methods(alist):
195+
print(type(alist))
196+
print(alist)
197+
alist.append(3)
198+
print(alist)
199+
alist.append([2])
200+
print(alist)
201+
alist.pop(1)
202+
print(alist)
203+
alist.extend([8, 11])
204+
print(alist)
205+
206+
# function definition
207+
def main():
208+
l1 = [1]
209+
list_methods(l1)
210+
print(l1)
211+
l1 = ['hi', 'bye']
212+
list_methods(l1)
213+
print(l1)
214+
215+
# function call
216+
main()
217+
</input>
218+
</program>
219+
</exercise>
220+
221+
222+
<exercise label="flal_list_append_pop_predict">
223+
<statement>
224+
<p>What would the following code print?</p>
225+
<program language="python">
226+
<input>
227+
def list_trans(lst):
228+
lst.append(3)
229+
lst.pop(2)
230+
return lst
231+
l1 = [2, 5, 7]
232+
print(list_trans(l1))
233+
</input>
234+
</program>
235+
</statement>
236+
<choices>
237+
<choice>
238+
<statement>
239+
<p>[2, 5, 7, 3]</p>
240+
</statement>
241+
<feedback>
242+
<p>This is what the list looks like before the pop executes.</p>
243+
</feedback>
244+
</choice>
245+
<choice>
246+
<statement>
247+
<p>[5, 7, 3]</p>
248+
</statement>
249+
<feedback>
250+
<p>This would be true if pop removed the first value that was passed in, but it takes an index and removes the item at that index.</p>
251+
</feedback>
252+
</choice>
253+
<choice>
254+
<statement>
255+
<p>[2, 7, 3]</p>
256+
</statement>
257+
<feedback>
258+
<p>This would be true if pop removed the item at index 1, but it removes the item at index 2 and the first item is at index 0.</p>
259+
</feedback>
260+
</choice>
261+
<choice>
262+
<statement>
263+
<p>[2, 5, 7]</p>
264+
</statement>
265+
<feedback>
266+
<p>This would be true if pop removed the last item, but it removes the one at index 2.</p>
267+
</feedback>
268+
</choice>
269+
<choice correct="yes">
270+
<statement>
271+
<p>[2, 5, 3]</p>
272+
</statement>
273+
<feedback>
274+
<p>Correct. This adds 3 at the end and then removes the item at index 2.</p>
275+
</feedback>
276+
</choice>
277+
</choices>
278+
</exercise>
279+
280+
<note>
281+
<p>Lists are mutable (changeable). List methods like <c>append</c> and <c>pop</c> change the current list.</p>
282+
</note>
283+
284+
<p>You can also reverse a list or sort it.</p>
285+
286+
<exercise label="flal_ll_ac_list_methods2">
287+
<statement>
288+
<p>Run this code to see what it prints.</p>
289+
</statement>
290+
<program xml:id="flal_ll_ac_list_methods2_editor" interactive="activecode" language="python">
291+
<input>
292+
# function definition
293+
def list_methods2(alist):
294+
print(alist)
295+
alist.reverse()
296+
print(alist)
297+
alist.sort()
298+
print(alist)
299+
alist.sort(reverse = True)
300+
print(alist)
301+
302+
# function definition
303+
def main():
304+
l1 = [-2, 50, -20, 30]
305+
list_methods2(l1)
306+
print(l1)
307+
l1 = ['hi', 'bye', 'apple']
308+
list_methods2(l1)
309+
print(l1)
310+
311+
# function call
312+
main()
313+
</input>
314+
</program>
315+
</exercise>
316+
317+
318+
<exercise label="flal_list_result_of_reverse">
319+
<statement>
320+
<p>What is the last thing the following code prints?</p>
321+
<program language="python">
322+
<input>
323+
def list_trans(lst):
324+
r = lst.reverse()
325+
print(lst)
326+
print(r)
327+
328+
l1 = [2, 5, 7]
329+
list_trans(l1)
330+
</input>
331+
</program>
332+
</statement>
333+
<choices>
334+
<choice correct="yes">
335+
<statement>
336+
<p>None</p>
337+
</statement>
338+
<feedback>
339+
<p>It prints the return value from the reverse method which is None.</p>
340+
</feedback>
341+
</choice>
342+
<choice>
343+
<statement>
344+
<p>[2, 5, 7]</p>
345+
</statement>
346+
<feedback>
347+
<p>This would be true if it printed the value of</p>
348+
</feedback>
349+
</choice>
350+
<choice>
351+
<statement>
352+
<p>[7, 5, 2]]</p>
353+
</statement>
354+
<feedback>
355+
<p>This would be true if pop removed the item at index 1, but it removes the item at index 2 and the first item is at index 0.</p>
356+
</feedback>
357+
</choice>
358+
<choice>
359+
<statement>
360+
<p>Nothing, there will be an error.</p>
361+
</statement>
362+
<feedback>
363+
<p>This would be true if pop removed the last item, but it removes the one at index 2.</p>
364+
</feedback>
365+
</choice>
366+
</choices>
367+
</exercise>
368+
</subsection>
369+
64370
<subsection xml:id="flal_the-for-each-loop">
65371
<title>The For-Each Loop</title>
66372
<p>A for-each loop in Python will loop though the items in a list starting with the item at index 0, then index 1, and so on till the last item in the list.</p>

0 commit comments

Comments
 (0)