Skip to main content
Contents Index
Search Book
Search Results:
No results.
Readability settings Prev Up Next Scratch ActiveCode Profile
title here
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Worksheet 5.30 Group Work: Functions and Lists
It is best to use a POGIL approach with the following. In POGIL students work in groups on activities and each member has an assigned role. For more information see
https://cspogil.org/Home .
Section 5.30.1 Learning Objectives
Students will know and be able to do the following.
Use positive and negative indices to access elements of a list.
Identify the purpose of common list methods and common methods that take lists as parameters
Use the slice operator to copy parts of a list.
Predict the output of code with lists (Information Processing)
Write code using the slice operator (Assessment)
Subsection 5.30.1.1 List Indexing
A list holds items in order and you can get the value at an index, just like you can with strings.
Activity 5.30.1 .
Activity 5.30.2 .
Run this code to see what it prints.
Activity 5.30.3 .
Run this code to see what it prints.
Activity 5.30.4 .
Activity 5.30.5 .
Activity 5.30.6 .
What will the following code print?
def list_get(lst):
return lst[-2]
l = ["hi", 3, 'buy', 4]
print(list_get(l))
hi
This would be true if it was returning the item at index 0 or -4.
3
This would be true if it was returning the item at index 1 or -3.
buy
This is returning the second to the last item, the one at index -2.
4
This would be true if it was returning the item at index 3 or -1.
Nothing, there will be an error.
This code will run without any errors.
Activity 5.30.7 .
Describe in your own words how negative indices work.
Subsection 5.30.1.2 Built-in Functions That Work on Lists
There are several built-in functions in Python that work on lists.
Activity 5.30.8 .
Run this code to see what it prints.
Activity 5.30.9 .
Write a function
avg_with_drop that takes a list,
num_list 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,
avg_with_drop([1,2,3,4]) should return
2.5.
Subsection 5.30.1.3 List Methods
Lists are objects of the
list class and have methods that operate on list objects.
Activity 5.30.10 .
Run this code to see what it prints.
Activity 5.30.11 .
Activity 5.30.12 .
What would the following code print?
def list_trans(lst):
lst.append(3)
lst.pop(2)
return lst
l1 = [2, 5, 7]
print(list_trans(l1))
[2, 5, 7, 3]
This is what the list looks like before the pop executes.
[5, 7, 3]
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.
[2, 7, 3]
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.
[2, 5, 7]
This would be true if pop removed the last item, but it removes the one at index 2.
[2, 5, 3]
Correct. This adds 3 at the end and then removes the item at index 2.
Activity 5.30.13 .
Run this code to see what it prints.
Activity 5.30.14 .
What is the last thing the following code prints?
def list_trans(lst):
r = lst.reverse()
print(lst)
print(r)
l1 = [2, 5, 7]
list_trans(l1)
None
It prints the return value from the reverse method which is None.
[2, 5, 7]
This would be true if it printed the value of
[7, 5, 2]]
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.
Nothing, there will be an error.
This would be true if pop removed the last item, but it removes the one at index 2.
Activity 5.30.15 .
Subsection 5.30.1.4 Using the Slice Operator
You can use the slice operator[n:m] with lists to get a new list just like you can with strings.
Activity 5.30.16 .
Run this code to see what it prints.
Activity 5.30.17 .
Activity 5.30.18 .
What does the following code print?
alist = [1, 2, 3, 4, 5]
l2 = alist[-3: -1]
print(l2)
[2, 3, 4, 5]
It returns items starting from the 3rd from the right and ending before the last.
[2, 3, 4]
It returns items starting from the 3rd from the right and ending before the last.
[3, 4, 5]
It returns items starting from the 3rd from the right and ending before the last.
[3, 4]
It returns items starting from the 3rd from the right and ending before the last.
Nothing, there will be an error.
The code will run withtout an error.
Activity 5.30.19 .
Write a function
first_half that takes a list and returns a new list (use the slice operator) with just the items from the first half of the original list. For example, first_half([1,2,3,4]) would return [1, 2] and first_half([7,8,9]) should return [7].
If you worked in a group, you can copy the answers from this page to the other group members. Select the group members below and click the button to share the answers.
You have attempted
of
activities on this page.
The Submit Group button will submit the answer for each each question on this page for each member of your group. It also logs you as the official group submitter.