5.42. Group Work: Functions with Lists and Loops¶
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.
Note
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:
Understand how list indexing and slicing works.
Gain famililarity with built-in fuctions that take lists as well as list methods.
Recognize a for-each loop.
Recognize a range.
Recognize a for loop.
Recognize a while loop.
Process Objectives:
Predict the output of code with lists (Information Processing)
Write code using the slice operator (Assessment)
Write code using built-in functions that take lists
Predict the outcome of code with lists and loops.
Modify code with loops.
Fix code that processes a list with loops.
5.42.1. List Indexing and Slicing¶
A list holds items in order and you can get the value at an index, just like you can with strings. You can also
use negative indices, just like you can with strings. You can use the slice operator [start:end]
with lists to get a
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
all elements up to the end index - 1.
- 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.
Q-1: What is the last thing that the following code prints?
def list_get(lst):
print(lst[0])
print(lst[:2])
return lst[-2]
l = ["hi", 3, 'buy', 4]
print(list_get(l))
5.42.2. Built-in Functions That Work on Lists¶
There are several built-in functions in Python that work on lists such as max
, min
, len
, and sum
.
Run this code to see what it prints. You can also step through the code using the “Show CodeLens” button.
Write a function sum_first_half
that takes a list and returns a the sum of just the first half of the items.
For example, sum_first_half([1,2,3,4])
should return 3
(sum of 1 and 2) and first_half([7,8,9])
should return 7
.
5.42.3. List Methods¶
Lists are objects of the list
class and have methods that operate on list objects using dot notation (name.method()) such as
append
, pop
, and extend
.
Run this code to see what it prints. You can also step through the code using the “Show CodeLens” button.
- [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.
Q-5: 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))
Note
Lists are mutable (changeable). List methods like append
and pop
change the current list.
You can also reverse
a list or sort
it.
Run this code to see what it prints. You can also step through the code using the “Show CodeLens” button.
- 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 lst before it reversed it.
- [7, 5, 2]
- This would be true if it printed the value of lst after it revsersed it.
- Nothing, there will be an error.
- This would be true if there wasn't a reverse method.
Q-7: What is the last thing that the following code prints?
def list_trans(lst):
r = lst.reverse()
print(lst)
print(r)
l1 = [2, 5, 7]
list_trans(l1)
5.42.4. The For-Each Loop¶
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.
Run this code to see what it prints. You can also step through the code using the “Show CodeLens” button.
5.42.5. Range and For¶
How do you loop just a set number of times? You can use
the built-in range
function to do this.
Run this code to see what it prints. You can also step through the code using the “Show CodeLens” button.
Note
The range(end)
function will produce values from 0 to end - 1.
Run this code to see what it prints. You can also step through the code using the “Show CodeLens” button.
Drag the blocks from the left and put them in the correct order on the right to define a function total_at_odd_indices
that returns the total of the numbers at odd indices in the passed list.
5.42.6. While Loops¶
A while loop repeats while a Boolean expression is True.
Try running the code below. You can also step through the code using the “Show CodeLens” button.
Note
A loop that never ends is called an infinite loop. A while loop should have some way to end. If you have an infinite loop you may need to refresh the page to stop it.
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.
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.