×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Create and Import Modules in Python (with Examples)

By Pankaj Singh Last updated : December 21, 2024

Create a Python Module

It's very simple to create a Python module, you have to write Python code in a file and save that file with .py file extension.

Example to Create Python Modules

This is an example of creating module in python. Module files are special file that are used as library files and can be accessed in another file.

In this example, there are two module files "mycheck.py" and "mymath.py" – the modules contains the functions related to the checking the numbers and mathematical operations

Module 1: pycheck.py

def iseven(n):
    ans=False
    if n%2==0:
        ans=True
    return ans

def isodd(n):
    ans=False
    if n%2==1:
        ans=True
    return ans

def isprime(n):
    ans=False
    c=0
    for i in range(1,n+1):
        if n%i==0:
            c=c+1
    if c==2:
        ans=True
    return ans

def ispalindrome(n):
    ans=False
    m=n
    rev=0
    while n>0:
        dig=n%10
        rev = rev*10+dig
        n=n//10
    if rev==m:
        ans=True
    return ans

Module 2: mymath.py

def sum(a,b):
    c=a+b
    return c

def difference(a,b):
    c=a-b
    return c

def product(a,b):
    c=a*b
    return c

def quotient(a,b):
    c=a/b
    return c

def remainder(a,b):
    c=a%b
    return c

Import a Python Module

To import a Python module, you have to use the import statement followed by the module name.

Examples to Import Python Modules

Now, we are implementing the operations from the module functions in the below examples:

Example 1: menu.py

In this program all the functions of both modules i.e. mycheck.py/mymath.py are get loaded in menu.py’s memory and have to use module name as parent to access child functions.

import os
import mymath
import mycheck

def main():
    ans=True
    while ans:
        os.system('cls')
        print("MENU")
        print("--------------------------------------")
        print("1.Add")
        print("2.Substract")
        print("3.Multiply")
        print("4.Divide")
        print("5.Even Check")
        print("6.Odd Check")
        print("7.Prime Check")
        print("8.Palindrome Check")
        print("9.Exit")
        print("-------------------------------------")
        ch=int(input("Enter choice(1-9):"))
        print("-------------------------------------")
        if ch==1:
            a = int(input("Enter A: "))
            b = int(input("Enter B: "))
            c = mymath.sum(a,b)
            print("Sum    :",c)
        elif ch==2:
            a = int(input("Enter A: "))
            b = int(input("Enter B: "))
            c = mymath.difference(a,b)
            print("difference    :",c)
        elif ch==3:
            a = int(input("Enter A: "))
            b = int(input("Enter B: "))
            c = mymath.product(a,b)
            print("Product    :",c)
        elif ch==4:
            a = int(input("Enter A: "))
            b = int(input("Enter B: "))
            c = mymath.quotient(a,b)
            print("Quotient    :",c)
        elif ch==5:
            n = int(input("Enter N: "))
            if mycheck.iseven(n)==True:
                print(n,"is Even")
            else:
                print(n,"is Not Even")
        elif ch==6:
            n = int(input("Enter N: "))
            if mycheck.isodd(n)==True:
                print(n,"is Odd")
            else:
                print(n,"is Not Odd")
        elif ch==7:
            n = int(input("Enter N: "))
            if mycheck.isprime(n)==True:
                print(n,"is Prime")
            else:
                print(n,"is Not Prime")
        elif ch==8:
            n = int(input("Enter N: "))
            if mycheck.ispalindrome(n)==True:
                print(n,"is Palindrome")
            else:
                print(n,"is Not Palindrome")
        elif ch==9:
            ans=False
        print("-------------------------------------")
        input("Press any key.....")
if __name__=="__main__":main()

Example 1: menu2.py

In this program all the functions of both modules i.e. mycheck.py/mymath.py are get loaded in menu2.py’s memory and child functions can be called directly

from os import *
from mymath import *
from mycheck import *

def main():
    ans=True
    while ans:
        system('cls')
        print("MENU")
        print("--------------------------------------")
        print("1.Add")
        print("2.Substract")
        print("3.Multiply")
        print("4.Divide")
        print("5.Even Check")
        print("6.Odd Check")
        print("7.Prime Check")
        print("8.Palindrome Check")
        print("9.Exit")
        print("-------------------------------------")
        ch=int(input("Enter choice(1-9):"))
        print("-------------------------------------")
        if ch==1:
            a = int(input("Enter A: "))
            b = int(input("Enter B: "))
            c = sum(a,b)
            print("Sum    :",c)
        elif ch==2:
            a = int(input("Enter A: "))
            b = int(input("Enter B: "))
            c = difference(a,b)
            print("difference    :",c)
        elif ch==3:
            a = int(input("Enter A: "))
            b = int(input("Enter B: "))
            c = product(a,b)
            print("Product    :",c)
        elif ch==4:
            a = int(input("Enter A: "))
            b = int(input("Enter B: "))
            c = quotient(a,b)
            print("Quotient    :",c)
        elif ch==5:
            n = int(input("Enter N: "))
            if iseven(n)==True:
                print(n,"is Even")
            else:
                print(n,"is Not Even")
        elif ch==6:
            n = int(input("Enter N: "))
            if isodd(n)==True:
                print(n,"is Odd")
            else:
                print(n,"is Not Odd")
        elif ch==7:
            n = int(input("Enter N: "))
            if isprime(n)==True:
                print(n,"is Prime")
            else:
                print(n,"is Not Prime")
        elif ch==8:
            n = int(input("Enter N: "))
            if ispalindrome(n)==True:
                print(n,"is Palindrome")
            else:
                print(n,"is Not Palindrome")
        elif ch==9:
            ans=False
        print("-------------------------------------")
        input("Press any key.....")
if __name__=="__main__":main()

Example 3: menu3.py

In this program only few functions of both modules i.e. mycheck.py/mymath.py are get Loaded in menu3.py's memory and child functions can be called directly

from os import system
from mymath import sum,difference
from mycheck import iseven,isprime,ispalindrome

def main():
    ans=True
    while ans:
        system('cls')
        print("MENU")
        print("--------------------------------------")
        print("1.Add")
        print("2.Substract")
        print("3.Even Check")
        print("4.Prime Check")
        print("5.Palindrome Check")
        print("6.Exit")
        print("-------------------------------------")
        ch=int(input("Enter choice(1-6):"))
        print("-------------------------------------")
        if ch==1:
            a = int(input("Enter A: "))
            b = int(input("Enter B: "))
            c = sum(a,b)
            print("Sum    :",c)
        elif ch==2:
            a = int(input("Enter A: "))
            b = int(input("Enter B: "))
            c = difference(a,b)
            print("difference    :",c)
        elif ch==3:
            n = int(input("Enter N: "))
            if iseven(n)==True:
                print(n,"is Even")
            else:
                print(n,"is Not Even")
        elif ch==4:
            n = int(input("Enter N: "))
            if isprime(n)==True:
                print(n,"is Prime")
            else:
                print(n,"is Not Prime")
        elif ch==5:
            n = int(input("Enter N: "))
            if ispalindrome(n)==True:
                print(n,"is Palindrome")
            else:
                print(n,"is Not Palindrome")
        elif ch==6:
            ans=False
        print("-------------------------------------")
        input("Press any key.....")
if __name__=="__main__":main()

Download all files

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.