Attributes and Methods
Understand how attributes and methods define the behavior of objects.
Attributes and Methods is a free Learn AI with Python lesson on CoddyKit — lesson 2 of 6. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 6 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.

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)3
4
Instance Attributes
Instance attributes are specific to an object and are defined using the self keyword:
# Instance attributes in a class
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
car1 = Car("Toyota", "Corolla")
print(car1.brand, car1.model)5
What Are Methods?
Methods are functions defined inside a class. They define the actions that objects can perform:
# Defining a method in a class
class Person:
def greet(self):
print("Hello!")
person1 = Person()
person1.greet()6
Instance Methods
Instance methods operate on instance attributes and require the self parameter:
# Instance methods in a class
class Person:
def __init__(self, name):
self.name = name
def greet(self):
print(f"Hello, {self.name}!")
person1 = Person("Alice")
person1.greet()7
Using Parameters in Methods
Methods can accept parameters to customize their behavior:
# Methods with parameters
class Calculator:
def add(self, a, b):
return a + b
calc = Calculator()
print(calc.add(3, 5))8
Updating Attributes
You can update attributes using methods:
# Updating attributes using a method
class Person:
def __init__(self, name):
self.name = name
def update_name(self, new_name):
self.name = new_name
person = Person("Alice")
person.update_name("Bob")
print(person.name)9
10
Common Mistakes with Attributes and Methods
Here are some mistakes to avoid:
- Forgetting to use
selfin method definitions. - Accessing undefined attributes.
- Not initializing attributes properly in the constructor.
11
What Did We Learn?
In this lesson, you learned:
- What attributes and methods are in Python classes.
- How to define and use attributes and methods.
- How to customize methods with parameters and update attributes.
Great job! Let’s move to the next topic.

Frequently asked questions
Is the “Attributes and Methods” lesson free?
Yes — the full text of “Attributes and Methods” is free to read here on the web, and the Learn AI with Python course includes 6 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Attributes and Methods”?
Understand how attributes and methods define the behavior of objects. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 6, so you can start here or from the beginning and move at your own pace.
How long does the “Attributes and Methods” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.