Python | 如何创建模块(模块示例)?

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.

这是在python中创建模块示例 。 模块文件是用作库文件的特殊文件,可以在另一个文件中访问。

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

在此示例中,有两个模块文件“ mycheck.py”“ mymath.py” –这些模块包含与检查数字和数学运算有关的功能

Download all files

下载所有文件

pycheck.py

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

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

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

现在,我们在以下示例中通过模块功能实现操作:

Example 1) menu.py

示例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.

在此程序中,两个模块的所有功能(即mycheck.py/mymath.py)都加载到menu.py的内存中,并且必须使用模块名称作为父项来访问子函数

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 2) menu2.py

示例2)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

在该程序中,两个模块的所有功能(即mycheck.py/mymath.py)都加载到menu2.py的内存中,并且可以直接调用子功能

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

示例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

在该程序中,两个模块中只有少数几个函数(即mycheck.py/mymath.py)被加载到menu3.py的内存中子函数可以直接调用

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

下载所有文件

翻译自: https://www.includehelp.com/python/modules-with-examples.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值