Adding chapter2 files
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
# pkgmain3.py with with main function
|
||||
|
||||
import masifutil
|
||||
|
||||
|
||||
def my_main():
|
||||
""" This is a main function which generates two random numbers and then apply calculator functions on them """
|
||||
x = masifutil.random_2d()
|
||||
y = masifutil.random_1d()
|
||||
|
||||
sum = masifutil.add(x,y)
|
||||
diff = masifutil.subtract(x,y)
|
||||
|
||||
sroot = masifutil.sqrt(x)
|
||||
log10x = masifutil.log(x)
|
||||
log2x = masifutil.ln(x)
|
||||
|
||||
print("x = {}, y = {}".format(x, y))
|
||||
print("sum is {}".format(sum))
|
||||
print("diff is {}".format(diff))
|
||||
print("square root is {}".format(sroot))
|
||||
print("log base of 10 is {}".format(log10x))
|
||||
print("log base of 2 is {}".format(log2x))
|
||||
|
||||
""" This is executed only if the special variable '__name__' is set as main"""
|
||||
if __name__ == "__main__":
|
||||
my_main()
|
||||
@@ -0,0 +1,30 @@
|
||||
# pkgmain4.py with with main function
|
||||
|
||||
import sys
|
||||
sys.path.append('/Users/muasif/Google Drive/PythonForGeeks/source_code/chapter2/mypackages')
|
||||
|
||||
import masifutil
|
||||
|
||||
|
||||
def my_main():
|
||||
""" This is a main function which generates two random numbers and then apply calculator functions on them """
|
||||
x = masifutil.random_2d()
|
||||
y = masifutil.random_1d()
|
||||
|
||||
sum = masifutil.add(x,y)
|
||||
diff = masifutil.subtract(x,y)
|
||||
|
||||
sroot = masifutil.sqrt(x)
|
||||
log10x = masifutil.log(x)
|
||||
log2x = masifutil.ln(x)
|
||||
|
||||
print("x = {}, y = {}".format(x, y))
|
||||
print("sum is {}".format(sum))
|
||||
print("diff is {}".format(diff))
|
||||
print("square root is {}".format(sroot))
|
||||
print("log base of 10 is {}".format(log10x))
|
||||
print("log base of 2 is {}".format(log2x))
|
||||
|
||||
""" This is executed only if the special variable '__name__' is set as main"""
|
||||
if __name__ == "__main__":
|
||||
my_main()
|
||||
@@ -0,0 +1,30 @@
|
||||
# pkgmain5.py with with main function
|
||||
|
||||
import site
|
||||
site.addsitedir('/Users/muasif/Google Drive/PythonForGeeks/source_code/chapter2/mypackages')
|
||||
|
||||
import masifutil
|
||||
|
||||
|
||||
def my_main():
|
||||
""" This is a main function which generates two random numbers and then apply calculator functions on them """
|
||||
x = masifutil.random_2d()
|
||||
y = masifutil.random_1d()
|
||||
|
||||
sum = masifutil.add(x,y)
|
||||
diff = masifutil.subtract(x,y)
|
||||
|
||||
sroot = masifutil.sqrt(x)
|
||||
log10x = masifutil.log(x)
|
||||
log2x = masifutil.ln(x)
|
||||
|
||||
print("x = {}, y = {}".format(x, y))
|
||||
print("sum is {}".format(sum))
|
||||
print("diff is {}".format(diff))
|
||||
print("square root is {}".format(sroot))
|
||||
print("log base of 10 is {}".format(log10x))
|
||||
print("log base of 2 is {}".format(log2x))
|
||||
|
||||
""" This is executed only if the special variable '__name__' is set as main"""
|
||||
if __name__ == "__main__":
|
||||
my_main()
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
from .mycalculator import add, subtract
|
||||
from .myrandom import random_1d, random_2d
|
||||
from .advcalc.advcalculator import sqrt, log, ln
|
||||
@@ -0,0 +1,14 @@
|
||||
# advcalculator.py with sqrt,log and ln functions
|
||||
import math
|
||||
|
||||
def sqrt(x):
|
||||
"""This function takes square root of a number"""
|
||||
return math.sqrt(x)
|
||||
|
||||
def log(x):
|
||||
"""This function returns log of base 10"""
|
||||
return math.log(x,10)
|
||||
|
||||
def ln(x):
|
||||
"""This function returns log of base 2"""
|
||||
return math.log(x,2)
|
||||
@@ -0,0 +1,9 @@
|
||||
# mycalculator.py with add and subtract functions
|
||||
|
||||
def add(x, y):
|
||||
"""This function adds two numbers"""
|
||||
return x + y
|
||||
|
||||
def subtract(x, y):
|
||||
"""This function subtracts two numbers"""
|
||||
return x - y
|
||||
@@ -0,0 +1,14 @@
|
||||
# myrandom.py with default and custom random functions
|
||||
import random
|
||||
|
||||
def random_1d():
|
||||
"""This function get a random number between 0 and 9"""
|
||||
return random.randint(0,9)
|
||||
|
||||
def random_2d():
|
||||
"""This function get a random number between 10 and 99"""
|
||||
return random.randint(10,99)
|
||||
|
||||
def get_random(lower, upper):
|
||||
"""This function get a random number between lower and upper"""
|
||||
return random.randint(lower,upper)
|
||||
Reference in New Issue
Block a user