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 1 .
Note5.29.1.
If you work in a group, have only one member of the group fill in the answers on this page. You will be able to share your answers with the group at the bottom of the page.
Learning Objectives
Students will know and be able to do the following.
Content Objectives:
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.
Process Objectives:
Predict the output of code with lists (Information Processing)
Write code using the slice operator (Assessment)
Subsection5.29.1List Indexing
A list holds items in order and you can get the value at an index, just like you can with strings.
Checkpoint5.29.2.
Q-1: What is the last thing that will be printed when the code below runs?
Checkpoint5.29.3.
Run this code to see what it prints.
Checkpoint5.29.4.
Run this code to see what it prints.
Checkpoint5.29.5.
Q-4: What characters are used to indicate the start and end of a list?
Checkpoint5.29.6.
Q-5: What index is used to get the first item in a list?
Checkpoint5.29.7.
Q-6: What negative index is used to get the last item in a list?
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.
Checkpoint5.29.9.
Q-8: Describe in your own words how negative indices work.
Subsection5.29.2Built-in Functions That Work on Lists
There are several built-in functions in Python that work on lists.
Checkpoint5.29.10.
Run this code to see what it prints.
Checkpoint5.29.11.
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.
Checkpoint5.29.12.
Drag each built-in function name to what it does.
Read the chapter on functions and try again.
len
Returns the length of an object (the number of items in the list).
sum
Returns the total of the values in the list.
min
Returns the minimum value in the list.
max
Returns the maximum value in the list.
Subsection5.29.3List Methods
Lists are objects of the list class and have methods that operate on list objects.
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.
Checkpoint5.29.25.
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.
<div class="runestone sqcontainer %(optclass)s"> <div data-component="groupsub" id=func_list_groupsub data-size_limit=4> <div class="col-sm-6"> <select id="assignment_group" multiple class="assignment_partner_select" style="width: 100%"> </select> </div> <div id="groupsub_button" class="col-sm-6"> </div> <p>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.</p> </div> </div>