Member-only story

11 Essential Python Programs to Master Object-Oriented Programming (OOP)

Python Coding
3 min readSep 12, 2024

--

1. Basic Class and Object Creation

class Car:
def __init__(self, model, color):
self.model = model
self.color = color

def display_info(self):
print(f'Model: {self.model}, Color: {self.color}')

car1 = Car("Toyota", "Red")
car1.display_info()
Model: Toyota, Color: Red

2. Class with Methods

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

def area(self):
return 3.14 * self.radius ** 2

circle1 = Circle(5)
print("Area:", circle1.area())
Area: 78.5

3. Encapsulation (Private Attributes)

class Employee:
def __init__(self, name, salary):
self.__name = name
self.__salary = salary

def get_salary(self):
return self.__salary

emp1 = Employee("John", 5000)
print(emp1.get_salary())
5000

4. Inheritance

class Animal:
def sound(self):
return "Some sound"…

--

--

Python Coding
Python Coding

Written by Python Coding

Learn python tips and tricks with code I Share your knowledge with us to help society. Python Quiz: https://www.clcoding.com/p/quiz-questions.html

Responses (2)