DEV Community

Ugbem Job
Ugbem Job

Posted on • Updated on

Day 8: Python Modules and Packages

What I learnt

  • Modules
  • Built-in modules
  • How to import modules
  • Python Package

Modules

Modules are files containing Python code, for example: example.py, is called a module, and its module name would be example.
They can define functions, classes, and variables, and can also include runnable code. Modules help us break down large programs into small manageable and organized files.

Built-in modules

Python comes with a large standard library of packages that provide a wide range of functionality, such as os for working with the operating system, datetime for working with dates and times, and re for regular expressions.

Additionally, there are many third-party packages available on the Python Package Index (PyPI) that you can install and use in your Python programs.

The math module is a built-in module that contains many mathematical functions such as sqrt(), pow(), sin(), cos(), tan() etc. We can use these functions to perform calculations on numbers.

How to import modules

To use a module in your Python program, you first need to import it using the import statement. Here's an example:

import math
# or
from math import sqrt, pow
Enter fullscreen mode Exit fullscreen mode
import math
x = math.sqrt(25)
print(x)  # Output: 5.0
Enter fullscreen mode Exit fullscreen mode

In the above example, we are importing the math module, which provides a number of mathematical functions. We then use the sqrt() function from the math module to calculate the square root of 25.

You can also import specific functions or variables from a module using the from statement. Here's an example:

from datetime import date

today = date.today()
print("Today's date:", today) # Output: Today's date: 2023-02-17
Enter fullscreen mode Exit fullscreen mode

In this example, we are importing the date class from the datetime module, which provides classes for working with dates and times. We then create an instance of the date class using the today() method and print the current date.

You can also create your own modules in Python. To do this, you simply create a new file with a .py extension and define your variables, functions, and classes. Here's an example:

my_module.py

def greet(name):
print("Hello, " + name + "!")

def square(x):
return x*x

Enter fullscreen mode Exit fullscreen mode

To use this module in another Python code, you can simply import it using the import statement. Here's an example:

import my_module

my_module.greet("Jobizil") # Output: Hello, Jobizil!

print(my_module.square(5)) # Output: 25
Enter fullscreen mode Exit fullscreen mode

In this example, we are importing the my_module module, which contains the greet() and square() functions.
We then use the greet() function to greet Jobizil and the square() function to calculate the square of 5.

Python Package

In Python, a package is a collection of modules that are organized into a directory hierarchy. A package can contain sub-packages as well as modules, and it is used to group related functionality together.

To create a package, you simply create a directory and include an empty __init.__py file in it. This file tells Python that the directory should be treated as a package.

Here's an example of a simple package:

my_package/
    __init__.py
    module1.py
    module2.py
Enter fullscreen mode Exit fullscreen mode

In this example, the my_package directory is a package that contains two modules, module1 and module2. The __init__.py file is required to make the my_package directory a package.

To use a module from a package, you first need to import the package using the import statement, and then import the module using dot notation. Here's an example:

import my_package.module1

my_package.module1.my_function()
Enter fullscreen mode Exit fullscreen mode

In this example, we are importing themy_package package, and then importing the my_function() function from the module1 module.

You can also import a module from a package using the from statement. Here's an example:

from my_package import module2

module2.my_function()
Enter fullscreen mode Exit fullscreen mode

In this example, we are importing the module2 module from the my_package package, and then calling the my_function() function.

Conclusion

In this article, we learned about modules, built-in modules, how to import modules, and Python packages. We also learned how to create our own modules and packages.

Resources

Please contact me through Twitter or LinkedIn if you have any questions or recommendations.

Additionally, do leave a comment below if you have any queries or recommendations. I'd love to hear from you, and please share this post with your friends and subscribe to Dev.to for more articles like this.
Thank you for taking the time to read this.

Top comments (0)