Editor

Python Program to Manage Employees Using a Class

Intermediate · Classes and objects

This program manages a small team with an Employee class. Each employee gets an ID automatically, stores a name, a role and a monthly salary, and can be given a raise. The program then prints the team as a table with the total and average salary.

Program

employee_class.py
class Employee:
next_id = 101 # a class attribute, shared by all employees
def __init__(self, name, role, salary):
self.emp_id = Employee.next_id
Employee.next_id += 1
self.name = name
self.role = role
self.salary = salary
def give_raise(self, percent):
self.salary += self.salary * percent // 100
def show(self):
print(f"{self.emp_id} {self.name:<7} {self.role:<10} Rs. {self.salary}")
staff = [
Employee("Anita", "Manager", 85000),
Employee("Rahul", "Developer", 60000),
Employee("Sneha", "Designer", 52000),
]
staff[1].give_raise(10) # Rahul gets a 10% raise
print("ID Name Role Salary")
for emp in staff:
emp.show()
total = 0
for emp in staff:
total += emp.salary
print("Total monthly salaries: Rs.", total)
print("Average salary: Rs.", total // len(staff))
Output
ID   Name    Role       Salary
101  Anita   Manager    Rs. 85000
102  Rahul   Developer  Rs. 66000
103  Sneha   Designer   Rs. 52000
Total monthly salaries: Rs. 203000
Average salary: Rs. 67666

How it works

  1. next_id is a class attribute, shared by every employee, so each new object takes the next number as its emp_id and then moves the counter on.
  2. __init__ stores the name, role and salary in each object, alongside its ID.
  3. give_raise(10) raises one employee's salary by 10%, from Rs. 60000 to Rs. 66000, and the other objects are not affected.
  4. show() prints one row of the table, and padding such as :<7 in the f-string keeps the columns lined up.
  5. The total and the average are worked out from the list of objects, and // gives the average in whole rupees.

Other ways to write it

A Manager class that inherits from Employee

A Manager class that inherits from Employee
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def yearly_pay(self):
return self.salary * 12
class Manager(Employee):
def __init__(self, name, salary, bonus):
super().__init__(name, salary)
self.bonus = bonus
def yearly_pay(self):
return super().yearly_pay() + self.bonus
team = [Employee("Rahul", 60000), Manager("Anita", 85000, 100000)]
for person in team:
print(f"{person.name}: Rs. {person.yearly_pay()} a year")
Output
Rahul: Rs. 720000 a year
Anita: Rs. 1120000 a year

A manager is an employee with a bonus, so Manager inherits everything from Employee and only changes how the yearly pay is worked out.

Sorting and filtering employees

Sorting and filtering employees
class Employee:
def __init__(self, name, dept, salary):
self.name = name
self.dept = dept
self.salary = salary
staff = [Employee("Anita", "Sales", 85000), Employee("Rahul", "IT", 60000),
Employee("Sneha", "IT", 72000), Employee("Vikram", "Sales", 48000)]
print("Highest paid first:")
for e in sorted(staff, key=lambda e: e.salary, reverse=True):
print(" ", e.name, e.salary)
it_team = [e.name for e in staff if e.dept == "IT"]
print("IT department:", it_team)
Output
Highest paid first:
  Anita 85000
  Sneha 72000
  Rahul 60000
  Vikram 48000
IT department: ['Rahul', 'Sneha']

sorted() with key=lambda e: e.salary orders the objects by salary, and a list comprehension picks out the employees of one department.

Try it yourself

Add a method annual_salary() that returns 12 times the monthly salary, and print it for each employee.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises