Calculate Employee Salary with Python

Salary calculation is a critical aspect of payroll management. Python makes it easy to compute salaries by including basic pay, allowances, deductions, and bonuses. This tutorial walks you through the steps to calculate day-wise salary components and generate a detailed payslip using Python.

1. Components of Salary

  • Basic Pay: The base salary of the employee.
  • DA (Dearness Allowance): Calculated as 20% of the basic pay.
  • HRA (House Rent Allowance): Calculated as 15% of the basic pay.
  • Vehicle Allowance: ₹200 per day worked.
  • Medical Allowance: Fixed at ₹1,000 per month.
  • Bonus: 10% of the basic pay, applicable for full attendance only.
  • PF Deduction: 12% of the basic pay.
  • Professional Tax: Fixed at ₹200 per month.

2. Python Code for Salary Calculation

# Inputs
basic_pay = float(input("Enter Basic Pay: ₹"))
days_present = int(input("Enter Number of Days Present: "))

# Constants
full_attendance_days = 30  # Total days in a month (modifiable)
da_percentage = 0.20       # DA as 20% of Basic Pay
hra_percentage = 0.15      # HRA as 15% of Basic Pay
daily_vehicle_allowance = 200  # Daily vehicle allowance
medical_allowance = 1000       # Fixed monthly medical allowance
pf_percentage = 0.12           # PF as 12% of Basic Pay
professional_tax = 200         # Fixed professional tax per month
bonus_percentage = 0.10        # Bonus as 10% of Basic Pay (only for full attendance)

# Day-wise Components
daily_basic = basic_pay / full_attendance_days
daily_da = (basic_pay * da_percentage) / full_attendance_days
daily_hra = (basic_pay * hra_percentage) / full_attendance_days

# Components Based on Days Present
total_basic = daily_basic * days_present
total_da = daily_da * days_present
total_hra = daily_hra * days_present
total_vehicle_allowance = daily_vehicle_allowance * days_present

# Fixed Components
total_medical_allowance = medical_allowance
total_pf = basic_pay * pf_percentage
total_bonus = basic_pay * bonus_percentage if days_present == full_attendance_days else 0

# Deductions
total_deductions = total_pf + professional_tax

# Final Salary Calculation
gross_salary = total_basic + total_da + total_hra + total_vehicle_allowance + total_medical_allowance + total_bonus
net_salary = gross_salary - total_deductions

# Generate Payslip
print("\n--- Payslip Breakdown ---")
print(f"Basic Pay: ₹{total_basic:.2f}")
print(f"DA (20% of Basic Pay): ₹{total_da:.2f}")
print(f"HRA (15% of Basic Pay): ₹{total_hra:.2f}")
print(f"Vehicle Allowance: ₹{total_vehicle_allowance:.2f}")
print(f"Medical Allowance: ₹{total_medical_allowance:.2f}")
print(f"Bonus: ₹{total_bonus:.2f}")
print(f"Gross Salary: ₹{gross_salary:.2f}")
print(f"Deductions: ₹{total_deductions:.2f}")
print(f"Net Salary: ₹{net_salary:.2f}")

3. Sample Input and Output

Input:

Enter Basic Pay: ₹30000
Enter Number of Days Present: 30

Output:

--- Payslip Breakdown ---
Basic Pay: ₹30000.00
DA (20% of Basic Pay): ₹6000.00
HRA (15% of Basic Pay): ₹4500.00
Vehicle Allowance: ₹6000.00
Medical Allowance: ₹1000.00
Bonus: ₹3000.00
Gross Salary: ₹50500.00
Deductions: ₹3800.00
Net Salary: ₹46700.00

Conclusion

With Python, salary calculations become straightforward and customizable. This script provides a complete breakdown of salary components, including basic pay, allowances, deductions, and bonuses, ensuring a professional payslip generation.



Podcast on Python Basics


Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer