PyLearn Object-Oriented Programming Python Classes
Lesson 43 Object-Oriented Programming

Python Classes

Learn how Python classes work, how objects are created, how attributes and methods are used, and how classes help you structure real-world programs.

45–55 minutes Beginner → Intermediate Object-Oriented Programming
0%
01

Learning Objectives

By the end of this lesson, you will understand how object-oriented programming begins with classes and objects.

You will learn how to:

  • Understand the idea of object-oriented programming.
  • Create Python classes.
  • Create objects from classes.
  • Work with attributes.
  • Create methods.
  • Understand the purpose of self.
  • Understand instance and class attributes.
  • Work with multiple objects.
  • Design simple real-world classes.
02

What Is Object-Oriented Programming?

Object-Oriented Programming (OOP) is a programming approach where programs are designed around objects.

An object can represent something from the real world, such as a student, customer, product, employee, car, bank account, or order.

Objects can contain both data and behavior.

Think in terms of real-world entities

A Product might have a name, price, and stock quantity. It might also have behaviors such as displaying details or updating stock.

Concept

Class → Blueprint

Object → Actual instance

Attribute → Data

Method → Behavior

03

What Is a Class?

A class is a blueprint or template used to create objects.

Instead of separately defining the structure and behavior of every student, for example, we can define a Student class and create many student objects from it.

Simple analogy

A house blueprint is not the actual house. Similarly, a class is not normally the actual object. It describes how objects should behave and what they can contain.

04

What Is an Object?

An object is an actual instance created from a class.

If Student is the class, then Ali and Sara can be individual objects created from that class.

Example
class Student:
    pass

student1 = Student()
student2 = Student()

Here, Student is the class and student1 and student2 are objects.

05

Class Syntax

Python uses the class keyword to define a class.

Python
class ClassName:
    # class body
    pass

Python conventionally uses PascalCase for class names.

Examples
class Student:
    pass

class Product:
    pass

class BankAccount:
    pass
06

Creating a Simple Class

A class can initially contain nothing more than the pass statement.

Python
class Student:
    pass

student1 = Student()

print(student1)

Why use pass?

pass tells Python that the class body intentionally contains no implementation yet. It is useful when creating a structure that you will expand later.

07

Creating Objects

To create an object, write the class name followed by parentheses.

Python
class Student:
    pass

student1 = Student()
student2 = Student()

print(type(student1))
print(type(student2))
Output
<class '__main__.Student'>
<class '__main__.Student'>
08

Attributes

Attributes are values or pieces of data associated with an object.

For example, a student might have a name, age, and course.

Python
class Student:
    pass

student1 = Student()

student1.name = "Ali"
student1.age = 22
student1.course = "Python"

print(student1.name)
print(student1.age)
print(student1.course)
Output
Ali
22
Python
09

Methods

A method is a function defined inside a class.

Methods usually describe something an object can do.

Python
class Student:

    def introduce(self):
        print("Hello, I am a student.")

student1 = Student()

student1.introduce()
Output
Hello, I am a student.

Important idea

A normal function exists independently, while a method is defined inside a class and normally operates on an object.

10

Understanding self

One of the most important concepts when learning Python classes is self.

self refers to the current object on which the method is being called.

Python
class Student:

    def introduce(self):
        print("Hello from the student object.")

student1 = Student()

student1.introduce()

When Python sees:

Method Call
student1.introduce()

Python effectively passes student1 as the first argument to the method.

Conceptual Equivalent
Student.introduce(student1)

Do not forget self

Instance methods normally need self as their first parameter.

11

Instance Attributes

Instance attributes belong to a particular object.

Different objects can have different values for the same attribute.

Python
class Student:

    def introduce(self):
        print("My name is", self.name)


student1 = Student()
student2 = Student()

student1.name = "Ali"
student2.name = "Sara"

student1.introduce()
student2.introduce()
Output
My name is Ali
My name is Sara

Notice that the same method works with both objects, because self refers to whichever object called the method.

12

Instance Methods

Instance methods can access and modify attributes of the current object through self.

Python
class Product:

    def show_details(self):
        print("Product:", self.name)
        print("Price:", self.price)


product = Product()

product.name = "Laptop"
product.price = 85000

product.show_details()
Output
Product: Laptop
Price: 85000
13

Working with Multiple Objects

One of the major benefits of classes is that we can create many objects using the same blueprint.

Python
class Product:

    def show_details(self):
        print(self.name, "-", self.price)


product1 = Product()
product2 = Product()
product3 = Product()

product1.name = "Laptop"
product1.price = 85000

product2.name = "Phone"
product2.price = 50000

product3.name = "Headphones"
product3.price = 8000

product1.show_details()
product2.show_details()
product3.show_details()
Output
Laptop - 85000
Phone - 50000
Headphones - 8000
14

Class Attributes

A class attribute is defined directly inside the class and is shared by objects unless an instance provides its own value.

Python
class Student:

    school = "PyLearn Academy"


student1 = Student()
student2 = Student()

print(student1.school)
print(student2.school)
Output
PyLearn Academy
PyLearn Academy

