Python Object Oriented Programming

We know that Python is an object oriented programming language. So it's main focus is class and object. Here when a function will be invoked by object then function definition will execute.

In object oriented programming language,object is a memory containing data and function to manipulate data.


Python Class and object

As we know a class is a grouping of objects having identical properties, common behavior, and shared some relationships to perform a specified task.

Example: a computer system, a vehicle etc.

But in programming point of view a class is a user-defined data type which fully defines an object. That means without a class, we cant declare an object

An object is a named memory allocation which contains class property.

A class contains data or function or data and function which is considering as class property.

Once a class is declared, we can take any no of objects belonging to that class.

Python classes contain all the standard features of Object Oriented Programming. Lets see an example for better understanding:


Example 1:

#Develop a python program to display a message
class MyExample1:
	def show(self);
		print("python means pythontpoints.com")

obj=MyExample1()     #object declaration
obj.show()           #Message Passing

Output

Python means pythontpoints.com


Example 2:

#Develop a python program to display your full name
class MyExample2:
	def get(self,fname,lname);
		self.fname=fname
		self.lname=lname

	def show(self):
		print("The full name is",self.fname,self.lname)

obj=MyExample2()     #object declaration
obj.get("Sanghamitra","Mohanty")
obj.show()

Output

The full name is Sanghamitra Mohanty


Example 3:

#Develop a python program to find the area of a trianle
class AreaTriangle:
	def get(self,base,height):
		self.base=base
		self.height=height

	def area(self):
		a=0.5 * self.base * self.height
		print("The area of triangle is", a)

obj=AreaTriangle()
obj.get(2.3,4.5)
obj.area()

Output

The area of triangle is 5.175


Example 4:

#Develop a python program to find the factorial of a number using 
class and object

class Factorial:
	def get(self,n):
		self.n=n

	def fact(self):
		i=1
		f=1
 		while i<=self.n:
 			f=f*i
 			i+=1
 		print("the factorial is",f)


obj=Factorial()      #object declaration
obj.get(5)
obj.fact()


Output

the factorial is 120


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext