0% found this document useful (0 votes)
3 views5 pages

OOP Program Examples in Python

The document contains Python code examples demonstrating the use of classes and objects, including constructors, methods, inheritance, and operator overloading. It features various classes such as Person, Employee, Student, and Circle, showcasing how to create instances, manipulate data, and implement functionality. Additionally, it illustrates concepts like method overriding and the use of super() for inheritance.

Uploaded by

Tanisha Jena
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)
3 views5 pages

OOP Program Examples in Python

The document contains Python code examples demonstrating the use of classes and objects, including constructors, methods, inheritance, and operator overloading. It features various classes such as Person, Employee, Student, and Circle, showcasing how to create instances, manipulate data, and implement functionality. Additionally, it illustrates concepts like method overriding and the use of super() for inheritance.

Uploaded by

Tanisha Jena
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

1/24/25, 9:06 PM Class_and_objects.

ipynb - Colab

#Generic Example
x = list()
[Link]('python')
[Link]('c++')
[Link]()
print (x[0])
print (x.__getitem__(0))
print (list.__getitem__(x,0))

c++
c++
c++

#Simple class with constructor and a method


class Person:
def __init__(self,name, age, coll):
[Link] = name
[Link] = age
[Link] = coll
def display(self):
print("Name is ",[Link])
p1=Person("John", 35, "BMS")
p2=Person("Amar", 38, "BMS")
[Link]()
[Link]()

Name is John
Name is Amar

#class with constructor and a method with parameter


class Emp:
def __init__(self,name, id):
[Link] = name
[Link] = id

def sal_hike(self, sal):


if sal<10000:
new_sal = sal+0.3*sal
print("Dear ",[Link],"your ID is ",[Link]," and your updated salary is ", new_sal)
elif sal>=10000 and sal<20000:
new_sal = sal+0.2*sal
print(f"Dear {[Link]} your ID is {[Link]} and your updated salary is {new_sal}")#Notice the use of print f

e1 = Emp("Ram", 12)
e1.sal_hike(7200)

e2 = Emp("Shyam", 15)
e2.sal_hike(12500)

Dear Ram your ID is 12 and your updated salary is 9360.0


Dear Shyam your ID is 15 and your updated salary is 16250.0

#Develop a Python program to create a class Student with instance variables usn, name and marks.
#Create two objects and display the details of the students who has got the highest mark.

class Student:
def __init__(self, usn,name,marks):
[Link] = usn
[Link] = name
[Link] = marks
def max_marks(self,ls):
h = ls[0].marks
for i in range(1, len(ls)):
if ls[i].marks> h:
h = ls[i].marks
print(f"The student {ls[i].name} has score the highest ie {ls[i].marks}")

ls = []
s1 = Student(12, "ab", 42)
[Link](s1)
s2 = Student(15, "xy", 45)
[Link](s2)
s3 = Student(9, "pq", 43)
[Link](s3)
s1.max_marks(ls)

[Link] 1/5
1/24/25, 9:06 PM Class_and_objects.ipynb - Colab

The student pq has score the highest ie 43

class circle:
def __init__(self):
r=float(input("Enter radius of the circle"))
[Link]=r
def area(self):
[Link]=3.142*[Link]*[Link]
#return [Link]
def perimeter(self):
[Link]=2*3.142*[Link]
def display(self):
print("The radius of circle is:",[Link])
print("The area of circle is:",[Link])
print("The perimeter of circle is:",[Link])

c1=circle()
[Link]()
[Link]()
[Link]()

Enter radius of the circle10


The radius of circle is: 10.0
The area of circle is: 314.2
The perimeter of circle is: 62.839999999999996

class Employee:
def __init__(self, name, pay): # Initialize when created
[Link] = name # self is the new object
[Link] = pay
def lastName(self):
return [Link]()[-1] # Split string on blanks
def giveRaise(self, percent):
[Link] *= (1.0 + percent) # Update pay in-place

e1 = Employee ('Bob Smith', 50000)


e2 = Employee ('Sue Jones', 60000) # Each has name and pay attributes
print("The last name of e1 is",[Link]())
print("The last name of e2 is",[Link]())
[Link](.13)
[Link](.12)
print("E1's new salary = ",[Link])
print("E2's new salary = ",[Link])

