0% found this document useful (0 votes)
4 views8 pages

Saanp pt.4

The document contains multiple practical programming exercises demonstrating various concepts in Python, including class definitions, method overloading, inheritance, and exception handling. It showcases examples such as displaying data types, calculating areas of shapes, and managing employee and student information. Additionally, it includes error handling for division by zero and custom exceptions for password validation.

Uploaded by

Samruddhi Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Saanp pt.4

The document contains multiple practical programming exercises demonstrating various concepts in Python, including class definitions, method overloading, inheritance, and exception handling. It showcases examples such as displaying data types, calculating areas of shapes, and managing employee and student information. Additionally, it includes error handling for division by zero and custom exceptions for password validation.

Uploaded by

Samruddhi Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Practical No.

14

Q1.

class PrintData:
def display(self, n=None, c=None):
if isinstance(n, int) and isinstance(c, str):
print(f"Integer: {n}, Character: {c}")
elif isinstance(n, str) and isinstance(c, int):
print(f"Character: {n}, Integer: {c}")
else:
print("Invalid input")

# Example Usage
obj = PrintData()
obj.display(10, 'A') # Integer first, then Character
obj.display('B', 20) # Character first, then Integer

Output :

Q2.

class Shape:

def area(self, length, breadth=None):

if breadth is None:

print(f"Area of Square: {length * length}")

else:

print(f"Area of Rectangle: {length * breadth}")


# Example Usage

shape = Shape()

shape.area(5) # Square with side 5

shape.area(4, 6) # Rectangle with length 4 and breadth 6

Output:

Q3.

class Degree:
def getDegree(self):
print("I got a degree")

class Undergraduate(Degree):
def getDegree(self):
print("I am an Undergraduate")

class Postgraduate(Degree):
def getDegree(self):
print("I am a Postgraduate")

# Example Usage
degree = Degree()
undergrad = Undergraduate()
postgrad = Postgraduate()

degree.getDegree()
undergrad.getDegree()
postgrad.getDegree()
Output :
Practical No.15

Q1.

Output :

class Employee:

def __init__(self, name, department, salary):

self.name = name

self.department = department

self.salary = salary

def display_info(self):

print(f"Employee Name: {self.name}")

print(f"Department: {self.department}")

print(f"Salary: {self.salary}")

# Example Usage

emp1 = Employee("John Doe", "IT", 50000)

emp1.display_info()

Output :
Q2.

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def display(self):
print(f"Name: {self.name}")
print(f"Age: {self.age}")

class Student(Person):
def __init__(self, name, age, student_id, course):
super().__init__(name, age)
self.student_id = student_id
self.course = course

def display(self):
super().display()
print(f"Student ID: {self.student_id}")
print(f"Course: {self.course}")

# Example Usage
student1 = Student("Tanay", 18, "1505", "Computer Engineering")
student1.display()

Output :
Q3.

class Parent1:
def feature1(self):
print("Feature 1 from Parent 1")

class Parent2:
def feature2(self):
print("Feature 2 from Parent 2")

class Child(Parent1, Parent2):


def feature3(self):
print("Feature 3 from Child")

# Example Usage
child_obj = Child()
child_obj.feature1() # Inherited from Parent1
child_obj.feature2() # Inherited from Parent2
child_obj.feature3() # Child's own method

Output :
Practical No.16

Q1.

def divide_numbers(a, b):

try:

result = a / b

print(f"Result: {result}")

except ZeroDivisionError:

print("Error: Division by zero is not allowed.")

# Example Usage

num1 = int(input("Enter numerator: "))

num2 = int(input("Enter denominator: "))

divide_numbers(num1, num2)

Output :

Q2.

class InvalidPasswordError(Exception):

"""Custom exception for invalid password."""

pass

def check_password(user_input):

correct_password = "secure123" # Set the correct password


if user_input != correct_password:

raise InvalidPasswordError("Incorrect Password! Access Denied.")

else:

print("Login Successful!")

# Example Usage

try:

user_password = input("Enter password: ")

check_password(user_password)

except InvalidPasswordError as e:

print(e)

Output :

You might also like