0% found this document useful (0 votes)
338 views6 pages

Creating Our Own Modules in Python: Da, Hra, PF and Itax

The document describes how to create a Python module called "employee" that contains functions to calculate employee salary details like dearness allowance, house rent allowance, provident fund, and income tax. It defines these functions and saves them in a file called "employee.py". It then shows a Python program that imports this employee module and uses the functions to calculate an employee's gross and net salary based on their basic salary input by the user. It also explains Python's special "__name__" variable that indicates whether a file is run directly or imported as a module.

Uploaded by

Naveen Yallapu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
338 views6 pages

Creating Our Own Modules in Python: Da, Hra, PF and Itax

The document describes how to create a Python module called "employee" that contains functions to calculate employee salary details like dearness allowance, house rent allowance, provident fund, and income tax. It defines these functions and saves them in a file called "employee.py". It then shows a Python program that imports this employee module and uses the functions to calculate an employee's gross and net salary based on their basic salary input by the user. It also explains Python's special "__name__" variable that indicates whether a file is run directly or imported as a module.

Uploaded by

Naveen Yallapu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Creating our Own Modules in Python

A module represents a group of classes, methods, functions and


variables. While we are developing software, there may be several
classes, methods and functions. When a module is developed, it can
be reused in any program that needs that module. In Python, we
have several built-in modules like sys, io, time etc. Just like these
modules, we can also create our own modules and use them
whenever we need them.

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.

from employee import *


#calculate gross salary of employee by taking basic
basic = float(input('Enter basic salary:'))
#calculate gross salary
gross = basic+da(basic)+hra(basic)
print('Your gross salary: {:10.2f}'. format(gross))
#calculate net salary
net = gross - pf(basic)-itax(gross)
print('Your net salary: {:10.2f}'. format(net))
The Special Variable __name__

When a program is executed in Python, there is a special variable internally


created by the name ‘__name__’. This variable stores information regarding
whether the program is executed as an individual program or as a module. When
the program is executed directly, the Python interpreter stores the value
‘__main__’ into this variable. When the program is imported as a module into
another program, then Python interpreter stores the module name into this
variable. Thus, by observing the value of the variable ‘__name__’, we can
understand how the program is executed.

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.

A Python program using the special __name__ variable. #python program


to display a message. save this as one.py

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.

#in this program one.py is imported as a module.

import one
one.display()

You might also like