Day 10: Object-Oriented Programming (OOP) in Python
On this page
Day 10: Object-Oriented Programming (OOP) in Python
Topics to Cover:
- Classes and Objects
- Methods
- Inheritance
Introduction to OOP
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of “objects,” which can contain data and code. Data is in the form of fields (attributes or properties), and code is in the form of procedures (methods).
Classes and Objects
A class is a blueprint for creating objects. An object is an instance of a class.
Defining a Class:
class BankAccount:
def __init__(self, account_number, balance=0):
self.account_number = account_number
self.balance = balance
Creating an Object:
account = BankAccount("123456789")
print(account.account_number) # Output: 123456789
print(account.balance) # Output: 0
Methods
Methods are functions defined inside a class. They are used to define the behaviors of an object.
Example: Adding Methods to the BankAccount Class:
class BankAccount:
def __init__(self, account_number, balance=0):
self.account_number = account_number
self.balance = balance
def deposit(self, amount):
self.balance += amount
return self.balance
def withdraw(self, amount):
if amount > self.balance:
return "Insufficient funds"
self.balance -= amount
return self.balance
def get_balance(self):
return self.balance
Using Methods:
account = BankAccount("123456789", 100)
print(account.deposit(50)) # Output: 150
print(account.withdraw(30)) # Output: 120
print(account.get_balance()) # Output: 120
Inheritance
Inheritance allows a class to inherit attributes and methods from another class.
Example: Base Class Animal and Derived Classes Dog and Cat:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
pass
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"
class Cat(Animal):
def speak(self):
return f"{self.name} says Meow!"
# Creating Objects of Derived Classes
dog = Dog("Buddy")
cat = Cat("Whiskers")
print(dog.speak()) # Output: Buddy says Woof!
print(cat.speak()) # Output: Whiskers says Meow!
Potential Problems to Solve
Problem 1: Model a Simple Bank Account
Task: Create a class to model a simple bank account.
Solution:
class BankAccount:
def __init__(self, account_number, balance=0):
self.account_number = account_number
self.balance = balance
def deposit(self, amount):
self.balance += amount
return self.balance
def withdraw(self, amount):
if amount > self.balance:
return "Insufficient funds"
self.balance -= amount
return self.balance
def get_balance(self):
return self.balance
# Example Usage
account = BankAccount("123456789", 100)
print(account.deposit(50)) # Output: 150
print(account.withdraw(30)) # Output: 120
print(account.get_balance()) # Output: 120
Problem 2: Implement Inheritance
Task: Create a base class Animal and derived classes Dog and Cat.
Solution:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
pass
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"
class Cat(Animal):
def speak(self):
return f"{self.name} says Meow!"
# Example Usage
dog = Dog("Buddy")
cat = Cat("Whiskers")
print(dog.speak()) # Output: Buddy says Woof!
print(cat.speak()) # Output: Whiskers says Meow!
Conclusion
Understanding OOP principles is fundamental to writing clean and efficient Python code. Classes and objects help in organizing code, while inheritance promotes code reuse.
Stay tuned for Day 11 of the python4ai 30-day series, where we’ll continue exploring advanced Python topics to enhance our programming skills!
More in PythonForAI
See all →- Day 14: Matplotlib for Data Visualization Topics to Cover: Introduction to Matplotlib Basic Plotting Introduction to Matplotlib Matplotlib is a popular plotting library for Python. I...
- Day 13: Pandas for Data Manipulation Topics to Cover: Introduction to Pandas DataFrames and Basic Operations Introduction to Pandas Pandas is a powerful data manipulation and an...
- Day 12: NumPy for Numerical Computation Topics to Cover: Introduction to NumPy Arrays and Basic Operations Introduction to NumPy NumPy is a powerful numerical computing library in...
- Day 11: Recursion and Iterators in Python Topics to Cover: Basics of Recursion and Recursive Functions Iterators, Iterable Objects, and the iter() Function Introduction to Recursion...
- Day 9: Exception Handling in Python – Python4AI Day 9: Exception Handling in Python Topics to Cover: Try-Except Blocks Custom Exceptions Introduction to Exception Handling In programming,...
- PythonForAI Day 8: Advanced Data Structures Welcome to Day 8 of PythonForAI! Today, we will delve into advanced data structures, specifically sets and advanced list comprehensions. The...