0% found this document useful (0 votes)
5 views6 pages

Python Exception Handling & Classes

Uploaded by

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

Python Exception Handling & Classes

Uploaded by

shrishail.ad24
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON PROGRAMS – EXCEPTION HANDLING, CLASSES, OVERLOADING

1. CHECK VALIDITY OF AGE

------------------------

try:

age = int(input("Enter age: "))

if age < 0:

raise Exception("Age cannot be negative!")

print("Valid age:", age)

except ValueError:

print("Enter a valid number!")

except Exception as e:

print(e)

2. INHERITANCE EXCEPTION – FATHER & SON

---------------------------------------

class WrongAge(Exception):

pass

class Father:

def __init__(self, age):

if age < 0:

raise WrongAge("Father age cannot be negative!")

[Link] = age

class Son(Father):

def __init__(self, fage, sage):

super().__init__(fage)

if sage < 0:

raise WrongAge("Son age cannot be negative!")

if sage >= fage:

raise WrongAge("Son age cannot be >= Father age!")


[Link] = sage

try:

obj = Son(40, 20)

print("Valid ages")

except WrongAge as e:

print(e)

3. INPUT A NUMBER – RAISE EXCEPTION IF NOT NUMBER

-------------------------------------------------

try:

num = int(input("Enter a number: "))

print("You entered:", num)

except ValueError:

print("Error: Not a number!")

4. DIVISION WITH MULTIPLE EXCEPTIONS + FINALLY

----------------------------------------------

try:

a = int(input("Enter first number: "))

b = int(input("Enter second number: "))

print("Result =", a / b)

except ZeroDivisionError:

print("Cannot divide by zero!")

except ValueError:

print("Enter integers only!")

finally:

print("Completed.")

5. TRY–EXCEPT–ELSE (COMPLEX ADDITION)

-------------------------------------

try:
r1 = int(input())

i1 = int(input())

r2 = int(input())

i2 = int(input())

except ValueError:

print("Enter only integers!")

else:

print("Sum =", r1+r2, "+ i", i1+i2)

6. CLASS STUDENT – HIGHEST MARKS

--------------------------------

class Student:

def __init__(self, usn, name, marks):

[Link] = usn

[Link] = name

[Link] = marks

def display(self):

print([Link], [Link], [Link])

s1 = Student("1", "A", 90)

s2 = Student("2", "B", 88)

if [Link] > [Link]:

[Link]()

else:

[Link]()

7. ACTOR – MINIMUM AVERAGE EARNING

----------------------------------

class Actor:

def __init__(self, actorid, name, no_movies, earnings):


[Link] = actorid

[Link] = name

self.no_movies = no_movies

[Link] = earnings

def avg(self):

return [Link] / self.no_movies

a1 = Actor(1, "A", 10, 200)

a2 = Actor(2, "B", 8, 120)

a3 = Actor(3, "C", 12, 400)

m = min([a1, a2, a3], key=lambda x: [Link]())

print("Min avg =", [Link], [Link]())

8. CIRCLE CLASS – ACCEPT, AREA, PERIMETER, DISPLAY

--------------------------------------------------

class Circle:

def __init__(self, r=0):

self.r = r

def accept(self):

self.r = float(input())

def area(self):

return 3.14 * self.r * self.r

def perimeter(self):

return 2 * 3.14 * self.r

def display(self):

print(self.r, [Link](), [Link]())

9. EMPLOYEE – SALARY, OVERTIME, DEDUCTION

-----------------------------------------
class Employee:

def accept(self):

[Link] = input()

[Link] = input()

[Link] = int(input())

[Link] = float(input())

[Link] = float(input())/100

[Link] = float(input())/100

[Link] = float(input())/100

def calculate(self):

[Link] = [Link] + [Link]*[Link] + [Link]*[Link] -


[Link]*[Link]

if [Link] > 200:

[Link] += ([Link] - 200) * 100

else:

[Link] -= (200 - [Link]) * 100

def display(self):

print("Gross =", [Link])

10. COMPLEX NUMBER ADDITION (OPERATOR OVERLOADING)

--------------------------------------------------

class Complex:

def __init__(self, r, i):

self.r = r

self.i = i

def __add__(self, other):

return Complex(self.r + other.r, self.i + other.i)

c1 = Complex(3, 2)

c2 = Complex(4, 5)
c3 = c1 + c2

print(c3.r, c3.i)

You might also like