0PricingLogin
Learn AI with Python · Lesson

Attributes and Methods

Understand how attributes and methods define the behavior of objects.

1

Introduction to Attributes and Methods

Attributes and methods define the characteristics and behavior of objects in Object-Oriented Programming (OOP). Attributes store data, while methods perform actions.

In this lesson, you’ll learn how to define and use attributes and methods effectively in Python classes.

Attributes and Methods — illustration 1

2

What Are Attributes?

Attributes are variables that belong to a class or an object. They are defined inside the class and describe the properties of an object:

# Defining attributes in a class
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

person1 = Person("Alice", 30)
print(person1.name, person1.age)

All lessons in this course

  1. Classes and Objects
  2. Attributes and Methods
  3. Inheritance
  4. Polymorphism
  5. Encapsulation and Abstraction
  6. Magic Methods (__str__, __repr__, etc.)
← Back to Learn AI with Python