Here are our solutions for the day 26 exercises in the 30 Days of Python series. Make sure you try the exercises yourself before checking out the solutions!
1) Define a Movie
tuple using namedtuple
that accepts a title, a director, a release year, and a budget. Prompt the user to provide information for each of these fields and create an instance of the Movie
tuple you defined.
First things first, we need to remember to import namedtuple
from the collections module.
import namedtuple
Now we're going to be able to create our template. In this case the name of our new tuple type and the fields have already been provided for us, so we just need to call namedtuple
with that information.
import namedtuple
Movie = namedtuple("Movie", ["title", "director", "year", "budget"])
Finally, we just need to get some information from the user to create an instance of our new Movie
tuple.
import namedtuple
Movie = namedtuple("Movie", ["title", "director", "year", "budget"])
title = input("What is the title of the movie? ")
director = input(f"Who directed {title}? ")
year = input(f"When was {title} released? ")
budget = input(f"What was the budget for {title}? ")
movie = Movie(title, director, year, budget)
2) Use a defaultdict
to store a count for each character that appears in a given string. Print the most common character in this dictionary.
This exercise is very similar to the inventory exercise from the post.
First we need to import the defaultdict
type from the collections
module. Then we need to create an instance of defaultdict
where the factory function is int
.
At this point I'm also going to create a string containing the word onomatopoeia as a test string.
from collections import defaultdict
s = "onomatopoeia"
letter_count = defaultdict(int)
Next let's iterate over s
and start counting the characters, using the same method we used to increment item quantities before.
from collections import defaultdict
s = "onomatopoeia"
letter_count = defaultdict(int)
for character in s:
letter_count[character] += 1
Now we just need to find the most common letter in this new dictionary by calling max
and passing in some sorting key. In this case, I'm going to use a lambda expression to return the value for each key.
from collections import defaultdict
s = "onomatopoeia"
letter_count = defaultdict(int)
for character in s:
letter_count[character] += 1
most_common_character = max(letter_count, key=lambda key: letter_count[key])
print(most_common_character) # o
As we can see, we get the correct answer: "o"
.
3) Use the mul
function in the operator
module to create a partial
called double
that always provides 2
as the first argument.
In this case we just need to pass in a positional argument when creating our partial
, since we're just interesting in fixing the first argument.
We need to do two imports here. First we need to import mul
from operator
, and second, we need to import partial
from functools
.
from operator import mul
from functools import partial
Creating the partial in this case is fairly simple.
from operator import mul
from functools import partial
double = partial(mul, 2)
4) Create a read
function using a partial
that opens a file in read ("r"
) mode.
The process for creating this partial
is very similar to the on in exercise 3, but this time we need to use a keyword argument.
The parameter name we need to assign to in this case is mode
.
from functools import partial
read = partial(open, mode="r")
We can now use this read
function just like the open
function: it even works in context managers!