Introduction to Classes and Objects
Start thinking in objects. Learn the basics of Object-Oriented Programming (OOP) in Python.
Learning Objectives
- Understand the concept of Classes and Objects
- Define a basic class in Python
- Create objects from a class
- Understand attributes (data) and methods (behavior)
Thinking in Objects
Until now, we’ve focused on Procedural Programming—writing a list of instructions for the computer to follow. As programs grow, it becomes easier to organize code around “things” (Objects) rather than just “actions”.
Object-Oriented Programming (OOP) allows us to group data (attributes) and functionality (methods) together into a single unit called an Object.
Classes vs. Objects
Think of a Class as a blueprint or a recipe. It describes what something is and what it can do. An Object is an actual instance created from that blueprint.
- Class: Dog (The concept)
- Object: Your pet “Rex” (The real dog)
Defining a Class
In Python, we use the class keyword.
|
|
Attributes (Data)
Objects can store data. These are called Attributes. We can assign them directly to an object.
|
|
Methods (Behavior)
Functions defined inside a class are called Methods. They define what the object can do.
|
|
What is self?
Notice the self parameter in the method. self refers to the specific instance of the object that is calling the method. It allows the function to access that object’s own data.
Interactive Practice
Imagine you are building a game. You might have a Player class. What attributes (like health or name) and methods (like jump or attack) would it have?
Quiz
Complete this quiz with a minimum score of 80% to mark Day 15 as complete.
Discussion
Have questions or want to discuss this lesson? Join the conversation below!