def average(values): nvals = len(values) sum = 0.0 for v in values: sum += v return float(sum) / nvals ## my script using the math module ## import mymath # values = [2, 4, 6, 8, 10] print('squares:') for v in values: print(mymath.square(v)) print('Cubes:') for v in values: print(mymath.cube(v)) print('average: ' + str(mymath.average(values))) import mymath as mt print(mt.square(2)) print(mt.square(3))
def test_square(self): self.assertTrue(mymath.square(10), (40, 100))
#http://effbot.org/pyfaq/tutor-what-is-if-name-main-for.htm The if __name__ == "__main__": ... trick exists in Python so that our Python files can act as either reusable modules, or as standalone programs. As a toy example, let’s say that we have two files: $ cat mymath.py def square(x): return x * x if __name__ == '__main__': print "test: square(42) ==", square(42) $ cat mygame.py import mymath print "this is mygame." print mymath.square(17) In this example, we’ve written mymath.py to be both used as a utility module, as well as a standalone program. We can run mymath standalone by doing this: $ python mymath.py test: square(42) == 1764 But we can also use mymath.py as a module; let’s see what happens when we run mygame.py: $ python mygame.py this is mygame. 289 Notice that here we don’t see the ‘test’ line that mymath.py had near the bottom of its code. That’s because, in this context, mymath is not the main program. That’s what the if __name__ == "__main__": ... trick is used for.
## My script using the math module ## import mymath # note no .py values = [2, 4, 6, 8, 10] print('squares:') for v in values: print(mymath.square(v)) print('Cubes:') for v in values: print(mymath.cube(v)) print('Average: ' + str(mymath.average(values)))
import mymath print "this is mygame." print mymath.square(17)
## file: mymath.py ## def spuare(n): return n * n def cubr(n): return n * n * n def average(values): nvals = len(values) sum = 0.0 for v in values: sum += v return float(sum) / navals ## my script using the math module ## import mymath # Note no.py values = [2, 4, 6, 8, 10] print('squares: ') for v in values: print(mymath.square(v)) print('Cubes: ') for v in values: print(mymath.cube(v)) print('average: ' + str(mymath.average(values))) import mymath as mt print(mt.square(2)) print(mt.square(3))
import mymath as a values=[1,4,6,8,10] print('squares:') for v in values: print(a.square(v)) print ('cubes') for v in values: print(a.cube(v)) print('average:'+ str(a.average(values)))
from mymath import square from mymath import cube square(6) cube(4)
import mymath as th values = [2, 4, 6, 8, 10] print('square:') for c in values: print(th.square(c)) print('cubes:') for c in values: print(th.cube(c)) print('Average: ' + str(th.average(values)))
import mymath as mt print(mt.square(2)) print(mt.square(3))
def my_game(num): squa_num = mymath.square(num) return squa_num
import mymath print(__name__) print('this is my game') print(mymath.square(17))
''' Write a module named mymath consisting of following functions: square(n) cube(n) power(a,n) sqrt(n) cubert(n) nthroot(n) # A function that finds nth root of a number. PI # Create a constant PI with value 3.1415 e # Create a constant e with value 2.71 Note: Write documentation string along with all the functions of the said module. Perform the following tasks using your module. a) Generate the Web Documentation using Pydoc Module. 5 b) Print the contents of your module. 5 c) Use all the functions of the module mymath into your program and calculate the area of a circle,also Calculate the sqaures, cube, power n and squareroot, cuberoot and nth root of the values of a list and print the result as follow: ''' #calculate the area of a circle import mymath as mm rad = float(input("Enter the radius of circle:")) area = mm.PI * mm.square(rad) print("The area of the circle is", area)
# import a single file module import mymath print(mymath.square(4)) # alias import mymath as m print(m.square(3)) # import specific function from module from mymath import square, sum print(square(5)) # import module from a folder/package import custom_package.mystring as ms print(ms.getstr()) from custom_package import myconvertor, mystring print(myconvertor.strtoint('3'))