OOP Concept is the backbone of Python because everything in Python is an object ranging from classes to methods. It helps coders maintain program durability, clarity, and ease of debugging. Additionally, OOP prevents the need for writing long, repetitive code by allowing the reuse of the same class or object attributes throughout the program.
Example:
A Car has properties (or attributes or variables) like color, brand name, and speed.
It also has actions (or methods) like stop, start, and accelerate.
It also has actions (or methods) like stop, start, and accelerate.
What is Class, Variable, Method & Objects?
Class: A class defines variables and methods that tell how the object will behave. A class is also known as a blueprint or template for an object. We can reuse its functionality without rewriting the same code.
Syntax:
class Student:
pass
Here:
Student is the class name
pass means empty class (no data yet)
Example with Data
class Student:
Name = "John"
Age = 28
This class has two properties: Name and Age.
Variable: This is also know as properties and it defines the stae of object under the class. To initialize (or access) variable, we use "self" keyword and set the value with _init_ method constructor. Let's break in part:
Properties (Attributes): These are the variables located within a class that represent the state of an object.
self: We use "self" to initialize (or access) the attributes of an object.
__init__ method: This is a special method known as a constructor. Its function is to set the attributes automatically when an object is created.
Example
class Student:
def __init__(self, name, age): #self refer the object. name and age are input.
self.name = name
self.age = age
s1 = Student("Ravi", 22)
print(s1.name)Method: Methods are use to define or modify object behaviour (or action) in class.
Object: We have already stated that all things are objects; this does not imply that we intend to rename them or assign them a new name. An object is created within a class with the aid of variables and methods. These are utilized to execute the class or to obtain the desired output.
Example
s1 = Student() #s1 is an object and it use the Student class.
print(s1.name)Example with all above topics
#define a class
class Book:
#Use constructor (_init_) to initialize the attribute of the oject.
def __init__(self, title, author, year):
self.title = title #title is an attribute that stores book's title
self.author = author #author is an attribute that stores book's author
self.year = year #year is an attribute that stores book's year
#method to display the book's details
def display_info(self):
print(f"Book Title:{self.title}") #f is used to insert variables or values or input values into a string.
print(f"Author:{self.author}")
print(f"Year Publish:{self.year}")
#method to check if the book is an old book
def is_old_book(self):
if self.year > 1940:
return True
return False
#Create objects (instances) of the "Book" class
book1 = Book("Untouchable","Mulk Raj", 1935) #book1 is an object of the Book class
book2 = Book("Maulgudi Days","R.K Narayan", 1943)#book2 is an object of the Book class
#Call the "display_info" method to show the details of book1
book1.display_info()
#Call the "is_old_book" method to check if book1 is old
if book1.is_old_book():
print(f"{book1.title} is an old book.")
else:
print(f"{book1.title} is a latest book.")
print()
#Call the "display_info" method to show the details of book2
book2.display_info()
#Call the is_old_book" method to check if book2 is old
if book2.is_old_book():
print(f"{book2.title} is an old book.")
else:
print(f"{book2.title} is a latest book.")
Note: In Python, inside a class you should only define
- __inti__ Constructor to initialize attributes.
- Methods functions that belong to the class.
Anything else, like creating objects or calling methods, must be outside the class, at the main program level.
Output of above example
Task 1: Create City Class, initialize Population and Name then use ojbect to acccess City with population.
Conclusion
OOPs concepts are a very important chapter as they help you make programs more structured and easier to understand. The topics covered above include methods, variables, classes, and objects. In the next chapter, we will go further and explore the four principles of Python OOP: Encapsulation, Inheritance, Polymorphism, and Abstraction. Understanding these principles will take your coding skills a step further, enabling you to write more secure, maintainable, and robust programs.
