pythonAssignment1
pythonAssignment1
OUTPUT:
Assignment No:5
Aim: Write a python program to demonstrating the tuple.
PROGRAM:
# Demonstrating Tuple
print("Tuple Example:")
# Creating a tuple with multiple data types
student_info = ("John", 21, "Computer Science")
print("\n")
# Demonstrating Dictionary
print("Dictionary Example:")
# Creating a dictionary with key-value pairs
student_grades = {
"John": "A",
"Alice": "B",
"Bob": "C"
}
# Accessing values using keys
print("John's grade:", student_grades["John"])
print("Alice's grade:", student_grades["Alice"])
# Adding a new key-value pair to the dictionary
student_grades["Eve"] = "A"
print("Updated student grades:", student_grades)
# Modifying an existing value
student_grades["Bob"] = "B"
print("Modified student grades:", student_grades)
# Iterating over the dictionary
print("\nIterating through dictionary:")
for student, grade in student_grades.items():
print(f"{student}: {grade}")
OUTPUT:
Assignment No.6
Aim : Write a python program to display name,age and student id.
PROGRAM:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"Hi, my name is {self.name} and I am {self.age} years old.")
class Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age)
self.student_id = student_id
def introduce(self):
super().introduce()
print(f"My student ID is {self.student_id}.")
person1 = Person("Alice", 30)
person1.introduce()
student1 = Student("Bob", 20, "S12345")
student1.introduce()
OUTPUT:
Assignment No.7
Aim: Write a python program to perform following operations
1.Polymorphism 2.Encapsulation 3.Magic Method.
1.Polymorphism:
PROGRAM:
#Polymorphism Program
class Cat:
def sound(self):
return "Meow"
class Dog:
def sound(self):
return "Bark"
def make_sound(animal):
print(animal.sound())
cat = Cat()
dog = Dog()
make_sound(cat)
make_sound(dog)
OUTPUT:
2.Encapsulation:
PROGRAM:
#Encapsulation Program
class BankAccount:
def __init__(self, balance):
self.__balance = balance
def get_balance(self):
return self.__balance
# Encapsulation in action
account = BankAccount(1000)
account.deposit(500)
print(account.get_balance())
print(account.withdraw(300))
print(account.get_balance())
OUTPUT:
3.Magic Methods
PROGRAM:
#Magic Method Program
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
def __str__(self):
return f"'{self.title}' by {self.author}"
print(book1)
print(book1 + book2)
OUTPUT: