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=Falseif n%2==0:ans=Truereturn ansdef isodd(n):ans=Falseif n%2==1:ans=Truereturn ansdef isprime(n):ans=Falsec=0for i in range(1,n+1):if n%i==0:c=c+1if c==2:ans=Truereturn ansdef ispalindrome(n):ans=Falsem=nrev=0while n>0:dig=n%10rev = rev*10+dign=n//10if rev==m:ans=Truereturn ansmymath.py
mymath.py
def sum(a,b):c=a+breturn cdef difference(a,b):c=a-breturn cdef product(a,b):c=a*breturn cdef quotient(a,b):c=a/breturn cdef remainder(a,b):c=a%breturn cNow, we are implementing the operations from the module functions in the below examples:
現在,我們在以下示例中通過模塊功能實現操作:
.minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}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 mycheckdef main():ans=Truewhile 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=Falseprint("-------------------------------------")input("Press any key.....") if __name__=="__main__":main() .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}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=Truewhile 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=Falseprint("-------------------------------------")input("Press any key.....") if __name__=="__main__":main() .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}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,ispalindromedef main():ans=Truewhile 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=Falseprint("-------------------------------------")input("Press any key.....") if __name__=="__main__":main()Download all files
下載所有文件
翻譯自: https://www.includehelp.com/python/modules-with-examples.aspx
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Python | 如何创建模块(模块示例)?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java Random nextInt(
- 下一篇: Java PushbackReader