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
- 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.
- The class also has a method print_details that prints the employee’s name and salary.
- 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.
- 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 Manager that inherits from the Employee class.
Create a Python class called Customer with attributes name and balance.
Create a Python class called VIPCustomer that inherits from the Customer class.
Create a Python class called Phone with attributes brand, model, and storage.
Create a Python class called Laptop with attributes brand, model, and storage.
Create a Python class called Book with attributes title, author, and pages.