2019-06-03:模块使用练习

实现自己的数学模块mymath,提供有4个函数,分别为加减乘除,在B模块中调用A模块的函数。

文件夹A中,hemath.py 中添加的函数:

#encoding=utf-8
def add(a,b):
    return a+b

def sub(a,b):
    return a-b
def mul(a,b):
    return a*b

def div(a,b):
    return float(a)//float(b)

 文件夹A中创建a.py

    第一种导入方法,直接导入模块的名字hemath

#encoding=utf-8
import hemath
"""
带有模块名字的导入方法
"""
print("加:",hemath.add(1,2))
print('减:',hemath.sub(2,1))
print('乘:',hemath.mul(3,4))
print('除:',hemath.div(4,2))

第二种导入方法:使用的时候,不加模块名字,直接使用方法的导入。

from hemath import *

print("加:",add(1,2))
print('减:',sub(2,1))
print('乘:',mul(3,4))
print('除:',div(4,2))

第三种导入方法:将导入的模块重命名(为了方便使用,我们可以给导入的方法重新命名)

from hemath import add as a
from hemath import sub as s
from hemath import mul as m
from hemath import div as d

print("加:",a(1,2))
print('减:',s(3,1))
print('乘:',m(3,4))
print('除:',d(8,2))

猜你喜欢

转载自blog.csdn.net/sinat_18722099/article/details/90759777
今日推荐