The last name of e1 is Smith


The last name of e2 is Jones
E1's new salary = 56499.99999999999
E2's new salary = 67200.0

#Inheritance Example
#Parent class
class Person:
def __init__(self, fname,lname):
[Link] = fname
[Link] = lname
def print(self):
print(f" First name is {[Link]} and last name is {[Link]}")
#instances of the parent
p1 = Person("Virat", "Kohli")
[Link]()

#Child class that is using the parent class' init


class Player(Person):
pass

#Child class instance


s1 = Player("KL", "Rahul")
[Link]()

First name is Virat and last name is Kohli


First name is KL and last name is Rahul

#Parent class
class Person:
def __init__(self, fname,lname):
print("Parent class constructor")
[Link] = fname
[Link] = lname

[Link] 2/5
1/24/25, 9:06 PM Class_and_objects.ipynb - Colab
def print(self):
print("Parent method")
print(f"First name is {[Link]} and last name is {[Link]}")
#Child class that has defined its own version of __init__()
class Player(Person):
def __init__(self, fname,lname):

print("Child constructor")
[Link] = fname
[Link] = lname

'''def print(self):
print("Child Method")'''
#Child class instance
s1 = Player("KL", "Rahul")
[Link]()

Child constructor
Parent method
First name is KL and last name is Rahul

#Parent class
class Person:
def __init__(self, fname,lname):
print("Parent class constructor")
[Link] = fname
[Link] = lname
def print(self):
print(f"First name is {[Link]} and last name is {[Link]}")

#Child class that is calling the Parent class constructor explicitly


class Player(Person):
def __init__(self, fname,lname):
Person.__init__(self, fname, lname)

#Child class instance


s1 = Player("KL", "Rahul")
[Link]()

Parent class constructor


First name is KL and last name is Rahul

#Parent class
class Person:
def __init__(self, fname,lname):
print("Parent class constructor")
[Link] = fname
[Link] = lname
def print(self):
print(f"First name is {[Link]} and last name is {[Link]}")

#Child class that is calling the Parent class constructor using super
class Player(Person):
def __init__(self, fname,lname):
super().__init__(fname, lname)

#Child class instance


s1 = Player("KL", "Rahul")
[Link]()

Parent class constructor


First name is KL and last name is Rahul

#Parent class
class Person:
def __init__(self, fname,lname):
print("Parent class constructor")
[Link] = fname
[Link] = lname
def print(self):
print(f"First name is {[Link]} and last name is {[Link]}")

#Child class that is calling the Parent class constructor using super
class Player(Person):
def __init__(self, fname,lname, play_type):
super().__init__(fname, lname)
self.play_type = play_type

[Link] 3/5
1/24/25, 9:06 PM Class_and_objects.ipynb - Colab
#Child class instance
s1 = Player("KL", "Rahul","Right handed")
[Link]()
print(s1.play_type)

Parent class constructor


First name is KL and last name is Rahul
Right handed

class num_ex:
def __init__(self, n):
self.n = n
def __add__(self, o):
print("Overloaded")
return self.n+o.n
ob1 = num_ex(10)
ob2 = num_ex(20)
ob3 = num_ex(30)
print(type(ob1))
#print(ob1+ob2)
#print(ob1.__add__(ob2))
print(ob3.__add__(ob2))

<class '__main__.num_ex'>
Overloaded
30

class complex_num:
def __init__(self,r,i):
self.r = r
self.i = i
def __add__(self, c):
print("Overloaded")
return self.r + c.r,
ob1 = complex_num(10, 12)
ob2 = complex_num(20, 22)
ob3 = ob1+ob2
print(type(ob1))

Overloaded
<class '__main__.complex_num'>

class Person:
def __init__(self, name, age):
[Link] = name
[Link] = age

def __lt__(self, ob):


return [Link]<[Link]
p1 = Person("Alice", 20)
p2 = Person("Bob", 30)

print(p1<p2)

True

Start coding or generate with AI.

[Link] 4/5
1/24/25, 9:06 PM Class_and_objects.ipynb - Colab

[Link] 5/5

You might also like