Section5.37Group Work: Functions with Tuples and Dictionaries
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.37.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:
Learn the differences and similarities between strings, lists, tuples, and dictionaries.
Predict the output of functions with tuples and dictionaries.
Use .get to prevent key errors in dictionaries.
Process Objectives:
Predict the output of functions with tuples and dictionaries.
Subsection5.37.1Tuples
A tuple is like a list in that holds items in order and those items are separated by commas. They can be enclosed in (), but don't have to be.
Checkpoint5.37.2.
Q-1: What is the first thing that will be printed when the code below runs?
Checkpoint5.37.3.
Q-2: What is the last thing that will be printed when the code below runs?
Checkpoint5.37.4.
Run this code to see what it prints.
Checkpoint5.37.5.
Q-4: What characters are used to indicate the start and end of a tuple in the code above?
Checkpoint5.37.6.
Q-5: What index is used to get the first item in a tuple?
Checkpoint5.37.7.
Q-6: What negative index is used to get the last item in a tuple?
Subsection5.37.2Tuples are Immutable
Unlike lists, tuples are immutable (can not change). This makes them more efficient than lists which can change.
Checkpoint5.37.8.
Q-7: Look at the Python code below. What do you think will happen when you run the following code?
Checkpoint5.37.9.
Run this code to see what it prints.
Note5.37.10.
Tuples are immutable (not changeable), so you will get an error if you try to change them.
Some functions that work on lists return an iterator (an object that you can loop through the values of) which you can convert to a list of tuples using the list function. The range function also returns an iterator.
Checkpoint5.37.11.
Q-9: What is the first thing the code below prints?
Checkpoint5.37.12.
Run this code to see what it prints.
Note5.37.13.
The zip function takes two lists and returns an iterator. You can convert this iterator to a list of tuples using the list function. Each tuple has an element from list1 and an element from list2 in order.
Subsection5.37.3Dictionaries
A dictionary stores a value for a key.
Checkpoint5.37.14.
Q-11: What is the first thing the code below prints?
Checkpoint5.37.15.
Run this code to see what it prints.
Checkpoint5.37.16.
Q-13: Look at the Python code below. What do you think will happen when you run the following code?
Checkpoint5.37.17.
Run this code to see what it prints.
There is another way to update the value for a key that works even if the key isn't in the dictionary already.
Checkpoint5.37.18.
Run this code to see what it prints.
Note5.37.19.
The better way to increment a count at a key is to use dict[key] = dict.get(key,0) + 1. This will avoid a key error if the key isn't in the dictionary and the code is shorter.
Checkpoint5.37.20.
Drag the item to its type.
Read this page and try again.
["a", "b"]
List
("a", "b")
Tuple
"ab"
String
{"a": 5, "b": 2}
Dictionary
Checkpoint5.37.21.
Q-17: Which of the following types are immutable (don't change)?
Strings
Yes, Strings are immutable.
Lists
No, Lists can change.
Tuples
Yes, Tuples are immutable.
Dictionaries
No, Dictionaries can change.
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_tuplesanddict_groupsub data-size_limit=3> <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>