Modules
title: Python Modules description: Learn Programming for Data Science keywords: [python modules, math module, json module, date time module, random module, what is a python module, why use modules] author: Juma Shafara date: "2023-11"

Modules
A module in Python is a python file that can contain variables, functions and classes
Why use Modules?
Modules allow us to split our code into multiple files
Instead of writing all our codes inside a sigle Python file, we can use modules
Note!
That way, our code will be easier to read, understand and maintain
Creating a Module
There is nothing so special with creating a module, simply write you Python code and save it with the .py extension.
In this example, we have a module saved as my_module.py and it contains the following code
After that, to use my_module.py, we need to import it.
To import, use the import statement and the module name.
Then we can use the variables and functions in the module.
In this example, the code below is saved as main_code.py and it imports the module.py.
Using Aliases
We can use an alias to refer to the module
To use an alias, use the as keyword
Importing Parts of a Module
We can choose to import only some specific parts of a module
>Note! When we import a part of a module, we will be able to use its variables and functions directly
Use the from keyword to import a part of a module.
In this example, we will import the first_name variable and access it directly
The dir() Function
The dir() function returns a list of all the variables, functions and classes available in a module
Built in Modules
Python has many useful built-in modules that we can use to make coding easier.
Built-in modules can be imported without having to create them
In this example, we will import the sysconfig module and use its get_python_version() to return the Python version we're using
Math Module
The math module gives us access to mathematical functions
To use the math module, import it first, then we can start using it.
We can use the math module to find the square root of a number using the math.sqrt() method
We can use the math module to get the factorial of a number by using the math.factorial() method
The math module also contains some constants like pi and e
The math module can do those and so much more
Random Module
The random module lets us generate a random number
As usual, to use the random module, import it first.
We can generate a random number that falls within a specified range by using the random.randint() method
We can generate numbers from a gaussian distribution with mean (mu) as 0 and standard deviation (sigma) as 1
Date and Time
The datetime module allows us to work with dates
As usual we have to import the datetime module to be able to use it.
Current Date and Time
The datetime.datetime.now() method returns the current date and time
The date Object
The date object represents a date (year, month and day)
To create a date object, import it from the datetime module first.
JSON
JSON stands for JavaScript Object Notation.
JSON contains data that are sent or received to and from a server
JSON is simply a string, if follows a format similar to a Python dictionary
Example:
JSON to Dictionary
Before we can individually access the data of a JSON, we need to convert it to a Python dictionary first.
To do that, we need to import the json module
Dictionary to JSON
To convert a dictionay to JSON, use the json.dumps() method.
Exercise
Write a program to calculate the factorial of any given positive integer.