Create Class & Object
Define a simple class with attributes and methods, then create and use an object of that class.
BeginnerTopic: Object-Oriented Programs
Python Create Class & Object Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# 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
Understanding Create Class & Object
Shows how to:
•Define a class with __init__ (constructor)
•Store data in instance attributes
•Call a method using an object reference
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.