One of the benefits of creating functions in a Python program is to introduce modularity, which is the idea of separating the functionality of a program into independent, interchangeable blocks of code. This can make it easier to understand and maintain complex programs. Functions also allow you to reuse code, so you don't have to write the same code over and over again.
The same can be said for modules, which are a grouping of functions, classes, or variables. In this tutorial, I will focus on functions -- but the same concepts apply when it comes to using classes and OOP. I will cover that more in a future tutorial.
They are a way to structure Python code, making it easier to reuse and maintain. In this tutorial, you will learn about:
What modules are and how to use them
How to create and use your own modules
How to use the
import
statement to import modules and specific symbols from modules
Using Modules
Python actually has a set of built-in modules that you can use. You just need to know how to use them! This includes modules for:
Generating random numbers
Performing mathematical calculations
Working with regular expressions
Accessing the system's time
etc.
To use a module, you first need to import it. You can use the import
statement to import a module, like this:
import math
x = math.sqrt(25)
print(x)
# Output:
# 5.0
In this example, we import the math
module and use the sqrt
function from the module to calculate the square root of 25.
Importing in different ways
The above code will let you use all of the functions available in a module as long as you specify the module it is coming from. You can import specific functions from a module like so:
from math import sqrt
x = sqrt(25)
print(x)
# Output:
# 5.0
Or to import multiple functions:
from math import sqrt, pow
x = sqrt(25)
y = pow(2, 5)
print(x)
print(y)
# Output:
# 5.0
# 32.0
If you find yourself needing a lot of functions from a module(or maybe all of them), you can import all of them at once:
from math import *
x = sqrt(25)
y = pow(2, 5)
print(x)
print(y)
# Output:
# 5.0
# 32.0
However, it is generally not recommended to use the import *
syntax because it can make it difficult to tell where a specific function is coming from and you could potentially be using your resources to import a bunch of functions that you will not even be using.
Creating Your Own Modules
You can also create your own modules in Python. To create a module, you just need to save your functions and variables in a file with a .py
extension. Then, you can use the import
statement to use the functions and variables in your module in another Python script.
For example, let's say you have a file my_module.py
with the following code:
def greeting(name):
print("Hello, " + name + "!")
def farewell(name):
print("Goodbye, " + name + "!")
You can then use the import
statement to use the functions in this module in another Python script, like this:
import my_module
my_module.greeting("Alice")
my_module.farewell("Alice")
# Output:
# Hello, Alice!
# Goodbye, Alice!
Aliases
Both modules and functions from modules can be given nicknames or "aliases" in Python if you find that the name that they are currently given is lacking.
To give an alias to a module, you can use the import
statement with the as
keyword, like this:
import math as m
x = m.sqrt(25)
print(x)
In this example, we import the math
module and give it the alias m
. We can then use the m
alias to access the functions in the math
module.
To give an alias to a function from a module, you can use the from
keyword with the import
statement and the as
keyword, like this:
from math import sqrt as sq
x = sq(25)
print(x)
In this example, we import the sqrt
function from the math
module and give it the alias sq
. We can then use the sq
alias to call the sqrt
function.
Aliases can be useful for making your code more concise and easier to read. Just be careful not to choose an alias that could be confused with an existing function or variable in your program.
Conclusion
Modules are an important concept in Python that can help you structure your code, reuse code, and maintain complex programs. Modules are files that contain a group of functions, classes, or variables, and you can use the import
statement to import them into your Python scripts. You can also create your own modules by writing Python code and saving it in a file with a .py
extension.