0% found this document useful (0 votes)
7 views5 pages

Python Classes and Objects Explained

This document provides an overview of Python's object-oriented programming concepts, focusing on classes and objects. It explains the structure of a class, the creation of objects, and includes examples such as a Student class and a BankAccount class. The document emphasizes the benefits of using classes and objects, including encapsulation, reusability, and modularity.

Uploaded by

fixshit
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Python Classes and Objects Explained

This document provides an overview of Python's object-oriented programming concepts, focusing on classes and objects. It explains the structure of a class, the creation of objects, and includes examples such as a Student class and a BankAccount class. The document emphasizes the benefits of using classes and objects, including encapsulation, reusability, and modularity.

Uploaded by

fixshit
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Classes and Objects with Example

November 9, 2025

1. Introduction
Python is an object-oriented programming (OOP) language, meaning it supports the
concepts of classes and objects that allow code to be organized, reusable, and modular.
In OOP, a class defines the blueprint or structure of data and the operations that can
be performed on it, while an object is an instance of that class containing actual data
values.
Object-oriented programming helps developers model real-world entities (like stu-
dents, employees, or vehicles) in code and provides a foundation for encapsulation, inher-
itance, and polymorphism.

2. What is a Class?
A class in Python is a user-defined data type that serves as a blueprint for creating ob-
jects. It encapsulates data (attributes) and functions (methods) that define the behavior
of the object.
The basic syntax for defining a class is:
class ClassName :
# class attribute
class_variable = value

# constructor ( initializer )
def __init__ ( self , parameters ) :
self . attribute = value

# instance method
def method_name ( self ) :
# perform operations
pass

Key components of a class:

1
• Attributes: Variables that hold data related to an object.

• Methods: Functions defined inside a class that perform operations on data.

• Constructor ( init ): A special method that initializes object attributes when


the object is created.

• Self: Refers to the current instance of the class and is used to access its variables
and methods.

3. What is an Object?
An object is an instance of a class. It contains real data and can use the methods defined
in the class. Multiple objects can be created from the same class, each maintaining its
own data.
Creating an object in Python involves calling the class as if it were a function:
object_name = ClassName ( arguments )

4. Example: Class and Object in Python


The following example demonstrates how to define a class and create objects from it.
# Define a class
class Student :
# Class attribute
school_name = " Greenwood High School "

# Constructor
def __init__ ( self , name , age , grade ) :
self . name = name
self . age = age
self . grade = grade

# Method to display student details


def display_info ( self ) :
print ( f " Name : { self . name } " )
print ( f " Age : { self . age } " )
print ( f " Grade : { self . grade } " )
print ( f " School : { Student . school_name } " )

# Create objects of the Student class


student1 = Student ( " Alice " , 15 , " 10 th Grade " )
student2 = Student ( " Bob " , 16 , " 11 th Grade " )

2
# Access methods and attributes
student1 . display_info ()
student2 . display_info ()

Output:
Name: Alice
Age: 15
Grade: 10th Grade
School: Greenwood High School

Name: Bob
Age: 16
Grade: 11th Grade
School: Greenwood High School

5. Explanation of the Example


• The class Student defines the structure for creating student objects.

• init () initializes the attributes name, age, and grade whenever a new object
is created.

• The method display info() prints details about the student.

• student1 and student2 are objects that store individual student data.

• The class variable school name is shared by all instances of the class.

6. Accessing Attributes and Methods


Attributes and methods can be accessed in the following ways:

• Instance Attributes: Accessed using [Link].

• Class Attributes: Accessed using either [Link] or [Link].

• Methods: Accessed using [Link]() syntax.

Example:
print ( student1 . name ) # Access instance attribute
print ( Student . school_name ) # Access class attribute
student1 . display_info () # Call instance method

3
7. Modifying Attributes
Attributes can be modified either directly or through class methods.
# Modify instance attribute
student1 . grade = " 11 th Grade "

# Modify class attribute


Student . school_name = " Bluebell Academy "

All objects of the class reflect the change in class-level attributes.

8. Benefits of Using Classes and Objects


• Encapsulation: Combines data and methods into a single unit.

• Reusability: Classes can be reused to create multiple objects.

• Abstraction: Hides implementation details, exposing only relevant information.

• Modularity: Organizes code logically into separate components.

9. Example: A Simple Bank Account Class


class BankAccount :
def __init__ ( self , owner , balance =0) :
self . owner = owner
self . balance = balance

def deposit ( self , amount ) :


self . balance += amount
print ( f " Deposited $ { amount }. New balance : $ { self . balance } " )

def withdraw ( self , amount ) :


if amount <= self . balance :
self . balance -= amount
print ( f " Withdrew $ { amount }. Remaining balance : $ { self .
balance } " )
else :
print ( " Insufficient funds ! " )

# Creating objects
acc1 = BankAccount ( " John " )
acc2 = BankAccount ( " Emma " , 500)

# Performing transactions

4
acc1 . deposit (200)
acc2 . withdraw (150)

10. Conclusion
Classes and objects form the foundation of object-oriented programming in Python. A
class acts as a blueprint defining attributes and methods, while objects are individual
instances containing specific data. Through classes, developers can encapsulate function-
ality, reuse code efficiently, and model real-world systems effectively. Mastering classes
and objects is essential for building scalable, modular, and maintainable Python applica-
tions.

You might also like