Python program to calculate gross pay


Basically Gross pay is the total amount paid to an employee for the total time he/she worked, i.e. this is the full amount of salary of an employee before the taxes and deductions. The Gross pay also includes the bonuses, overtime or reimbursements from an employer on top of the salary pay. Mathematically the formula for the gross pay is as follows.

Hours worked * pay per hour = gross pay per period

For example, if an employee worked for 200 hours in a given period and earned Rs.300/- for an hour then the gross pay will be

200 * 300 = 60000 pay per period

Similarly we can calculate the gross pay using the python language.

Example

In this example we are trying to calculate the gross pay, by mathematical formula using the python language.

hours = 200
hour_pay = 500
def gross_pay():
   g_pay = hours * hour_pay 
   print("Gross pay for",hours,"hours is",g_pay)
gross_pay()

Output

Gross pay for 200.0 hours is 100000.0

Example

In the previous example we just handled the number of hours worked and hourly pay. Now in this example we are going to handle the hours worked hourly pay along with the overtime pay. Let’s keep the limit for the usual working hours as 300 and if any hour more than 300 will comes under overtime and the pay for the overtime is 1.5 times of the hourly rate then the gross pay is calculated as follows.

hours = 18
hour_pay = 4
def gross_pay():
   if hours <= 300:
      g_pay = hours * hour_pay 
      print("Gross pay without overtime for",hours,"hours is",g_pay)
   else:
       overtime_hours = hours - 300
       g_pay = (300 * hour_pay) + (overtime_hours * hour_pay * 1.5)
       print("Gross pay with overtime for",hours,"hours is",g_pay)
gross_pay()

Output

Gross pay without overtime for 18 hours is 72

Example

Now in this example we are trying to calculate the gross pay with pay rates for different hours.

The below code takes list of tuples, where each tuple represents the maximum hours for a specific pay rate and the corresponding hourly rate and iterates through the list, calculating the pay for each range until the total hours worked are calculated.

def gross_pay(hour_pay,hours_worked):
   total_gpay = 0
   for hours, rate in hour_pay:
      if hours_worked <= hours:
         total_gpay += hours_worked * rate 
         break
      else:
         total_gpay += hours * rate 
         hours_worked -= hours
   return total_gpay
hour_pay = [(40, 15), (10, 20), (40, 25)]
hours_worked = 100
print(gross_pay(hour_pay,hours_worked))

Output

1800

Updated on: 19-Oct-2023

125 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements