Sunday, May 17, 2026

Python Material - Part - 20 - Oops - 3

 __author__ = "Narendra Boyina"


from pandas.conftest import index

# -----------------------------------------------------------------------------
# Copyright (c) 2025 BR Technologies PVT LTD
# -----------------------------------------------------------------------------
"""
Topics to be covered in today's class:
--> hasattr , getattr, delattr
--> small bank application
--> Practice programs
"""


class Employee:
"""Common base class for all employees"""
empCount = 0 # class variable/ attribute

def __init__(self, name, salary):
"""as soon as created the instance, this method will be executed"""
self.name = name
self.salary = salary
Employee.empCount += 1 # class_name.class_variable
print("Employee count till now is :{} ".format(Employee.empCount))
# print("This is constructor method")
# print("Once created instance automatically this method will execute")
# print("How many times you will create new instace those many times this contructor method will execute aumatically")

def displayCount(self):
# using class variable inside method : Classname.ClassVariable
print("total employee count is {}".format(Employee.empCount))

def displayEmployee(self):
print("Name : ", self.name, ", Salary: ", self.salary)


# emp_1 = Employee("Narendra", 120000) # emp_1 (instance variable (object)) or attribute
# emp_1.displayCount() # by using object calling method
# print(emp_1.empCount) # by using object/instance calling the class variable
#
# emp_2 = Employee("Manasa", 30000)
# emp_2.displayCount()
# emp_2.displayEmployee()
#
# emp_3 = Employee("Surendra", 22000)
# emp_3.displayCount()
# emp_3.displayEmployee()
# emp_3.salary = 52000
# emp_3.displayEmployee()
#
# emp_2.displayEmployee()


"""Note:
If you have created multiple objects for the same class then, different objects located in different memory location
You can check memory locations in decimal format or hexa_decimal format
Narenrdra != manasa != Surendra"""

# print(id(emp_1)) #
# print(id(emp_2)) #
# print(id(emp_3)) #

"""You can add, remove, or modify attributes of classes and objects at any time """
# emp_1.age = 27 # Add an 'age' attribute,by using object
# print(emp_1.age)
# emp_2.name = 'ganesh' # Modify 'name' attribute's value.
# print(emp_2.name)
# # del emp_1.salary # Delete 'salary' attribute.
# # print(emp_1.salary) # it will give AttributeError
# print("Employee 2 salary is : {}".format(emp_2.salary))
# print(emp_2.name)

"""Instead of using the normal statements to access/modify/delete attributes,
you can use the following in-built methods """

""" The hasattr(obj,name) − to check if an attribute exists or not,
if attribute is present then return True else return False"""


# print(hasattr(emp_1, 'salary')) # already we have deleted so it will give False as output
# print(hasattr(emp_1, 'age')) # checking for age attribute --> True
# print(hasattr(emp_1, 'designation')) # False (designation attribute is not present so it will give False)
# print(hasattr(emp_2,'salary')) # True (I haven't deleted salary attribute to emp_2, I have deleted salary attribute for emp_1 object)

# # The setattr(obj,name,value) − to set an attribute. If attribute does not exist, then it would be created.
# setattr(emp_1, "salary", 146780)
# setattr(emp_1, "Designation", "Technical Lead")
#
# # """The getattr(obj, name[, default]) − to access the attribute of object """
# print(emp_1.salary) # instance_variable.class_variable
# print(getattr(emp_1, "salary"))
# print(emp_1.Designation)
# print(getattr(emp_1,"Designation"))
#
# """ The delattr(obj, name) − to delete an attribute."""
# delattr(emp_1,"salary")
# print(hasattr(emp_1, 'salary')) # Returns true if 'salary' attribute exists
#
# setattr(emp_1, 'salary', 70000) # Set attribute 'salary' at 7000
# s = getattr(emp_1, 'salary') # Returns value of 'salary' attribute
# print(s)
# print(emp_1.salary)
# delattr(emp_1, 'salary') # Delete attribute 'salary'
"""We in previous steps we have deleted salary attribute, so displaying result as False """
# print(hasattr(emp_1, 'salary')) # it will give False Reason: you have deleted in the before line
# print(emp_1.salary) # gives AttributeError Reason: Already deleted in the above line
#
#
# class Employee:
# """This class is designed to help HR to calculate year-end salary increases based on employee performance and other parameters"""
# pass
#
# print(Employee.__doc__, end=".")
#
# class Kruthika:
# """This class having the methods related to kruthika details """
# pass


