Experiment 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Name: K Mahesh

ID No:2200030757
Sub: Python Lab

Experiment-03

1. Displaying the namespace of the class


class Student:
def __init__(self, student_id, student_name):
self.student_id = student_id
self.student_name = student_name
student = Student('30757', 'Mahesh')
print(student.__dict__)

Output

2. Creating A Student with Two Attributes


class Student:
def __init__(self, student_id, student_name):
self.student_id = student_id
self.student_name = student_name
def display_info(self):
print("Student ID:", self.student_id)
if hasattr(self, 'student_name'):
print("Student Name:", self.student_name)
if hasattr(self, 'student_class'):
print("Student Class:", self.student_class)
student_instance = Student(student_id=1, student_name="John Doe")

print("Entire values of the class:")

1|Pa ge
Name: K Mahesh
ID No:2200030757
Sub: Python Lab

student_instance.display_info()

student_instance.student_class = "Class A"


print("\nValues after adding student_class:")
student_instance.display_info()
delattr(student_instance, 'student_name')

print("\nValues after removing student_name:")


student_instance.display_info()

Output

2|Pa ge
Name: K Mahesh
ID No:2200030757
Sub: Python Lab

3. Code For Bank Transactions using OOPs Concept


class BankAccount:
def __init__(self, account_number, name, balance):
self.account_number = account_number
self.name = name
self.balance = balance
def deposit(self, amount):
try:
if amount > 0:
self.balance += amount
print(f"Deposit of {amount} successful. New balance: {self.balance}")
else:
raise ValueError("Invalid deposit amount. Please enter a positive
amount.")
except ValueError as e:
print(str(e))
def withdrawal(self, amount):
try:
if 0 < amount <= self.balance:
self.balance -= amount
print(f"Withdrawal of {amount} successful. New balance:
{self.balance}")
elif amount > self.balance:
raise ValueError("Insufficient funds for withdrawal.")
else:
raise ValueError("Invalid withdrawal amount. Please enter a positive
amount.")
except ValueError as e:
print(str(e))
def bank_fees(self):
try:
fees = 0.05
fee_amount = self.balance * fees
self.balance -= fee_amount
print(f"Bank fees of {fee_amount} applied. New balance: {self.balance}")

3|Pa ge
Name: K Mahesh
ID No:2200030757
Sub: Python Lab

except Exception as e:
print(f"Error applying bank fees: {str(e)}")
def display(self):
print(f"Account Number: {self.account_number}")
print(f"Account Owner: {self.name}")
print(f"Balance: {self.balance}")
account1 = BankAccount(account_number=7143, name="Mahesh", balance=3700)
account1.display()
account1.deposit(500)
account1.withdrawal(0)
account1.bank_fees()
account1.display()

Output

4|Pa ge

You might also like