New: Complete Beginner's Guide to Coding is now available in Premium
Updated: Indian Govt Exam roadmaps now include salary breakdowns & timelines
Tip: Use the Career Hub to explore all career paths in one place
Tutorials Python Python OOP (Classes & Objects)

Python OOP (Classes & Objects)

Python

Object-Oriented Programming lets you create your own data types using classes. Think of a class as a blueprint.

Key Points

  • class — defines a blueprint
  • __init__ — constructor, runs when object is created
  • self — refers to the current object
  • Objects are instances of a class
  • Methods are functions inside a class
Example — PYTHON
# Simple class
class Student:
    def __init__(self, name, marks):
        self.name = name
        self.marks = marks
    def grade(self):
        if self.marks >= 90:
            return "A+"
        elif self.marks >= 75:
            return "A"
        elif self.marks >= 60:
            return "B"
        return "C"
    def display(self):
        print(f"{self.name} — {self.marks} marks — Grade: {self.grade()}")
# Creating objects
s1 = Student("Rahul", 85)
s2 = Student("Priya", 92)
s1.display()
s2.display()
# Inheritance
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def greet(self):
        print(f"Hi, I am {self.name}")
class Employee(Person):
    def __init__(self, name, age, company):
        super().__init__(name, age)
        self.company = company
    def info(self):
        print(f"{self.name}, Age {self.age}, works at {self.company}")
emp = Employee("Amit", 25, "TCS")
emp.greet()
emp.info()
Result
Rahul — 85 marks — Grade: A
Priya — 92 marks — Grade: A+
Hi, I am Amit
Amit, Age 25, works at TCS