# print(Kruthika.__doc__, end=".")
#
# class Meera:
# """This class having the methods related to kruthika details """
# pass
#
# print(Meera.__doc__)

"""
Python code
--> to open Multiple bank accounts
--> to deposit amounts
--> with draw amounts
--> to get balance from any particular account
"""

class BankAccount:
venkat =10

""" this code belongs to bank application which can perform creation of account, deposit, withdraw,get_balance """
def __init__(self, account_holder, balance=0):
"""
Initializes a new bank account.
:param account_holder: Name of the account holder
:param balance: Initial balance (default is 0)
"""
self.account_holder = account_holder
self.balance = balance

def deposit(self, amount):
"""
Deposits a specified amount into the account.
:param amount: The amount to deposit
"""
self.balance += amount
print(f"Deposited amount is {amount}. latest balance: {self.balance}")


def withdraw(self, with_draw_amount):
"""
Withdraws a specified amount from the account if sufficient balance exists.
:param amount: The amount to withdraw
"""
if with_draw_amount <= self.balance:
self.balance -= with_draw_amount # self.balance = self.balance - with_draw_amount
print(f"Withdrew {with_draw_amount}. Remaining balance: {self.balance}")
elif with_draw_amount > self.balance:
print(f"current balance in your account:{self.balance}, you are trying to withdraw {with_draw_amount} So Insufficient funds. ")


def get_balance(self):
"""
Returns the current balance of the account.
"""
print(f"{self.account_holder}'s Available balance: {self.balance}")
return self.balance


# Small bank application
# if __name__ == "__main__":
# # Create a new bank account for meera with an initial balance of $500
# Meera_account = BankAccount("Meera", 500) # init method will be executes as soon as you have created an object for a particular class
# #
# # # Deposit 500 into Meera's account
# Meera_account.deposit(500)
# #
# # # Withdraw200 from Meera's account
# Meera_account.withdraw(200)
# #
# # # Attempt to withdraw an amount greater than the balance
# Meera_account.withdraw(2000)
# #
# # # Check the final balance
# Meera_account.get_balance()
# #
# ganesh_account = BankAccount("ganesh", 500) # init method will be executes as soon as you have created an object for a particular class
# # Deposit 500 into Meera's account
# ganesh_account.deposit(50000)
# # Withdraw 200 from Meera's account
# ganesh_account.withdraw(10000)
# #
# Manikanta_account = BankAccount("Manikanta", 500) # init method will be executes as soon as you have created an object for a particular class
# # Deposit $500 into Meera's account
# Manikanta_account.deposit(100000)
# # Withdraw $200 from Meera's account
# Manikanta_account.withdraw(10000)
# #
# ganesh_account.get_balance() # this will give ganesh's Available balance





""" Practice programs """
""" Write a Python function that takes a list and returns a new list with unique elements of the first list."""


def unique_list(data):
req_list = []
for element in data:
if element not in req_list:
req_list.append(element)

print(req_list)


# data = [5, 3, 1, 3, 4, 5, 6, 6, 68, 8, 8, 8, 68]
# unique_list(data)

# x = set(data)
# print(list(x))


#
def unique_list(data):
req_list = [element for index, element in enumerate(data) if element not in data[ :index]] # omit starting address
print(req_list)

# data = [5, 3, 1, 3, 4, 5, 6, 6, 68, 8, 8, 8, 68]
# unique_list(data)

No comments:

Post a Comment