Learning Management Platform for Written Tutorial Series.

Python 3 Modules- Write organized python application

Python 3 Modules- Write organized python application

Python Modules

A module is a python file that contains variables and/or functions and or classes. Modules allow us to group related function into a single file so that we can re-use the module in many different files in our program. For example, let's say we are working on an application that deals with mathematical calculations, we can create a mathemagical module that will group all of the math code together then we can import it into any script that we want.

Topics to be covered

In this tutorial, we will cover the following topics

  • How to create modules
  • How to import modules
  • Built-in modules
  • Python module methods

How to create modules

Let's now create a simple module that we can import into a script and use it.

Create a new file mathemagics.py

Add the following code to mathemagics.py

def sum(x,y):
    return x + y


def product(x,y):
    return x * y

HERE,

  • def sum(x,y): defines a method called sum that accepts two numbers then returns their sum
  • def product(x,y): defines a method called product that accepts two numbers then returns their product

How to import modules

Let's now look at how we can use this module. Create another file called app.py.

Add the following code to it.

import mathemagics

x = 3
y = 7

print (f'The sum of {x} and {y} is {mathemagics.sum(x,y)}')
print (f'The product of {x} and {y} is {mathemagics.product(x,y)}')

HERE,

  • import mathemagics import the mathemagics module into our script
  • x = 3; y = 7 defines two variables x and y and assigns the values 3 and 7 to them respectively.
  • print (f'The sum of {x} and {y} is {mathemagics.sum(x,y)}') prints the sum of x and y by calling the sum function that we created in the mathemagics module.

Executing the above code produces the following results.

The sum of 3 and 7 is 10
The product of 3 and 7 is 21

In the above example, we are prefixing the function calls with the module name. While I personally like the term mathemgics, I refuse to repeatedly type it every time I want to call a function in it. How can we resolve that? Python allows us to import function from the module into our local script and work with them as if they are ordinary functions like so.

from mathemagics import sum, product

x = 3
y = 7

print (f'The sum of {x} and {y} is {sum(x,y)}')
print (f'The product of {x} and {y} is {product(x,y)}')

HERE,

  • from mathemagics import sum, product uses the from keyword to import the functions sum and product.
  • sum(x,y); product(x,y) utilizes the sum and product function without mathemagics.

The alternative way of importing the variables,methods and classes is using the asterisk * like so

from mathemagics import *

The above method is generally not recommended as it has the potential of creating conflicts with the scope of the script where you are importing the module into.

Built-in modules

Python comes with a good number of built-in modules. For example, time is a built-in module that we can use to work with dates and time in python. We can also work with math module that has methods for working with numeric data types.

For more information, you can read the documentation on Python Module Index.

Let's now look at a practical example that utilizes the built-in module Fractions.

from fractions import Fraction

print(Fraction(5, 10))

HERE,

  • from fractions import Fraction imports the Fraction module from the fractions package.
  • print(Fraction(5, 10)) converts 5, 10 to a fraction.

Executing the above code produces the following results

1/2

Let's look at another built-in python module http. It is used to work with the Hyper Text Transfer Protocol. For those of you who will major into developing python web applications then you might encounter this module.

Let's look at how to import and use the http module.

from http import HTTPStatus

print(HTTPStatus.NOT_FOUND.value)

print(HTTPStatus.NOT_FOUND.phrase)

print(HTTPStatus.NOT_FOUND.description)

HERE,

  • from http import HTTPStatus imports the HTTPStatus class from the http module
  • print(HTTPStatus.NOT_FOUND.value) prints the value of the status NOT_FOUND
  • print(HTTPStatus.NOT_FOUND.phrase) prints the phrase associated with the NOT_FOUND status
  • print(HTTPStatus.NOT_FOUND.description) prints the description of NOT_FOUND

Executing the above code produces the following results

404
Not Found
Nothing matches the given URI

Tutorial challenge

Browser the standard python modules and create a simple example application that utilizes the any built-in module then share the results in the comments section. Sharing is one of the great ways to improve and increase your learning capacity.

Summary

A module is simply a python file with a *.py extension that contains related pieces of code. This could be variables, methods, and classes. We use module to promote code reusability and to make our programs more organized. Python allows us to create custom modules, utilize built-in modules or third-party modules.

What next?

If you enjoyed this lesson then show us your appreciation by creating a free accounts on our site. As always we appreciate your comments down below.


...