Learn with Yasir

Share Your Feedback

Python Encapsulation Exercises – Practice Object-Oriented Programming (OOP)


🧪 Coding Exercises

🟢 Beginner Exercises

  1. Protected Attributes
  2. Create a `BankAccount` class with a protected `_balance` attribute.
    Implement methods to deposit and withdraw money with basic validation.
    

    Requirements

    • - Use single underscore convention for protected attribute - Prevent negative balance withdrawals - Include a method to display current balance

    Input

    account = BankAccount(100)
    account.deposit(50)
    account.withdraw(30)
    account.display_balance()
    

    Expected Output

    Current balance: 120
    

    📚 Protected Attributes Tutorial



  3. Private Attributes with Getters/Setters
  4. Create a `Person` class with private `__age` attribute.
    Implement proper getter and setter methods with age validation.
    

    Requirements

    • - Use double underscores for private attribute - Setter should reject ages < 0 or > 120 - Getter should return age with "years" suffix

    Input

    person = Person()
    person.set_age(25)
    print(person.get_age())
    person.set_age(150)
    

    Expected Output

    25 years
    Invalid age!
    

    📚 Private Attributes Guide






🟡 Intermediate Exercises



  1. Property Decorators
  2. Create a `Temperature` class that uses @property to control access to celsius value.
    Add validation to prevent temperatures below absolute zero (-273.15°C).
    

    Requirements

    • - Use @property for getter - Use @temperature.setter for setter - Raise ValueError for invalid temperatures

    Input

    temp = Temperature()
    temp.celsius = 25
    print(temp.celsius)
    temp.celsius = -300
    

    Expected Output

    25
    ValueError: Temperature below absolute zero!
    

    📚 Property Decorators Explained



  3. Read-Only Property
  4. Create a `Circle` class with private `__radius` and read-only `area` property.
    The area should be calculated when accessed but not modifiable directly.
    

    Requirements

    • - Use private attribute for radius - Implement area as read-only property - Area calculation: πr² (use math.pi)

    Input

    circle = Circle(5)
    print(circle.area)
    circle.area = 100
    

    Expected Output

    78.53981633974483
    AttributeError: can't set attribute
    

    📚 Read-Only Properties



  5. Full Encapsulation Example
  6. Create a `Student` class that fully encapsulates student data:
    - Private name (validated to be non-empty)
    - Private grades list (with methods to add grades and calculate average)
    

    Requirements

    • - Use double underscore for private attributes - Implement proper getters/setters - Add method to calculate grade average - Prevent direct modification of grades list

    Input

    student = Student("Alice")
    student.add_grade(90)
    student.add_grade(85)
    print(student.get_name())
    print(student.get_average())
    

    Expected Output

    Alice
    87.5
    

    📚 Complete Encapsulation



🔴 Advanced Exercises







🧠 Practice & Progress

Explore More Topics

Python Fundamentals

Flow Control Statements


Python Functions


Fundamentals more ...




🧠 Python Advanced

Object-Oriented Programming in Python (OOP)

More...

🧠 Modules