Class: A class in Python is a blueprint or template for creating objects. It defines a set of attributes (variables) and methods (functions) that can operate on these attributes.
Objects: An object is an instance of a class.It is created based on the class template and can hold its own data and use the methods defined in the class.
Step-by-Step Process
A class is defined using the class keyword, followed by the class name. Inside the class, you define variables and methods to represent the data and behavior of the class.To create an object, you instantiate the class by calling it like a function
Access Attributes and Methods: Once an object is created, you can access and modify its attributes, and call its methods using the dot (.) notation.
Sample Code
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hello, my name is {self.name} and I am {self.age} years old."
person1 = Person("Antony", 30)
person2 = Person("Ram", 25)
person3 = Person("Kumar", 22)
print("Output for class and objects:")
print(person1.greet())
print(person2.greet())
print(person3.greet())