Classes and Objects in Python


Explore object-oriented programming in Python by defining classes and creating objects.

Source 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."

alice = Person("Alice", 30)
print(alice.greet())
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments