Create Python Modules

How to create Python modules?

Python programming language is famous for its modules such as sys, io, and time, which offer a wide range of possibilities to make your program fantastic or straightforward. Did you ever think about how to create Python modules yourself based on your program’s needs? You can do that very quickly, and we will discuss this concept in this post.

If you want to create Python modules, please pay attention to the code mentioned below. It contains the code to create a module called ‘team_member’ that calculates the allowances and taxes in an employee’s salary.

#save this code as team_member.py
#calculate the da allowance
def dearness_allowance(basicsalary):
    """Da is 70 percent of basic salary"""
    dearness_allowance=basicsalary*70/100
    return dearness_allowance
#calculate the rent allowance
def rent_allowance(basicsalary):
    """Rent allowance is 10 percent of salary"""
    rent_allowance=basicsalary*10/100
    return rent_allowance
#calculate provident fund
def provident_fund(basicsalary):
    """PF is 15 % of basic salary"""
    provident_fund=basicsalary*15/100
    return provident_fund
#calculate income tax payable by employee
def income_tax(gross_salary):
    """tax is 10% of gross salary"""
    income_tax=gross_salary*10/100
    return income_tax

Save the code mentioned above as team_member.py and create another python file with the name employee_life.py in the same directory. Add the code mentioned below to the employee_life.py file, save and run it in the command prompt to get the output discussed below.

#add this code to employee_life.py
#this employee_life.py file should also exist
#in the same directory as team_member.py
from team_member import *

#ask employee's basic salary
basicsalary=float(input("Enter your basic salary: "))

#calculate the gross salary of employee based on basic
gross_salary=basicsalary+dearness_allowance(basicsalary)+rent_allowance(basicsalary)
print(f"Your gross salary is {gross_salary}")

#calculate final salary
final=gross_salary-provident_fund(basicsalary)-income_tax(basicsalary)
print("Your final salary after deductions: ",final)

Output

Enter your basic salary: 20000
Your gross salary is 36000.0
Your final salary after deductions:  31000.0

In the programs mentioned in this post, the team_member.py file acts as a module, and we imported the same as a module to the employee_life.py file using the code from team_member import *. This imports everything available in the team_member.py module.

You can notice that we saved the logic to calculate the allowances in the module, and when we imported the module in the first line, all the functions declared in the module were also imported to the employee_life.py file. You can see that it helped calculate the allowances easily and calculate the net salary eventually.

This is just an example program, and you can use this concept to create Python modules on your own to accommodate the needs of your requirements.

Scroll to Top