Create Class & Object

Define a simple class with attributes and methods, then create and use an object of that class.

PythonBeginner
Python
# Program to create a simple class and object

class Student:
    def __init__(self, name, roll_no):
        self.name = name
        self.roll_no = roll_no

    def display_info(self):
        print(f"Name: {self.name}, Roll No: {self.roll_no}")


student1 = Student("Alice", 101)
student1.display_info()

Output

Name: Alice, Roll No: 101

Shows how to:

  • Define a class with init (constructor)
  • Store data in instance attributes
  • Call a method using an object reference