Classes in Python
Classes are used to define new types of objects in Python. Here's an example:
python
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print("Hello, my name is " + self.name + " and I am " + str(self.age) + " years old.")
person = Person("Alice", 30)
person.greet()
In this example, we define a class called Person that has two attributes, name and age, and a method called greet that prints a greeting message. We create a new Person object with the name "Alice" and the age 30, and call the greet method on the object.
No comments:
Post a Comment