Building a Basic Payroll System with Python: A Beginner’s Guide

Sergio S Ribeiro, June 2023

A payroll system is a software or process that helps organizations manage employee compensation. It automates tasks like calculating wages, taxes, and deductions, ensuring accurate and timely employee payment. It tracks employee information, handles payroll calculations, generates paychecks, and assists with tax compliance and reporting. Using a payroll system improves accuracy, saves time, enhances efficiency, and ensures legal compliance in managing employee compensation.

In this post, I want to explore a Python code snippet demonstrating a very basic Payroll System (see code below). The idea is to exemplify the application of Python language to a business problem. The code allows users to add employees, remove employees, list employee information (id, name, salary, and total), and exit the program. We will go through the code step by step to understand its functionality.

Code:


	payroll = []
	stay = True

	while stay:
	  print("+-------------------------------------+")
	  print("| <MENU>                Payroll System |")
	  print("+-------------------------------------+")
	  print("  Would you like to")
	  option = input("  <A>dd, <R>emove, <L>ist, or <E>xit?").upper()
	  print("---------------------------------------")
	  
	  if option == "E":
		stay=False
	  elif option == "A":
		name = input("Name:")
		salary = int(input("Salary:"))
		payroll.append([name, salary])
	  elif option == "R":
		i = int(input("Enter employee ID #"))
		if len(payroll) >= i:
		  payroll.pop(i)
		  print("Employee successfully removed")
		else:
		  print("ID # not found")
	  elif option == "L":
		total = 0
		for i, employee in enumerate(payroll):
		  name = employee[0]
		  salary = employee[1]
		  print(i, name, salary)
		  total += salary
		print(">> Total:", total)
	  else:
		print(option, "is not a valid option!")
	  
	print("Program ended")
			  

The code begins by initializing an empty list called payroll to store employee information. The variable stay is set to True to control the loop that keeps the program running until the user chooses to exit. Inside the loop, a menu is printed to the console, and the user is prompted to select an option: <A>dd, <R>emove, <L>ist, or <E>xit by typing their initial letter.

The code uses conditional statements to handle the user’s selected option. If the option is "E" (exit), the loop is terminated by setting stay to False. If the option is "A" (add), the code prompts the user to enter the employee’s name and salary. The employee’s information is then appended as a list to the payroll list.

If the option is "R" (remove), the code prompts the user to enter an employee ID. If the length of the payroll list is greater than or equal to the entered ID, the employee is removed using the pop() method. Otherwise, a message is printed indicating that the ID was not found.

If the option is "L" (list), the code iterates over the payroll list using enumerate(). It retrieves each employee’s name and salary and prints them along with their respective index. Additionally, the code calculates the total salary by summing up all the salaries.

If none of the above options are chosen, an error message is printed to notify the user that their input is invalid.

Finally, when the loop is exited, a message indicating the end of the program is printed.

In this post, I tried to demonstrate how to implement a very basic Payroll System. The code allowed users to add employees, remove employees, list employee information, and exit the program. By breaking down the code and understanding each section, I hope it helps you gain insights into how such a system can be developed using Python.

Take the opportunity to test the program and make any changes you want.

If you don’t know Python and would like to start programming in Python, check out Computers and Information Processing for Business: Microsoft Office 2019 and Python.