Exercises: Collections
The exercises for this chapter can be found in the data-analysis-projects/collections/exercises
directory.
Part 1: Strings
Bracket Notation Basics
The following exercises can be found in the data-analysis-projects/collections/exercises/strings.py
file.
Use bracket notation to:
Print a slice of the first 12 characters from
"Strings_are_sequences_of_characters."
Print a slice of the last 12 characters from the same string. You should NOT have to count the index values yourself!
Print a slice of the middle 12 characters from the same string.
Looping Through a String
Use index values to loop backwards through a string:
First, print one letter per line.
Next, instead of one letter per line, use the accumulator pattern to build up and print the reversed string. For example, if given the string
'good'
, your program printsdoog
.Finally, use concatenation to print the combination of the original and reversed string. For example, given the string
'tomato'
, your program printstomatootamot
. (If you want to be fancy, include the|
character to make the output look almost like a mirrored image:tomato | otamot
).
Part 2: Lists
The following exercises can be found in the data-analysis-projects/collections/exercises/lists.py
file.
Create a list called
adding_practice
with a single entry:273.15
. Use theappend
method to add the following elements to the list one at a time.- 42
- “hello”
Print the list after each step to confirm the changes.
Use
concatenation
to add these three items to the list all at once:[False, -4.6, '87']
.append
,insert
,pop
, andremove
are used to add or remove elements from a list. Bracket notation can be used to modify any element within a list. Starting with thecargo_hold
list,['oxygen tanks', 'space suits', 'parrot', 'instruction manual', 'meal packs', 'slinky', 'security blanket']
write statements to do the following:
- Use bracket notation to replace
'slinky'
in the list with'space tether'
. Print the list to confirm the change. - Remove the last item from the list with
pop
. Print the element removed and the updated list. - Remove the first item from the list with
pop
. Print the element removed and the updated list. append
andinsert
require arguments inside the()
. Add the items1138
and'20 meters'
to the the list—the number at the start and the string at the end. Print the updated list to confirm the changes.remove
the parrot from the cargo hold, then print the updated list.- Use
format()
to print the final contents of the list and its length."The list ___ contains ___ items."
- Use bracket notation to replace
Part 3: Search a Dictionary
The exercises for this portion can be found in the data-analysis-projects/collections/exercises/search-a-dictionary.py
file.
The flavors
dictionary contains entries that pair different ice cream flavors with their cost per scoop. Your job is to do the following:
Set a variable called choice to the flavor you want to search for.
Use an if statement to check if choice is in the flavors dictionary.
If it is, set another variable called cost to the value associated with choice.
If it isn’t, set cost to 0.
Print the cost.
Search a Dictionary Part 2
Initialize two variables: highest_cost to 0 and fanciest to an empty string.
Loop through the flavors dictionary using a for loop.
For each flavor, check if its price is higher than highest_cost.
If it is, update fanciest to this flavor and highest_cost to its price.
After the loop, print the most expensive flavor.
Submitting Your Work
When finished copy the URL to your GitHub repository for the exercises and paste it into the submission box in Canvas for Exercises: Collections and click Submit.