Skip to content
Python Programming

Object-Oriented Programming in Python

Beginner Lesson 6 of 10

OOP is a programming paradigm based on objects containing data and methods.

class Dog:
species = "Canis familiaris" # Class attribute
def __init__(self, name, age):
self.name = name # Instance attribute
self.age = age
def bark(self):
return f"{self.name} says Woof!"
# Create objects
buddy = Dog("Buddy", 3)
print(buddy.bark()) # Buddy says Woof!
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
raise NotImplementedError
class Cat(Animal):
def speak(self):
return f"{self.name} says Meow!"
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"
class BankAccount:
def __init__(self, balance):
self._balance = balance # Protected
self.__pin = "1234" # Private
@property
def balance(self):
return self._balance
def deposit(self, amount):
if amount > 0:
self._balance += amount

Continue to Modules →