Python

Classes & Objects Basics

Learn the fundamentals of Object-Oriented Programming. Create classes, instantiate objects, and understand the power of OOP.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Learn the fundamentals of Object-Oriented Programming. Create classes, instantiate objects, and understand the power of OOP. This hands-on tutorial focuses on practical implementation of classes & objects basics concepts.

Classes & Objects Basics

Welcome to Object-Oriented Programming (OOP)! This is a powerful way to organize your code by grouping related data and functions together.

What is a Class?

A Class is a blueprint for creating objects. Think of it like a cookie cutter - the class is the cutter, and objects are the cookies.

A Class defines:

  • Attributes (data/variables)
  • Methods (functions)

An Object is an instance of a class - a specific cookie made from the cutter.

Creating Your First Class

Use the class keyword to define a class.

PYTHON PLAYGROUND
⏳ Loading editor…

The __init__ Constructor

The __init__ method is called automatically when you create an object. It initializes the object's attributes.

PYTHON PLAYGROUND
⏳ Loading editor…

Understanding self

self represents the instance of the class. It's how methods access the object's attributes.

Key Points:

  • self is always the first parameter in methods
  • Python passes it automatically
  • You can name it anything, but self is convention

Adding Methods

Methods are functions that belong to a class.

PYTHON PLAYGROUND
⏳ Loading editor…

Instance Attributes vs Class Attributes

Instance attributes are unique to each object.
Class attributes are shared by all instances.

PYTHON PLAYGROUND
⏳ Loading editor…

String Representation: __str__ and __repr__

These special methods control how your object is displayed.

  • __str__: User-friendly string (for print())
  • __repr__: Developer-friendly string (for debugging)
PYTHON PLAYGROUND
⏳ Loading editor…

Memory Model: Multiple Instances

Each object has its own memory space for instance attributes.

Coding Challenge: BankAccount Class 🏦

Create a BankAccount class with:

  • Attributes: owner, balance
  • Methods: deposit(amount), withdraw(amount), get_balance()
PYTHON PLAYGROUND
⏳ Loading editor…

AI Mentor

Confused about "Python classes objects OOP basics"? Ask our AI mentor for a simplified explanation.

Quiz

Quiz

Question 1 of 5

What is the purpose of __init__?

To delete an object
To initialize object attributes
To print the object
To create a class

Key Takeaways

Classes are blueprints, objects are instances.
__init__ initializes object attributes.
self refers to the current instance.
Instance attributes are unique per object.
Class attributes are shared by all instances.
__str__ and __repr__ control string representation.

What's Next?

In the next lesson, we'll learn about Encapsulation & Properties - how to protect and validate your data.

Keep coding! 🚀