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.
# 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}")
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
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.

Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.