If you want to take your programming skills to the next level, Python is your language. Python object-oriented programming language offers many advantages over traditional programming languages. Its syntax is relatively straightforward and easy to read, code, and maintain.
This article will introduce you to Python Object Oriented Programming basics and explain how these concepts can be used to create more complex applications. Ready to start learning Python? Let us get started.
Table of Contents
What is OOP ?
Object-oriented programming is a popular concept that allows creating complex software by breaking down functionality into manageable objects. These objects are then easy to use and implement, making them ideal for reusable code. This makes object-oriented programmers well suited for creating applications that have multiple layers or tiers of functionality. Additionally, it helps modularise an application so that different parts can be easily replaced or updated without affecting other system parts.
1.Class
In Python, a class definition is an object that represents the common properties and methods shared by all instances of that. For example, classes can divide your code into easy-to-read and understand blocks, making it possible to reuse pieces of code multiple times without having to write the same code twice.
2.Object
In Python, an object is a data collection that can be accessed and manipulated using unique methods. Objects are used to represent instances of particular classes (which you create), and they are nearly always created when a script starts execution.
In the following code example, we have created a class named Car using the class keyword and object, and by using the init function, we have assigned values, let us have a look on code to understand it better,
class Car:
def __init__(model, colour, year):
model.colour = colour
model.year = year
p1 = Car("Grey toyota", 2018)
print(p1.colour)
print(p1.year)
Output

3.Method
In Python, a method is simply an executable block of code that you can use to perform a specific task. It typically takes one or more arguments (also known as parameters) and returns one or more values. For example, you can call methods directly from your code or import them into your project to be accessible at runtime.
class Car:
def __init__(model, colour, year):
model.colour = colour
model.year = year
def myfunc(model):
print("Car model and colour is " + model.colour)
p1 = Car("Grey Toyota", 2018)
p1.myfunc()
Output

4.Inheritance
Inheritance in Python is passing characteristics and properties from one object to another. It can be done using either class-based or instance-based inheritance. Class-based inheritance works by defining a set of common attributes that all instances of a particular class will share. On the other hand, instance-based inheritance allows you to inherit specific characteristics or properties from an individual class instance instead of the entire class.
# parent class
class samsung:
def __init__(self):
print("samsung is korean company makes galaxy")
# child class
class galaxy(samsung):
def __init__(self):
super().__init__()
print("galaxy is a smart phones")
def launch(self):
print("launch in 2009")
sam= galaxy()
sam.launch()
Output

There are two methods of inheritance:
Method Overloading
In programming, method overloading uses multiple methods with the same name on a class or object. This can be useful when you create flexible code that can be used in different situations.
def divide(a, b):
d = a / b
print(d)
def product(a, b, c):
d = a / b/c
print(d)
product(30, 10, 5)
Output

Method Overriding
Method overriding is a feature of the Python programming language that allows you to override methods on an instance. This can be useful in cases where you need to access specific details or behaviour of a method without using its full signature.
class Parent():
def __init__(self):
self.value = "Parent"
def show(self):
print(self.value)
class Child(Parent):
def __init__(self):
self.value = "Child"
def show(self):
print(self.value)
obj1 = Parent()
obj2 = Child()
obj1.show()
obj2.show()
Output

5.Destroying Objects (Garbage Collection)
Garbage collection in Python is when objects are no longer needed and have reclaimed memory occupied by built-in types or class instances. This is typically done when an application runs out of memory and, as a result, begins to crash or experience other performance problems. By relying on garbage collection, programs can run more smoothly and reliably because they do not have to keep track of all the objects that may have been forgotten.
#gc=> garbage collector
import gc
print("Garbage collection:",
gc.get_threshold())
Output

6.Encapsulation
Encapsulation is a way of protecting data within the bounds of a module. This technique helps prevent external data access, which can be dangerous if not properly secured. Using the statement, you can create an instance of a class that will only have access to specified attributes and methods of another class.
class ipad:
def __init__(self):
self.__maxprice = 50000
def sell(self):
print("Selling Price: {}".format(self.__maxprice))
def setMaxPrice(self, price):
self.__maxprice = price
c = ipad()
c.sell()
# change the price
c.__maxprice = 900
c.sell()
# using setter function
c.setMaxPrice(900)
c.sell()
Output

7.Polymorphism
Polymorphism is a feature of Python that allows you to simultaneously have several versions of the same object in your code. This can be helpful when handling different types of inputs or if you need to easily switch between implementations without affecting the rest of your program. For example, polymorphism makes it easy to create generic functions and objects that work with multiple data types without specifying which type they should use.
class animal:
def intro(self):
print("There are many types of animal.")
def voice(self):
print("Most of the animal has voice but some has not.")
class dog(animal):
def voice(self):
print("dogs bark.")
class rabbit(animal):
def voice(animal):
print("rabbits have no voice.")
obj_animal = animal()
obj_dg = dog()
obj_rab = rabbit()
obj_animal.intro()
obj_animal.voice()
obj_dg.intro()
obj_dg.voice()
obj_rab.intro()
obj_rab.voice()
Output

Learn more in detail about Polymorphism in Python.
8.Data Abstraction
Data abstraction is a process of defining data structure and can be done through creating abstract classes so that it can be easily accessed and manipulated. This allows for more accessible analysis, processing, storage, and communication across systems. It can also help to reduce complexity and increase organization within data structures.
from abc import ABC, abstractmethod
class bike(ABC):
def move(self):
pass
class honda(bike):
def move(self):
print("honda is japanese multinatioal company")
class superstar(bike):
def move(self):
print("super star is Pakisan's manufacturer memon motors")
h = honda()
h.move()
s = superstar()
s.move()
Output

9.Data Hiding
Data hiding in python is a technique that allows you to hide data from view. This can be useful when you do not want users to see sensitive information or if you are confidentially processing the data. When working with data, it is often necessary to protect it from being accessed by unauthorized individuals.
class Solution:
__hideCounter = 0
def sum(self):
self.__hideCounter += 1
print(self.__hideCounter)
count = Solution()
count.sum()
count.sum()
#this give error message because there is no to access private member
print(count.__hideCount)
Output

10.Exception Handling
Exception handling is a feature of programming languages that allows exceptions to be handled gracefully. This helps ensure the correct code is executed in the event of an error and makes your application more robust. When an exception occurs, Python typically prints out traceback information and lets you handle the situation as needed.
marks = 50
a = marks / 0
print(a)
Output

Learn in detail about, Python Exception handling.
FAQs
What is the difference between OOP and SOP?
Structured programming allows building a application using a collection of modules or functions, whereas object-oriented programming allows creating a application using a collection of objects and their interactions.
Can you call the base class method without creating an instance in python?
Without creating an object or instance, static methods may be called. Simply call the method directly without creating it.
conclusion
In the end, we hope this article helped you understand the basic concepts Python Object Oriented Programming. Object-oriented programming is a faster and more scalable way to create complex software. It also allows us to develop applications in modular layers that can be reused and upgraded easily. From basic to advanced levels, we have everything covered to ensure you become a pro at Python programming in no time. Happy coding.