Python Modules are Toolboxes for Programming Lumberjacks

Python Modules are Toolboxes for Programming Lumberjacks

Whether or not you like to press wildflowers, Python modules make coding much easier. If you were building a house, you wouldn’t start by making your own hammer, saw and measuring tape, unless, of course, you were a crazy DIY lumberjack. Instead, you would go to your workshop and pick up your toolbox. The same is true for programming. You could write a program from scratch, and while you would learn a lot, it would take a very long time.

Python ships with a big, invisible workshop. This workshop is called the Standard Library and it’s filled with toolboxes called modules. We used a few of these toolboxes already, such as math and random. Each module is loaded with tools related to a specific task. Carpenters, plumbers, and electricians each have very different tools in their toolboxes. The same is true of modules. Modules are a collection of functions. The math module, for example, is a toolbox filled with math tools.

Python Modules: Toolboxes for Programming Lumberjacks

To access a module, we use the import statement. To see what’s available in a module, we use the help() function.

Try this:

This will present you with NAME, DESCRIPTION, and a list of FUNCTIONS. The functions are listed in alphabetical order. Scroll down to the ‘s’ section. See the function sqrt()?

The help file presents you with an example of how to use the function and a short description of what it will do, just like the functions we wrote.

If you scroll down further, you will see under DATA two predefined variables: e and pi. Cool, right? Type ‘q’ to quit and return to the Python prompt.

We access those predefined variables like this:

And we use our math functions like this:

The Dot

What’s up with that dot? Didn’t we import math? Why do we need to write it out like that?

Importing a module is like placing that toolbox on your workbench. If there’s a specific tool you want to use, you need to tell the interpreter to fetch it from its toolbox. But, if you know which tools you plan to use, you can take them out of the toolbox and put them on the workbench, like this:

And then you can call those tools, without the math prefix, like this:

Whether or not you want to be a programmer or a lumberjack, you now know how to import Python modules from the Standard Library. You can now spend your free time to pressing wildflowers.

Python Modules are Toolboxes for Programming Lumberjacks was posted by on . Jared likes to make things. He really wants you to watch The Hello World Program so you can learn the skills you need to build an awesome future. His contributions to the show include puppetry, 3D animation, doodling and speaking in a bad British accent. And yes, that is a fox sitting on his face.

Discuss this article in the Comments below, or and it with your friends. Learn more about Python by subscribing to The Hello World Newsletter .

Join the Discussion

Subscribe to our Mailing List

Stay up to date on the latest videos and tutorials with our monthly newsletter

The email entered is invalid