- Author

- Name
- Nelson Silva
- Social
Introduction
Modules in Python are one of the features that make this language one of the most flexible and powerful. Through modules, you can extend the base functionality of the language, using existing libraries and frameworks or creating your own.
Understanding Modules
A module in Python is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.
Advantages of using Modules:
- Code Reusability: You can use the code from a module in multiple programs without having to copy it.
- Code Organisation: Modules help organise related functions, classes, and variables in a logical way.
- Namespace: Modules create their own isolated namespace, avoiding name conflicts.
How to Import Modules
There are several ways to import modules in Python, each suited for different situations:
import math
print(math.sqrt(16)) # 4.0
from math import sqrt
print(sqrt(16)) # 4.0
from math import *
print(pi) # 3.141592653589793
Using dir()
The dir() function is a powerful tool that provides a list of available attributes in a module.
import math
print(dir(math)) # Returns all attributes and functions of the math module
Conclusion
Modules are essential for any Python programmer, whether to take advantage of the vast libraries available or to organise your own code. With them, Python becomes even more adaptable, allowing you to create and maintain complex projects with efficiency and clarity.