Get startedGet started for free

Modules

1. Modules

We know how useful Python's built-in functions are. Now let's look at another helpful component of the Python ecosystem - modules.

2. What are modules?

Modules are Python scripts, meaning they are files ending with the `.py` extension that contain reusable code. Modules contain functions and attributes, which we cover in this video. In larger projects, modules can also contain other modules! Like built-in functions, modules help us avoid rewriting code that already exists.

3. Python modules

Python comes with around 200 built-in modules. In this video, we'll cover two of the most popular: `os` and `string`. The `os` module lets us interact with our operating system. We can check the current working directory, list available files, access environment variables, and more. The `string` module provides predefined character sets that simplify common text processing tasks.

4. Importing a module

To use code from a module, we need to import it first. We use the `import` keyword followed by the module name. Here, we import the `os` module. Checking the type confirms that `os` is a module.

5. Finding a module's functions

Once a module is imported, how do we know what's inside? The easiest approach is to check the documentation. We can also use the built-in `help()` function, which shows how to use a module and what it contains, though the output can be quite lengthy.

6. Getting the current working directory

To use a function from the `os` module, we write os-dot, followed by the function name. For example, calling `os.getcwd()` retrieves the current working directory - that's the folder where our Python script is running. The output shows the full directory path, which is the complete location on our computer, including parent directories or folders that contain our current folder. Let's save this result to a variable called `work_dir`. This is useful when we need to reference the directory later in our script, such as when reading multiple files from the same location.

7. Changing directory

We can change directories using `os.chdir()`, passing in the path we want to move to. Calling `os.getcwd()` again confirms we've successfully changed locations. As expected, our `work_dir` variable still contains the old path, which we might need to reference later.

8. Module attributes

The `os` module also has attributes, which are values rather than functions that perform tasks. For example, `os.environ` gives us information about our local environment. Since it's an attribute, we don't include parentheses - we're not calling it like a function. This returns a dictionary containing values like where Python is installed and our current timezone.

9. String module

Now let's look at the `string` module. After importing it, we can access useful attributes for text validation. When building apps, we often need to check if a string contains letters, numbers, or specific characters. The `ascii_lowercase` attribute returns all lowercase letters. The `digits` attribute returns numbers from 0 to 9. And `punctuation` returns all special characters. These attributes are especially handy when validating that user input matches a specific format, like checking if a password contains the required character types. Later in the course, we'll use those attributes to create a password validator.

10. Let's practice!

Now it's your turn to work with modules!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.