Next

Object-Oriented Programming (OOP)

  • Learn the fundamentals of OOP in Python including classes, objects, attributes, and methods.

  • What is a Class?

    A class is a blueprint used to create objects.
    It defines attributes (data) and methods (functions) that an object can use.

    Class Syntax

    class ClassName:

        # attributes

        # methods

Creating a Basic Class

This example shows how to create a class with a method and call it using an object.

class Student:
    def display(self):
        print("This is a Student class")

obj = Student()
obj.display()
  • Attributes & Methods

    Attributes

    • Variables that store data inside a class

    • Defined using self

    Methods

    • Functions defined inside a class

    • Used to perform actions

Class with Attributes and Methods

This example demonstrates how attributes store data and methods display that data.

class Student:
    name = "Rahul"
    age = 20

    def show(self):
        print("Name:", self.name)
        print("Age:", self.age)

obj = Student()
obj.show()
Next