Introduction to Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP) is a programming paradigm that focuses on organizing code into “objects” that represent real-world entities. OOP makes it easier to manage complex programs by breaking them down into smaller, more manageable pieces.
Classes and Objects
In OOP, a class is a blueprint for creating objects, and an object is an instance of a class. You can define a class using the 'class' keyword, and create an object from a class using the class name followed by parentheses.
Example:
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
print("Woof!")
my_dog = Dog("Buddy", 3)
my_dog.bark()
Instance Methods and Attributes
Instance methods are functions defined inside a class, and attributes are variables associated with an object. You can define instance methods using the def keyword and access attributes using the self keyword.
Example:
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
print(f"{self.name} barks!")
my_dog = Dog("Fido")
my_dog.bark() # Output: Fido barks!
Class Methods and Attributes
Class methods and attributes are associated with the class itself, rather than individual objects. You can define class methods using the '@classmethod' decorator and class attributes by placing them outside of any methods.
Example:
class Dog:
species = "Canis lupus familiaris"
@classmethod
def describe(cls):
print(f"Dogs belong to the species {cls.species}")
Dog.describe() # Output: Dogs belong to the species Canis lupus familiaris
Hands-on Exercises
- Create a Python script that defines a
'Person'class with attributes for'first_name'and'last_name', and a method for printing the full name. - Create a Python script that defines a
'BankAccount'class with methods for depositing, withdrawing, and checking the balance.
Real-World Example
Object-Oriented Programming can be used to model complex systems, such as a company’s payroll system. You could create classes for employees, managers, and departments, with methods for calculating salaries, bonuses, and other financial information.
Additional Resources
- Python documentation: https://docs.python.org/3/tutorial/classes.html
- Real Python: https://realpython.com/python3-object-oriented-programming/
- W3Schools Python tutorial: https://www.w3schools.com/python/python_classes.asp
Review Questions
- What is the purpose of Object-Oriented Programming?
- What are the differences between instance methods and class methods in Python?
- Explain how to define a class and create an object in Python.
- Create a Python script that defines a
Carclass with methods for starting the engine, accelerating, and braking. - Define a
Studentclass with attributes for name and age, and a method for printing the student’s information.
Summary and Next Steps
In this lesson, you have learned about:
- Introduction to object-oriented programming
- Classes and objects in Python
- Instance methods and attributes
- Class methods and attributes
In the next lesson, you will learn about inheritance and polymorphism, which allow you to create more flexible and powerful code structures.
Remember to review the material periodically and practice your skills to reinforce your learning. Good luck on your journey to becoming a Python developer!



