- Lab
- A Cloud Guru
Creating a Python Module
Being able to reuse code is incredibly useful, but to make our code even more useful, we need to bundle it up so that it can be used from other programs. The primary way this is done in Python is by using modules. In this hands-on lab, we'll define and use our custom module and built-in modules to ensure the `using_modules.py` file can execute properly. The following are prerequisites for feeling comfortable completing this lab: - Creating and importing modules. Watch the "Creating and Using Python Module" video from the Certified Associate in Python Programming Certification course. - Using different import styles. Watch the "Using Imports" video from the Certified Associate in Python Programming Certification course.
Path Info
Table of Contents
-
Challenge
Import the `math` Module within `using_modules.py`
To get started, let's run the
using_modules.py
file to see what our first issue is.$ python3.7 using_modules.py Traceback (most recent call last): File "using_modules.py", line 7, in <module> assert math.ceil(14.11) == 15, f"Expected 15, but got {math.ceil(14.11)}" NameError: name 'math' is not defined
We're attempting to use the
math
module, but it has not been imported. Let's import themath
module now.~/using_modules.py
# 1) Import the built-in `math` module import math # 2) Import the `reverse` and `str_shuffle` as `shuffle` from the custom `strhelpers` module (Needs to be created) name = "Kevin Bacon" assert math.ceil(14.11) == 15, f"Expected 15, but got {math.ceil(14.11)}" assert ( reverse(name) == "nocaB niveK" ), f"Expected 'nocaB niveK', but got {reverse(name)}" assert type(shuffle(name)) == str, f"Expected a string, but got {type(shuffle(name))}" assert sorted(shuffle(name)) == sorted( name ), f"Expected [' ', 'B', 'K', 'a', 'c', 'e', 'i', 'n', 'n', 'o', 'v'], but got {sorted(shuffle(name))}"
We've imported the whole
math
module, so now we should be able to run the file and see a different error.$ python3.7 using_modules.py Traceback (most recent call last): File "using_modules.py", line 10, in <module> reverse(name) == "nocaB niveK" NameError: name 'reverse' is not defined
-
Challenge
Create the `strhelpers` Module and Implement the `reverse` Function
We're going to be creating a module called
strhelpers
that will contain two functions we need:reverse
andstr_shuffle
. For now, we're going to just focus on thereverse
function. From the previous error, we saw this function needs to take a string and return that string in reverse. Let's create our module by creating astrhelper.py
file right next to theusing_modules.py
file, and then define areverse
function.~/strhelpers.py
def reverse(str_value): return str_value[::-1]
We've used string slicing with a negative step value to reverse the string. Now we need to import this function from this module using a
from ... import
statement.~/using_modules.py
# 1) Import the built-in `math` module import math # 2) Import the `reverse` and `str_shuffle` as `shuffle` from the custom `strhelpers` module (Needs to be created) from strhelpers import reverse name = "Kevin Bacon" assert math.ceil(14.11) == 15, f"Expected 15, but got {math.ceil(14.11)}" assert ( reverse(name) == "nocaB niveK" ), f"Expected 'nocaB niveK', but got {reverse(name)}" assert type(shuffle(name)) == str, f"Expected a string, but got {type(shuffle(name))}" assert sorted(shuffle(name)) == sorted( name ), f"Expected [' ', 'B', 'K', 'a', 'c', 'e', 'i', 'n', 'n', 'o', 'v'], but got {sorted(shuffle(name))}"
Running this again, we should see a new error.
$ python3.7 using_modules.py Traceback (most recent call last): File "using_modules.py", line 13, in <module> assert type(shuffle(name)) == str, f"Expected a string, but got {type(shuffle(name))}" NameError: name 'shuffle' is not defined
-
Challenge
Implement `str_shuffle` and Import It as `shuffle`
Our final task is to implement a function called
str_shuffle
in thestrhelpers
module. This function needs to take a string and return a shuffled version of it. Thankfully, the standard library includes a way to shuffle a list, and we can convert strings to lists and back again. Let's implement thestr_shuffle
function using therandom.shuffle
function.~/strhelpers.py
from random import shuffle as l_shuffle def reverse(str_value): return str_value[::-1] def str_shuffle(str_value): str_list = list(str_value) l_shuffle(str_list) return "".join(str_list)
A thing to note here is that
random.shuffle
doesn't return a new list. It modifies the existing list. Next, let's import this function intousing_modules.py
, giving it the identifiershuffle
.~/using_modules.py
# 1) Import the built-in `math` module import math # 2) Import the `reverse` and `str_shuffle` as `shuffle` from the custom `strhelpers` module (Needs to be created) from strhelpers import reverse, str_shuffle as shuffle name = "Kevin Bacon" assert math.ceil(14.11) == 15, f"Expected 15, but got {math.ceil(14.11)}" assert ( reverse(name) == "nocaB niveK" ), f"Expected 'nocaB niveK', but got {reverse(name)}" assert type(shuffle(name)) == str, f"Expected a string, but got {type(shuffle(name))}" assert sorted(shuffle(name)) == sorted( name ), f"Expected [' ', 'B', 'K', 'a', 'c', 'e', 'i', 'n', 'n', 'o', 'v'], but got {sorted(shuffle(name))}"
Finally, let's run
using_modules.py
one last time to ensure there are no more errors.$ python3.7 using_modules.py $
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.