[go: up one dir, main page]

0% found this document useful (0 votes)
110 views4 pages

Python OOP Basics & Examples

The document contains code examples demonstrating object-oriented programming concepts in Python including class definitions, inheritance, method definitions, and object instantiation. Several classes are defined including Person, Student, Rectangle, Circle, and BankAccount with methods like display(), getAge(), setAge(), perimeter(), area(), deposit(), withdraw(), and interest1(). Objects are instantiated from these classes and their methods are called to demonstrate class functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
110 views4 pages

Python OOP Basics & Examples

The document contains code examples demonstrating object-oriented programming concepts in Python including class definitions, inheritance, method definitions, and object instantiation. Several classes are defined including Person, Student, Rectangle, Circle, and BankAccount with methods like display(), getAge(), setAge(), perimeter(), area(), deposit(), withdraw(), and interest1(). Objects are instantiated from these classes and their methods are called to demonstrate class functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 4

3/16/23, 8:26 PM OOPS-Python - Jupyter Notebook

In [13]:

class Person:
def __init__(self,name,age,gender):
self.name=name
self.age=age
self.gender=gender
def display(self):
print("Name :",self.name)
print("Age :",self.age)
print("Gender :",self.gender)
def getAge(self):
print(self.age)
def setAge(self,age):
self.age=age
print("The person age has been set to :",self.age)

p=Person("Shiv",12,"Male")
p.display()
p.getAge()
p.setAge(30)
p.getAge()
p.display()

Name : Shiv
Age : 12
Gender : Male
12
The person age has been set to : 30
30
Name : Shiv
Age : 30
Gender : Male

2 ¶

localhost:8888/notebooks/Python Note/OOPS-Python.ipynb# 50/53


3/16/23, 8:26 PM OOPS-Python - Jupyter Notebook

In [35]:

class Person:
def __init__(self,fullname,age):
self.n=fullname
self.a=age

def show(self):
print("Full name :",self.n)
print("Age :",self.a)

class Student(Person):

def __init__(self,fullname,age,Id,major,GPA):
Person.__init__(self,fullname,age)
self.Id=Id
self.major=major
self.GPA=GPA

def getstudentinfo(self):
print(self.Id)
print(self.major)
print(self.GPA)

def setstudentinfo(self,Id,major,GPA):
self.Id=Id
self.major=major
self.GPA=GPA
print("Please find the revised details :")

p=Person("Pooja Rai",25)
print("Person details")
p.show()

print("student details :")


s=Student("Pooja Rai",25,"102A","Microbiology",8.5)
s.getstudentinfo()

s.setstudentinfo("103A","surgery",9)
s.getstudentinfo()

Person details
Full name : Pooja Rai
Age : 25
student details :
102A
Microbiology
8.5
Please find the revised details :
103A
surgery
9

localhost:8888/notebooks/Python Note/OOPS-Python.ipynb# 51/53


3/16/23, 8:26 PM OOPS-Python - Jupyter Notebook

In [2]:

class Rectangle:
def __init__(self,width,height):
self.w=width
self.h=height
def perimeter(self):
print("Perimeter of rectangle is :",2*(self.w+self.h))
def area(self):
print("Area of rectangle is :",(self.w*self.h))

width=float(input("Enter the width of the rectangle: "))


height=float(input("Enter the height of the rectangle: "))

r=Rectangle(width,height)

r.perimeter()

r.area()

Enter the width of the rectangle: 8


Enter the height of the rectangle: 4
Perimeter of rectangle is : 24.0
Area of rectangle is : 32.0

4
In [3]:

class Circle:
def __init__(self,radius):
self.r=radius

def perimeter(self):
print("Perimeter of circle is :",(2*3.14*self.r))
def area(self):
print("Area of circle is :",(3.14*self.r**2))

radius=float(input("Enter the radius of the circle: "))

r=Circle(radius)

r.perimeter()

r.area()

Enter the radius of the circle: 4


Perimeter of circle is : 25.12
Area of circle is : 50.24

localhost:8888/notebooks/Python Note/OOPS-Python.ipynb# 52/53


3/16/23, 8:26 PM OOPS-Python - Jupyter Notebook

In [6]:

class BankAccount:

def __init__(self,balance,interest_rate):
self.b=balance
self.r=interest_rate

def deposit(self,amount):
self.b=self.b+amount

def withdraw(self,amount):
self.b=self.b-amount

def interest1(self):
interest=self.b*self.r
self.b=self.b+interest
print("Current balance :",self.b)

a1=BankAccount(10000,0.07)
a1.deposit(1000)
a1.withdraw(100)
a1.interest1()

Current balance : 11663.0

In [ ]:

localhost:8888/notebooks/Python Note/OOPS-Python.ipynb# 53/53

You might also like