38. Problem to create employee class in Python

In this Python oops program, we will create employee class in Python. Include a method to print the employee’s name and salary.

Create Employee class in Python

Steps to solve the program
  1. The Employee class is defined with a constructor method __init__ that takes name and salary as parameters and initializes the instance variables self.name and self.salary with the provided values.
  2. The class also has a method print_details that prints the employee’s name and salary.
  3. The code creates an object employee of the Employee class by calling its constructor and passing the name “John Walker” and salary 50000 as arguments.
  4. The print_details method is called on the employee object, which prints the employee’s name and salary.
				
					class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary
    
    def print_details(self):
        print("Name:", self.name)
        print("Salary:", self.salary)

# Create an object of the Employee class
employee = Employee("John Walker", 50000)
employee.print_details()
				
			

Output:

				
					Name: John Walker
Salary: 50000
				
			

Related Articles

Create a Python class called Shape with a method to calculate the area of the shape.

Create a Python class called Manager that inherits from the Employee class.

Leave a Comment