Both objects can access the shared class attribute.

15

Instance vs Class Attributes

Understanding the difference between these two types of attributes is very important in object-oriented programming.

Python
class Student:

    school = "PyLearn Academy"


student1 = Student()
student2 = Student()

student1.name = "Ali"
student2.name = "Sara"

print(student1.name)
print(student2.name)

print(student1.school)
print(student2.school)
Output
Ali
Sara
PyLearn Academy
PyLearn Academy

Remember

Instance attribute: belongs to a particular object.

Class attribute: belongs to the class and can be shared by its objects.

16

Updating Attributes

Object attributes can be changed after an object has been created.

Python
class Product:
    pass


product = Product()

product.name = "Laptop"
product.price = 85000

print(product.price)

product.price = 80000

print(product.price)
Output
85000
80000
17

Deleting Attributes & Objects

Python provides the del statement for deleting attributes or references to objects.

Python
class Student:
    pass


student = Student()

student.name = "Ali"
student.age = 22

del student.age

print(student.name)
Output
Ali

You can also delete an object reference:

Python
student = Student()

del student

Important

del student removes that variable reference. Python's memory management and garbage collection determine when an object is actually removed from memory.

18

Real-World Examples

Classes become especially useful when a program contains many entities with similar structure and behavior.

Example 1 — Bank Account

Python
class BankAccount:

    def show_balance(self):
        print("Balance:", self.balance)


account = BankAccount()

account.owner = "Ali"
account.balance = 50000

account.show_balance()

Example 2 — Car

Python
class Car:

    def start(self):
        print(self.brand, "is starting.")


car = Car()

car.brand = "Toyota"

car.start()

Example 3 — Employee

Python
class Employee:

    def show_details(self):
        print("Name:", self.name)
        print("Department:", self.department)


employee = Employee()

employee.name = "Sara"
employee.department = "Marketing"

employee.show_details()

Why this matters

Classes allow large programs to organize related data and behavior into meaningful structures.

19

Common Mistakes

1. Forgetting the colon

# Wrong
class Student

# Correct
class Student:
    pass

2. Forgetting indentation

# Wrong
class Student:
print("Hello")

# Correct
class Student:
    print("Hello")

3. Forgetting self

# Wrong
class Student:
    def introduce():
        print("Hello")

# Correct
class Student:
    def introduce(self):
        print("Hello")

4. Confusing class and object

The class is the blueprint. The object is an actual instance created from that blueprint.

5. Using an attribute that does not exist

class Student:
    pass

student = Student()

print(student.name)

This produces an AttributeError because name has not been assigned to the object.

20

Practice Exercises

Exercise 1 — Student Class

Create a Student class. Create one object and give it a name and age.

Exercise 2 — Product

Create a Product class. Create three products with different names and prices.

Exercise 3 — Method

Create a Car class with a method called start().

Exercise 4 — self

Create a Person class with a method that prints the person's name using self.

Exercise 5 — Multiple Objects

Create an Employee class and create three employee objects with different names and departments.

Exercise 6 — Class Attribute

Create a Student class with a class attribute called school.

Exercise 7 — Product Details

Create a product object with name, price, and stock. Write a method that displays all three values.

Exercise 8 — Bank Account

Create a BankAccount class with account holder and balance attributes and a method to display the balance.

21

Mini Challenge — Product Inventory System

Build a small inventory system using Python classes.

Your Task

Create a Product class that can represent products in an online store.

Requirements

  • Create a Product class.
  • Create at least three product objects.
  • Each product should have a name.
  • Each product should have a price.
  • Each product should have a stock quantity.
  • Create a method called show_details().
  • Display the details of every product.

Starter Structure

Challenge Starter
class Product:

    def show_details(self):
        print("Name:", self.name)
        print("Price:", self.price)
        print("Stock:", self.stock)


product1 = Product()

product1.name = "Laptop"
product1.price = 85000
product1.stock = 10

product1.show_details()

Once this works, create product2 and product3 and give them different values.

Extension Challenge

Add an update_stock() method that changes the stock quantity.

22

Quick Knowledge Check

Test your understanding before moving to the next lesson.

1. What is a class?

2. What is an object?

3. Which keyword defines a class?

4. What does self refer to?

5. What is a method?

6. Which creates an object from Student?

7. Which attribute belongs specifically to one object?

8. Which statement is true?

9. What is a class attribute?

10. Which naming style is commonly used for classes?

23

Lesson Summary

Python classes are the foundation of object-oriented programming.

Key Takeaways

  • A class is a blueprint for objects.
  • An object is an instance of a class.
  • Attributes store object data.
  • Methods define object behavior.
  • self refers to the current object.
  • One class can create many objects.
  • Instance attributes belong to individual objects.
  • Class attributes can be shared by objects.
  • Classes help organize large programs.

What's next?

In the next lesson, we will learn about Python constructors and the __init__() method, which provides a much cleaner way to initialize object attributes.

Ready for the next step?

Complete this lesson once you are comfortable creating classes, objects, attributes, and methods.