Employee Class using Instance & Class Variables

Challenge: Employee Class using Instance & Class Variables

Objective: Create an Employee class that uses both instance variables and a class variable.


1. Class Properties (Variables)

  • Instance Variables: (Unique to each employee object)
    • name
    • salary
    • designation
    • employee_id (Automatically assigned, not taken as a parameter)
  • Class Variable: (Shared across all employee objects)
    • employee_count
    • Initial Value: 101
    • Purpose: Used to automatically generate the next employee ID.

2. Employee ID Rules

  • The first employee ID should be E101.
  • The next should be E102, and so on.
  • The ID must be assigned automatically when a new employee object is created.
  • The employee_count class variable must be incremented after assigning an ID.

3. Class Methods (Functions)

  • __init__(self, name, salary, designation)
    • Takes namesalary, and designation as parameters.
    • Does NOT take employee_id as a parameter.
    • Inside this function:
      • Assigns an employee_id using the current employee_count (e.g., “E101”).
      • Increments the employee_count class variable for the next employee.
  • show_details(self)
    • Prints all the details of the employee: Name, Salary, Employee ID, and Designation.
  • total_employees(cls) (A Class Method)
    • Correction: This must be a class method, not a static method.
    • Purpose: Returns the total number of employees created.
    • Logic: Calculate total employees as employee_count - 101.

4. Key Summary

  • Use a class variable (employee_count) to auto-generate IDs.
  • Use instance variables to store individual employee details.
  • Implement three methods__init__show_details, and the class method total_employees.

class Employee:

    employee_count = 101

    def __init__(self, name, desig, sal):
        self.name = name
        self.designation = desig
        self.salary = sal
        self.eid = 'e' + str(Employee.employee_count)
        Employee.employee_count += 1

    def show_details(self):
        print('Name:',self.name)
        print('Eid:',self.eid)
        print('Designation:',self.designation)
        print('Salary:',self.salary)

    @classmethod
    def total_emp(cls):
        return cls.employee_count - 101


e1 = Employee('John', 'Manager', 10000)
e2 = Employee('Mark', 'Team Leader', 8000)

e1.show_details()
print('')
e2.show_details()
print('Total Employees:', e1.total_emp())

Similar Posts

  • Why Python is So Popular: Key Reasons Behind Its Global Fame

    Python’s fame and widespread adoption across various sectors can be attributed to its unique combination of simplicity, versatility, and a robust ecosystem. Here are the key reasons why Python is so popular and widely used in different industries: 1. Easy to Learn and Use 2. Versatility Python supports multiple programming paradigms, including: This versatility allows…

  • Basic Character Classes

    Basic Character Classes Pattern Description Example Matches [abc] Matches any single character in the brackets a, b, or c [^abc] Matches any single character NOT in the brackets d, 1, ! (not a, b, or c) [a-z] Matches any character in the range a to z a, b, c, …, z [A-Z] Matches any character in the range A to Z A, B, C, …, Z [0-9] Matches…

  • Vs code

    What is VS Code? 💻 Visual Studio Code (VS Code) is a free, lightweight, and powerful code editor developed by Microsoft. It supports multiple programming languages (Python, JavaScript, Java, etc.) with: VS Code is cross-platform (Windows, macOS, Linux) and widely used for web development, data science, and general programming. 🌐📊✍️ How to Install VS Code…

  • What is list

    In Python, a list is a built-in data structure that represents an ordered, mutable (changeable), and heterogeneous (can contain different data types) collection of elements. Lists are one of the most commonly used data structures in Python due to their flexibility and dynamic nature. Definition of a List in Python: Example: python my_list = [1, “hello”, 3.14,…

  • Class 10 String Comparison ,Bitwise Operators,Chaining Comparisons

    String Comparison with Relational Operators in Python 💬⚖️ In Python, you can compare strings using relational operators (<, <=, >, >=, ==, !=). These comparisons are based on lexicographical (dictionary) order, which uses the Unicode code points of the characters. 📖 How String Comparison Works 🤔 Examples 💡 Python Important Notes 📌 String comparison is…

  • Variable Length Keyword Arguments in Python

    Variable Length Keyword Arguments in Python Variable length keyword arguments allow a function to accept any number of keyword arguments. This is done using the **kwargs syntax. Syntax python def function_name(**kwargs): # function body # kwargs becomes a dictionary containing all keyword arguments Simple Examples Example 1: Basic **kwargs python def print_info(**kwargs): print(“Information received:”, kwargs) print(“Type of…

Leave a Reply

Your email address will not be published. Required fields are marked *