×

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

Last Updated : April 28, 2025

Module files are special file that are used as library files and can be accessed in another file.

Create a Python Module

It's very simple to create a Python module. You just need to write Python code in a file and save that file with a .py extension.

Example to Create Python Modules

Here is an example where we create two modules: mycheck.py and mymath.py. These modules contain functions for number checking and mathematical operations.

Module 1: mycheck.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+=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):
    return a + b

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

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

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

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

Import a Python Module

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

Examples to Import Python Modules

Below are examples demonstrating how to import and use functions from the modules you created earlier.

Example 1: menu.py

This program imports both mycheck.py and mymath.py modules and uses them to perform operations like addition, subtraction, checking for even/odd numbers, prime checking, and more.

import os
import mymath
import mycheck

def main():
    ans = True
    while ans:
        os.system('cls')
        print("MENU")
        print("--------------------------------------")
        print("1. Add")
        print("2. Subtract")
        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):
                print(n, "is Even")
            else:
                print(n, "is Not Even")
        elif ch == 6:
            n = int(input("Enter N: "))
            if mycheck.isodd(n):
                print(n, "is Odd")
            else:
                print(n, "is Not Odd")
        elif ch == 7:
            n = int(input("Enter N: "))
            if mycheck.isprime(n):
                print(n, "is Prime")
            else:
                print(n, "is Not Prime")
        elif ch == 8:
            n = int(input("Enter N: "))
            if mycheck.ispalindrome(n):
                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 2: menu2.py

This example uses a more concise import structure, loading all functions from both modules directly into the memory.

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. Subtract")
        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):
                print(n, "is Even")
            else:
                print(n, "is Not Even")
        elif ch == 6:
            n = int(input("Enter N: "))
            if isodd(n):
                print(n, "is Odd")
            else:
                print(n, "is Not Odd")
        elif ch == 7:
            n = int(input("Enter N: "))
            if isprime(n):
                print(n, "is Prime")
            else:
                print(n, "is Not Prime")
        elif ch == 8:
            n = int(input("Enter N: "))
            if ispalindrome(n):
                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

This example demonstrates selective importing, where only specific functions from the modules are loaded into memory.

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. Subtract")
        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):
                print(n, "is Even")
            else:
                print(n, "is Not Even")
        elif ch == 4:
            n = int(input("Enter N: "))
            if isprime(n):
                print(n, "is Prime")
            else:
                print(n, "is Not Prime")
        elif ch == 5:
            n = int(input("Enter N: "))
            if ispalindrome(n):
                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

Exercise

Select the correct option to complete each statement about creating and importing modules in Python.

  1. In Python, to create a module, you simply write a Python file with the extension ___.
  2. To import a module, you use the ___ keyword followed by the module name.
  3. If you want to import a specific function from a module, you use the ___ keyword.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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