Python Basics: Modules and Packages (Exercises)
00:00 Watching this course and coding along is a great way to learn. If you want to step up your learning experience even more, then I have some review exercises and a challenge for you.
00:10 I will read the exercises and the challenge to you, but you can also find them in the PDF that you can download in the materials below this video. After finishing this course, it’s a good idea to tackle them in the next few days.
00:22
That way, you can strengthen what you learned and come back to a specific lesson of this course when you need a refresher. Here is the first exercise. Create a module called greeter.py
that contains a single function, greet()
.
00:38
This function should accept a single string parameter, name
, and print a text, Hello, {name}!
to the interactive window, with {name}
in the string replaced by the function argument.
00:54
So for example, Hello, Frieda!
or Hello, Carolina!
.
01:01
And here is another exercise. Create a module called main.py
that imports greeter
from greeter.py
, and then calls the function with the argument "Real Python"
as a string.
01:16
And here is a challenge. This challenge is a bit more involved than the review exercises because you should build an entire package yourself. Here are the details: In a project folder called package_exercises/
, create a package called helpers
with three modules: __init__.py
, string.py
, and calc.py
.
01:42
In the string.py
module, add a function called shout()
that takes a single string parameter and returns a new string with all of the letters in uppercase.
01:53
In the calc.py
module, add a function called area()
that takes two parameters called length
and width
, and returns their product, length
multiplied by width
.
02:08
In the package_exercises/
folder, create a module called main.py
that imports the string
and calc
modules.
02:15
Use string.shout()
and calc.area()
to print the following output: THE AREA OF A 5-BY-8 RECTANGLE IS 40
. Or maybe I should scream this part because it’s uppercase? No, I think it’s better for your ears this way.
02:32 The return string should be in uppercase letters. All right, these are two exercises and one challenge that you can do for yourself on your computer. If you are feeling motivated, you can tackle them right away.
02:45 Otherwise, it can make sense to revisit them in a few days. Either way, before you leave, let me wrap up this course in the next lesson.
Become a Member to join the conversation.