- Lab
 - 
                        Libraries: If you want this lab, consider one of these libraries.
 - Cloud
 
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.
Lab Info
Table of Contents
- 
                
                
Challenge
Import the `math` Module within `using_modules.py`
To get started, let's run the
using_modules.pyfile 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 definedWe're attempting to use the
mathmodule, but it has not been imported. Let's import themathmodule 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
mathmodule, 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
strhelpersthat will contain two functions we need:reverseandstr_shuffle. For now, we're going to just focus on thereversefunction. 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.pyfile right next to theusing_modules.pyfile, and then define areversefunction.~/strhelpers.pydef 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 ... importstatement.~/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_shufflein thestrhelpersmodule. 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_shufflefunction using therandom.shufflefunction.~/strhelpers.pyfrom 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.shuffledoesn'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.pyone last time to ensure there are no more errors.$ python3.7 using_modules.py $ 
About the author
Real skill practice before real-world application
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.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.