Mastering Python Modules: A Comprehensive Tutorial

At the end of this tutorial, you will understand what Python modules are, how to create and use a Python module, how to import and use a module and some Python in-built modules.

 What is a Python module?

A module is a file that contains Python code and usually has a “.py” extension. Modules are used to group related functions, classes, and variables to organise and reuse code. You can easily maintain, share, and reuse your codebase if you organise it into modules. Modules aid in code readability, maintainability, and organisation by breaking down code into smaller, more manageable chunks.

Python has many built-in modules, including os, sys, datetime, and others. This tutorial will teach us how to create and use custom Python modules.

How to create a module?

You can create a Python module by simply creating a new Python file (with a “.py” extension), and writing your functions, classes, or variables inside it.

For instance, let's create a file called my_module.py and add the following code:

Let’s understand the code snippet above.

The greet(name) takes a single argument name and returns a string with the greeting "Hello, {name}!".

The add (a, b) takes two arguments a and b and returns their sum.

 How to use a module in Python?

To use a Python module in another Python script, you first need to import it.

In a new Python file (e.g., main.py), let’s write the following code:

 Let’s explain the code:

We started by importing a custom module named my_module. Next, we set a variable name with the value "Alice". We Called the greet function from my_module with name as the argument and print out the result.

We called the add function from my module with arguments 3 and 4, saved the result in the variable result, and printed it.

 How to import specific functions or variables from a module?

You can also use the from... import... syntax to import specific functions or variables from a module:

 Let’s explain the code:

The greet and add functions from my module are imported.

Sets the variable name "Bob" to its value.

The greet function is called with the argument name and the result is printed.

With arguments 5 and 6, it calls the add function, stores the result in the variable result, and prints it.

Built-in modules in Python

Python has several built-in modules that you can use in your code. Math, random, and datetime are examples of common built-in modules.

 Let’s take a look at the math function.

The math module is used in this code to perform the following mathematical operations.

The square root of 16 is calculated and printed (4.0).

The value of the mathematical constant is printed (3.141592653589793).

Converts 30 degrees to radians, computes the sine value, and outputs the result (approximately 0.5).

Conclusion

In this tutorial, we covered the fundamentals of Python modules, including what they are, how to create and use them, and some commonly used built-in modules. You can improve the maintainability and reusability of your Python projects by organising your code into modules.