lab 11 output
lab 11 output
class student:
def __init__(self,name,age,grade):
self.name=name
self.__age=age
self.__grade=grade
def display_info(self):
print("Name:",self.name)
print("Age:",self.__age)
print("Grade:",self.__grade)
s1 = student("ALI",18,"B")
print(s1.name)
print(s1.__age)
print(s1.__grade)
------------------------------------------------------
ALI
Traceback (most recent call last):
File "D:\New folder\open file.py", line 12, in <module>
print(s1.__age)
^^^^^^^^
AttributeError: 'student' object has no attribute '__age'
------------------------------------------------------
task 2:
class student:
def __init__(self,name,age,grade):
self.name=name
self.__age=age
self.__grade=grade
def get_age(self):
return self.__age
def get_grade(self):
return self.__grade
s1=student("Ali",18,"B")
print(s1.get_age())
print(s1.get_grade())
-----------------------------------------------------
18
B
--------------------------------------------------------
task 3
class student:
def __init__(self, name, age, grade):
self.name = name
self.__age = age
self.__grade = grade
def get_age(self):
return self.__age
def display_info(self):
print(f'Name: {self.name}, Age: {self.get_age()}, Grade:
{self.get_grade()}')
------------------------------------------------------
Getter methods are used to access the private attributes. __age and __grade
Setter methods are used to modify the private attributes of a class.set_age() and
set_grade() are used to update the values of the private attributes __age and
__grade
Ali
B
18
Age updated to 20
Grade updated to a
Invalid age. Must be between 5 and 25.
Invalid grade. Must be 'a', 'b', 'c', 'd', 'f'
Name: Ali, Age: 20, Grade: a
-----------------------------------------------
task 4
Alice
Deposited 500. New balance: 1500
Withdrawn 200. New balance: 1300
----------------------------------------------
lab Assign
class BankAccount:
def __init__(self, name, balance, account_type):
self.name = name
self.__balance = balance
self.__account_type = account_type
def get_balance(self):
return self.__balance
def get_account_type(self):
return self.__account_type
# Example usage
account = BankAccount("John Doe", 1000, "Savings")
print(account.name) # Public attribute, accessible directly