Skip to content
New: in-browser code execution engineSee it now

Advanced Python · Week 5

Objects/Classes

Step 1 of 5

Exercise 1: Introduction to Objects and Classes

Welcome to Python 2, Lesson 5! Today, we'll learn about objects and classes, fundamental concepts in object-oriented programming (OOP).

What are Objects and Classes?

A class is like a blueprint for creating objects. An object is an instance of a class, which contains its own data and behaviors.

Let's start with a simple example of a class:

class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def bark(self):
        return f"{self.name} says Woof!"

# Create a Dog object
my_dog = Dog("Buddy", 3)
print(f"{my_dog.name} is {my_dog.age} years old.")
print(my_dog.bark())

Now it's your turn! Create a class called Cat with the following:

  1. An __init__ method that takes name and color as parameters
  2. A method called meow that returns a string like "name says Meow!"
  3. Create a Cat object and call its meow method
Starter code
# Define your Cat class here

# Create a Cat object and call its meow method

Write your code here

python