DEV Community

Cover image for Day 15 of My AI & Data Mastery Journey: From Python to Generative AI
Nitin-bhatt46
Nitin-bhatt46

Posted on

Day 15 of My AI & Data Mastery Journey: From Python to Generative AI

Class & Object

🧠 Concept:

A class is a blueprint; an object is a real instance.

Example :- You’re working for an app that manages employees.
You need a structure to store their name and salary and show info.

Creation Question

Create a class Employee with:

attributes → name, salary

method → display_info() that prints name and salary

Then create 2 employee objects and display their details.

Hint: use init() constructor to set attributes.

Debugging Question

Find & fix the bug:

`class Employee:
def init(name, salary):
name = name
salary = salary

def display(self):
    print(f"{self.name} earns ₹{self.salary}")
Enter fullscreen mode Exit fullscreen mode

emp = Employee("Aman", 50000)
emp.display()`

🎯 Bug: attributes are not being set correctly; identify why.

Attributes & Methods

Concept:

Attributes = data; Methods = actions on that data.

Real-world idea:

Bank Account System — deposit, withdraw, show balance.

Creation Question

Create a BankAccount class that:

takes owner and balance

has methods deposit(amount), withdraw(amount) and show_balance()

Make sure withdrawal doesn’t allow negative balance.

Debugging Question

Fix logic in:

`class BankAccount:
def init(self, owner, balance):
self.owner = owner
self.balance = balance

def deposit(amount):
    self.balance += amount
    print("Deposited!")
Enter fullscreen mode Exit fullscreen mode

a = BankAccount("Nitin", 1000)
a.deposit(500)`

🎯 Bug: method parameters missing or incorrect.

Encapsulation

Concept:
Hide internal data; allow safe access through functions.

Example:-

ATM should not let users directly change balance — only through methods.

🧱 Creation Question

Modify your Day-2 BankAccount:

make balance private (__balance)

add methods get_balance() and set_balance(amount) but restrict direct modification.

** Debugging Question**

Debug:

`class Student:
def init(self, name, grade):
self.__grade = grade

def get_grade(self):
    return grade`
Enter fullscreen mode Exit fullscreen mode

🎯 Bug: variable reference wrong — fix scope to access private member.

Top comments (0)