Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.
Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.
For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.
It makes sense to start learning by reading and watching videos about fundamentals and how things work.
Data Science and Machine Learning - 16 wks
Full-Stack Software Developer - 16w
Search from all Lessons
Curated list of small interactive and incremental exercises you can take to get better at any coding skill.
Curated section of projects to build while learning with simple instructions, videos, solutions, and more.
Guides on different topics related to the technologies that we teach in our courses
In this article, we will enter the exciting world of modules in Python. You will discover what modules are and how to use them to empower your programs: get ready to expand your horizons and take your programming skills to the next level! If you want to know a little more about what is Python? or its main uses, you can find a lot of information on the 4Geeks blog.
Next, we will see a small example of how to use modules in a Python script.
As you can see in the example, in the module
math_operations.py we have several functions that allow us to perform mathematical operations. Then in module
main.py we import these functions and invoke them. This is a small example of how to create and import modules in Python.
Modules in Python are one of the most powerful and versatile features of this language. They allow you to organize your code into reusable logical units, simplifying the development and maintenance of programs. Imagine them as magic boxes full of tools and functionalities ready to be used in your programs. Modules allow you to organize and divide your code into logical units, making it easy to maintain and reuse. They are like superpowers that take your programs to the next level! 💫🔧
To use a module in Python, you must first import it into your program. You can do this by using the
import keyword followed by the module name. For example:
This imports the module math, which provides useful mathematical functions, such as trigonometric calculations and advanced mathematical operations. Once imported, you can access the functions and variables defined in the module using the syntax
Modules in Python usually offer a variety of functions and variables that you can use in your programs. You can explore the module's documentation to discover all the possibilities it offers. Here are some examples of popular modules and how to use them.
random module allows you to generate random numbers. You can use the function
random() to obtain a random number between 0 and 1, and you can also use the function
uniform() to obtain a random number between an initial value and a final value. Examples:
random() function does not need parameters but function
uniform()needs two parameters, the first parameter is the initial range which indicates that the random number it generates cannot be smaller than this initial value, and the second parameter is the final range which indicates that the random number cannot be larger than this final value.
datetime module allows us to work with dates and times, this module provides us with different methods to work with and modify them.
As you can see in the examples, the method
now() returns the current date and time, this will depend on the country where you use it. The
datetime() method allows you to pass a specific date and returns that date in an appropriate syntax, and the method
today() only returns the current date, it does not return the time. These are just some examples. The module
datetime has many methods that will be useful when working with dates in Python.
os (Operating System) module provides you with functions to interact with the operating system. You can get information about the current directory, create folders, delete files, and much more.
os module has several methods that will help you interact with the operating system. You can create new files and directories, rename existing directories, list the files in a specific directory and much more. The example above shows just a few examples of what you can do with this module. This module offers a wide variety of methods that you can use to interact with your operating system.
There are multiple modules that you can use when working with Python, some of them are installed by default like os (Operating System), re (Regular Expressions), datetime, random, etc..., but some others like Numpy or Pandas libraries are not installed by default, to access these you must install them and then import them with the same syntax
In addition to using Python's built-in modules, you can also create your own custom modules. A custom module is simply a Python file with functions and variables that you can import into other programs. These modules will be especially useful when working on large Python projects such as an API.
To create your own module, follow these steps:
.pyfile with the name of your module, for example,
import module_nameor if the module is inside a folder, use the syntax
from folder_name import module_name.
In this example, we create a function that performs mathematical operations in module
math_operations.py then we import this module into the main module
main.py, in Python you can use the reserved word
as to assign an alias to the modules you import, in this example we use the
MO alias to rename module
math_operations. After importing the module, we create a file that when executed will ask you, in the console, to first enter the type of operation you want to perform with the help of the reserved word
input(), then it will ask you to enter the numbers to perform the operation and once you enter the numbers it will return the result.
Modules are a powerful tool for extending the capabilities of your Python programs. Now that you know the basics and how to use existing modules, you can explore further and create your own custom modules - the programming world is full of endless possibilities!
Code will set you free ✨👨💻