Creating Our Own Modules in Python: Da, Hra, PF and Itax
Creating Our Own Modules in Python: Da, Hra, PF and Itax
Now, we will create our own module by the name ‘employee’ and
store the functions da(), hra(), pf() and itax() in that module.
Please remember we can also store classes and variables etc. in the
same manner in any module. So, please type the following
functions and save the file as ‘employee.py’.
#save this code as employee.py
#to calculate dearness allowance
def da(basic):
""" da is 80% of basic salary """
da = basic*80/100
return da
#to calculate house rent allowance
def hra(basic):
""" hra is 15% of basic salary """
hra = basic*15/100
return hra
#to calculate provident fund amount
def pf(basic):
""" pf is 12% of basic salary """
pf = basic*12/100
return pf
#to calculate income tax payable by employee
def itax(gross):
""" tax is calculated at 10% on gross """
tax = gross*0.1
return tax
A Python program that uses the functions of employee module
and calculates the gross and net salaries of an employee.
Let’s assume that we have written a program. When this program is run, Python
interpreter stores the value ‘__main__’ into the special variable ‘__name__’.
Hence, we can check if this program is run directly as a program or not as:
if __name__ == '__main__':
print('This code is run as a program')
Suppose, if ‘__name__’ variable does not contain the value ‘__main__’, it
represents that this code is run as a module from another external program.
def display():
print('Hello Python!')
if __name__ == '__main__':
display() #call display function
print('This code is run as a program')
else:
print('This code is run as a module')
A Python program that import the previous Python
program as a module.
import one
one.display()