import cal as lib def cal(a,b): print(a,b) print(lib.add(1,2)) print(lib.sub(10,5)) cal(1,2) # above method is called "aliases" import
''' #### Python Modules #### -A module is a file containing Python definitions and statements. -A module can define functions, classes and variables. -A module can also include runnable code. -Grouping related code into a module makes the code easier to understand and use. ## Two Ways use Module ## 1-The import statement. 2-The from import Statement. ## The dir() function ''' # 1-The import statement. import cal print(cal.add(4, 3)) print(cal.sub(500, 230)) print(cal.mul(3, 5)) # 2-The from import Statement. from cal import add, sub print(add(200, 500)) print(sub(200, 123)) # dir() function print(dir(cal)) print(dir(sub))
#Chuong trinh Python Calculator #Su dung cac Module, Function #haind #Python import cal vonglap = 1 choice = 0 while vonglap == 1: choice = cal.menu() if choice == 1: cal.add() elif choice == 2: cal.sub() elif choice == 3: cal.mul() elif choice == 4: cal.div() elif choice == 5: vonglap = 0 print "Cam on ban da su dung chuong trinh"
#from cal import * #from cal import add from cal import ( add, sub, ) print(add(1, 2)) print(sub(10, 5)) #print(globals())
print('1.add') print('2.sub') print('3.mul') print('4.div') print('5.Go back') choice1 = int(input("enter the choice\n")) try: if (choice1 == 1): num1 = int(input("enter the number1\n")) num2 = int(input("enter the number2\n")) res = cal.add(num1, num2) print("The result of add of two numbers", res) elif (choice1 == 2): num1 = int(input("enter the number1\n")) num2 = int(input("enter the number2\n")) res = cal.sub(num1, num2) print("The result of sub of two numbers", res) elif (choice1 == 3): num1 = int(input("enter the number1\n")) num2 = int(input("enter the number2\n")) res = cal.mul(num1, num2) print("The result of mul of two numbers", res) elif (choice1 == 4): num1 = int(input("enter the number1\n")) num2 = int(input("enter the number2\n")) res = cal.div(num1, num2) print("The result of div of two numbers", res) elif (choice1 == 5): break else: print("enter the correct choice")
import cal vonglap = 1 choice = 0 while vonglap == 1: choice = cal.menu() if choice == 1: cal.add() elif choice == 2: cal.sub() elif choice == 3: cal.mul() elif choice == 4: cal.div() elif choice == 5: vonglap = 0
# import cal from cal import add, sub, mul, div, sine while (True): inp = input() if (inp == "stop"): break n = int(input()) m = int(input()) d = { "add": add(n, m), "sub": sub(n, m), "mul": mul(n, m), "div": div(n, m), } print(d[inp]) # s = add(n, m) # sub = sub(8, 4) # mul = mul(4, 8) # div = div(8, 4) # print(s, sub, mul, div) # print(sine(90))
import cal a = int(input("enter the first value")) b = int(input("ebter the second value")) cal.add(a, b) cal.sub(a, b)
def test_sub1(self): self.assertEqual(cal.sub(4,2), 2)
def test_sub2(self): self.assertEqual(cal.sub(7,2), 2 )
def test_sub(self): self.assertEqual(cal.sub(10,2), 8)
import cal as c a=9 b=7 print(c.add(a,b)) print(c.sub(a,b)) print(c.mul(a,b)) print(c.div(a,b))
def test3_sub(self): assert (cal.sub(10, 5) == 5), "Sub is failed"
# geekyshows.py <--- Main Module import cal as c # Importing Cal Module print("cal Module's variable:", c.a) # Accessing Cal Module's Variable c.name() # Accessing Cal Module's Function a = c.add(10, 20) # Accessing Cal Module's Function print(a) b = c.sub(20, 10) # Accessing Cal Module's Function print(b)
def test_sub(self): self.assertEqual(cal.sub(10, 5), 5, 'message') self.assertEqual(cal.sub(10, -5), 15, 'message') self.assertEqual(cal.sub(-10, -5), -5)
#!/usr/bin/env python # -*- coding:utf-8 -*- # Created by winchoo # 2018/7/6 #Directory和Python package的区别在于Directory是空的,Python package里面有个__init__.py文件 # import cal,time # print(cal.add(3,5)) # print(cal.sub(3,5)) #注意import cal会先执行cal.py里面的内容 #模块import做了两件事情: #1.执行对应文件 #2.引入变量名 #如果不想使用cal.则有另外一种书写方式 from cal import add from cal import sub #或者 from cal import *#这里的*代表一切,但是不推荐这么使用 #结果会是加载下面的函数,因为会覆盖掉之前的函数 # def add(x,y): # return x+y+100 print(add(3,5)) print(sub(3,5))
'''lis=[1,2,3,4,5,6,7,8,9,12,12,10] odd=list(filter(lambda a:a%2!=0,lis)) print(odd) odd1=list(map(lambda a:a+2,lis)) print(odd1)''' import cal sum = cal.add(5, 6) print(sum) dif = cal.sub(5, 6) print(dif) re = cal.mod(5, 6) print(re) print(__name__)