ADVERTISEMENT
ADVERTISEMENT

Python program to import modules – different Methods

Here, we will write a Python programs to import modules in different ways.
Submitted by Shivang Yadav, on March 21, 2021

Programming languages allows programmers to keep their codes in different files and then use them in a program by importing them.

If you are from C / C++ background, you might be familiar with #include statements. These are import statements that import libraries consisting of methods that can be used in a program.

Similarly, python provides import statements for importing modules in Python. These can be used to import methods in the current program for usage.

Python allows you to import files in different ways,

  • Importing the module (or file): A python module can be fully imported to a program for usage using import statements. To access the method of this imported file we need to use the dot " . "
  • Importing the modules functions: You can alternately import the function of the module in case you need only some specific function and loading the rest might slow down the application. This is done by using the following statement.
    from fileName import function(s)
  • You can select all functions also using the above method by using ' * ' instead of the function names. Importing like this will eliminate the usage of dot while calling, you can call the function directly.

Program 1: Program to see importing by importing the whole file

Files to be imported

File: 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=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

File: 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

Main file:

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()

Output:

MENU
--------------------------------------
1.Add
2.Substract
3.Multiply
4.Divide
5.Even Check
6.Odd Check
7.Prime Check
8.Palindrome Check
9.Exit
-------------------------------------
Enter choice(1-9):1
-------------------------------------
Enter A: 43
Enter B: 65
Sum    : 108
-------------------------------------
Press any key.....
MENU
--------------------------------------
1.Add
2.Substract
3.Multiply
4.Divide
5.Even Check
6.Odd Check
7.Prime Check
8.Palindrome Check
9.Exit
-------------------------------------
Enter choice(1-9):2
-------------------------------------
Enter A: 899
Enter B: 54
difference    : 845
-------------------------------------
Press any key.....
MENU
--------------------------------------
1.Add
2.Substract
3.Multiply
4.Divide
5.Even Check
6.Odd Check
7.Prime Check
8.Palindrome Check
9.Exit
-------------------------------------
Enter choice(1-9):3
-------------------------------------
Enter A: 3
Enter B: 7
Product    : 21
-------------------------------------
Press any key.....
MENU
--------------------------------------
1.Add
2.Substract
3.Multiply
4.Divide
5.Even Check
6.Odd Check
7.Prime Check
8.Palindrome Check
9.Exit
-------------------------------------
Enter choice(1-9):4
-------------------------------------
Enter A: 7
Enter B: 2
Quotient    : 3.5
-------------------------------------
Press any key.....
MENU
--------------------------------------
1.Add
2.Substract
3.Multiply
4.Divide
5.Even Check
6.Odd Check
7.Prime Check
8.Palindrome Check
9.Exit
-------------------------------------
Enter choice(1-9):5
-------------------------------------
Enter N: 6
6 is Even
-------------------------------------
Press any key.....
MENU
--------------------------------------
1.Add
2.Substract
3.Multiply
4.Divide
5.Even Check
6.Odd Check
7.Prime Check
8.Palindrome Check
9.Exit
-------------------------------------
Enter choice(1-9):6
-------------------------------------
Enter N: 7645
7645 is Odd
-------------------------------------
Press any key.....
MENU
--------------------------------------
1.Add
2.Substract
3.Multiply
4.Divide
5.Even Check
6.Odd Check
7.Prime Check
8.Palindrome Check
9.Exit
-------------------------------------
Enter choice(1-9):7
-------------------------------------
Enter N: 654
654 is Not Prime
-------------------------------------
Press any key.....
MENU
--------------------------------------
1.Add
2.Substract
3.Multiply
4.Divide
5.Even Check
6.Odd Check
7.Prime Check
8.Palindrome Check
9.Exit
-------------------------------------
Enter choice(1-9):8
-------------------------------------
Enter N: 54364
54364 is Not Palindrome
-------------------------------------
Press any key.....
MENU
--------------------------------------
1.Add
2.Substract
3.Multiply
4.Divide
5.Even Check
6.Odd Check
7.Prime Check
8.Palindrome Check
9.Exit
-------------------------------------
Enter choice(1-9):9
-------------------------------------
-------------------------------------
Press any key.....

Program 2: Importing using from - import statement using *

import os 
from mymath import *
from mycheck import *

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 = 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()

Output:

RUN as the above program

Program 3: Importing files using from-import statement, importing specific functions only

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()

Output:

Run as the above program

Python Basic Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.