Lesson 25 of 55
PythonOOP Basics – Classes
20 minutes
Course progress45%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 25 of 55
Python20 minutes
Keep these ideas in mind as you work through the lesson.
Object‑oriented programming groups data (attributes) and behavior (methods) into classes. Instances of a class are called objects.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f'Hi, I\'m {self.name} and I\'m {self.age} years old.'
bob = Person('Bob', 28)
print(bob.greet())
The __init__ method is the constructor; it runs automatically when you create a new object.