5.29. 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.

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.

Learning Objectives

Students will know and be able to do the following.

Content Objectives:

Process Objectives:

5.29.1. List Indexing

A list holds items in order and you can get the value at an index, just like you can with strings.

Run this code to see what it prints.

Run this code to see what it prints.

p9-9: Describe in your own words how negative indices work.

p9-10: What happens if you modify the code above to use an index that is larger than the length of the list? What happens if you modify the code above to use an index that is equal to the length of list? Why does this happen?

5.29.2. Built-in Functions That Work on Lists

There are several built-in functions in Python that work on lists.

Run this code to see what it prints.

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.

5.29.3. List Methods

Lists are objects of the list class and have methods that operate on list objects.

Run this code to see what it prints.

p9-16: Describe in your own words what pop does.

p9-17: Describe in your own words what append does.

p9-18: Describe in your own words what extend does. How is it different from append?

Note

Lists are mutable (changeable). List methods like append and pop change the current list.

Run this code to see what it prints.

5.29.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.

Run this code to see what it prints.

p9-25: In [2:] what is the end index?

Note

The slice operator always returns a new object. It doesn’t change the current object (list or string).

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].

You have attempted of activities on this page