Home » Python

Python | How to create Packages (Example of Packages)?

Python Packages with Examples: Here, we are going to learn how to create packages in Python? Here, we have some of the examples on Python Packages.
Submitted by Pankaj Singh, on October 20, 2018

This an Example of creating packages in python. In Python a package [Folder] is wrapper which contains modules [files].

[pankaj] It is folder which contains 3 files [__init__.py, mymath.py, mycheck.py]. So "pankaj" is package and "mycheck.py", "mymath.py" are modules.

"__init__.py" is package descriptor file which is used to configure which function of which module can be made available to outer world.

Download all files

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

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

__init__.py

from pankaj.mymath import *
from pankaj.mycheck import *

Example: Menu.py

Menu.py is a python script which is used to access Pankaj package.

from os import *
from pankaj 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()

Download all files




Comments and Discussions!

Load comments ↻






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