Adding more folders to chapter 2

This commit is contained in:
muassif
2021-01-16 15:06:16 +04:00
committed by GitHub
parent 55e9ad0515
commit abab477fe1
9 changed files with 140 additions and 0 deletions
+15
View File
@@ -0,0 +1,15 @@
import mypanda
def my_main():
dict = {"country": ["Brazil", "Russia", "India", "China", "South Africa"],
"capital": ["Brasilia", "Moscow", "New Dehli", "Beijing", "Pretoria"],
"area": [8.516, 17.10, 3.286, 9.597, 1.221],
"population": [200.4, 143.5, 1252, 1357, 52.98]}
mypanda.print_dataframe (dict)
""" This is executed only if the special variable '__name__' is set as main"""
if __name__ == "__main__":
my_main()
+9
View File
@@ -0,0 +1,9 @@
# mypanda.py with default and custom random functions
import pandas
def print_dataframe(dict):
"""This function output a dictionary as a data frame """
brics = pandas.tDataFrame(dict)
print(brics)
+15
View File
@@ -0,0 +1,15 @@
# myrandomv2.py with default and custom random functions
import random
#random = __import__('random')
#random = importlib.import_module('random')
print (globals())
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)
+18
View File
@@ -0,0 +1,18 @@
# calcmain1.py with a main function
import mycalculator
import myrandom
def my_main( ):
""" This is a main function which generates two random numbers and then apply calculator functions on them """
x = myrandom.random_2d( )
y = myrandom.random_1d( )
sum = mycalculator.add(x, y)
diff = mycalculator.subtract(x, y)
print("x = { }, y = { }".format(x, y))
print("sum is { }".format(sum))
print("diff is { }".format(diff))
""" This is executed only if the special variable '__name__' is set as main"""
if __name__ == "__main__":
my_main()
+22
View File
@@ -0,0 +1,22 @@
# calcmain2.py with alias for modules
import mycalculator as calc
import myrandom as rand
def my_main():
""" This is a main function which generates two random numbers and then apply calculator functions on them """
x = rand.random_2d()
y = rand.random_1d()
sum = calc.add(x,y)
diff = calc.subtract(x,y)
print("x = {}, y = {}".format(x,y))
print("sum is {}".format(sum))
print("diff is {}".format(diff))
print (globals())
""" This is executed only if the special variable '__name__' is set as main"""
if __name__ == "__main__":
my_main()
+22
View File
@@ -0,0 +1,22 @@
# calcmain3.py with from and alias combined
from mycalculator import add as my_add
from mycalculator import subtract as my_subtract
from myrandom import random_2d, random_1d
def my_main():
""" This is a main function which generates two random numbers and then apply calculator functions on them """
x = random_2d()
y = random_1d()
sum = my_add(x,y)
diff = my_subtract(x,y)
print("x = {}, y = {}".format(x,y))
print("sum is {}".format(sum))
print("diff is {}".format(diff))
print (globals())
""" This is executed only if the special variable '__name__' is set as main"""
if __name__ == "__main__":
my_main()
+10
View File
@@ -0,0 +1,10 @@
# globalmain.py with globals function
def print_globals():
print (globals())
def hello():
print ("Hello")
if __name__ == "__main__":
print_globals()
+10
View File
@@ -0,0 +1,10 @@
# 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
+19
View File
@@ -0,0 +1,19 @@
# myrandom.py with default and custom random functions
import random
#random = __import__('random')
#random = importlib.import_module('random')
print (globals())
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)