Python Modules: Learn Modules In Python

Python Modules: Learn Modules In Python

In the previous tutorial, you have learned about what is python global variable and keyword in detail.

Modules in Python

In this post we will discuss with you what modules are in Python, and how to build custom modules. And how to use it after creating modules in python. This means that in this post we will know and learn everything about modules.

What are the modules in Python?

The modules in Python are those in which the code to do work is written in advance. You can create modules in Python by using python functions, classes, or variables, etc.

A file containing Python code, for e.g.: evenOdd.py, is called a module and its module name would be evenOdd.

Creating modules in Python has only one meaning. It converts your large program into a small manageable and organized file. So that you can reuse it easily.

If you have to repeatedly check-in your Python program whether this number is even or odd. So you have to write the same code again and again. And if you make a module. So you just have to include it in the program and just call it. So this will tell you whether the given number is even number or odd number.

python create custom modules

You know about the above python modules. Now let’s see how you create modules and write code in modules:

Look like we built the module in python. Which return that the given number is even or even number.

Now we first create a module file named evenOdd.py. After this, the number in the file evenOdd.py is even or Od is made its function.

# Python Module example

def evenOddFunc( x ): 
    if (x % 2 == 0): 
        return "This is even number"
    else: 
        return "This is odd number"

How to import modules in Python?

We also created a module in Python. Now we will make the module import our function.

In Python, use the import keyword to import a custom created module and inbuilt model.

The following is how to import the module using the import keyword:

import evenOdd

How to use import and use module in python

So far we have imported the module in Python. Now we will see how to use the module. Or how to call a function created inside a module.

For this, you have to type the name of the module and then write ” dot “. After this you have to write the name of the function which you have to call from the module.

Note: – A Python module can have more than one function.

Let’s see below:

import evenOdd

x = evenOdd.evenOddFunc(10)

print(x)

Built-in Modules in Python

Python consists of inbuilt modules. Which are already made? You can only call and use them according to your requirements.

Python has a lot of inbuilt modules. Such as math modules, calendar module, etc.

Here we will tell you using the inbuilt math module. So let see the program to how to use inbuilt modules in python:

# to import math module

import math

print("The value of pi is", math.pi)

Output

3.14

Python rename module on import

If you have imported the module and you want to change its name. So you can:

# to import math module

import math as mx

print("The value of pi is", mx.pi)

Its output will also be similar to the above-given program output.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

One reply to Python Modules: Learn Modules In Python

  1. Bonne documentation

Leave a Reply

Your email address will not be published. Required fields are marked *