Creating Classes and Objects
Build your first Python class.
Creating Classes and Objects is a free Python For Kids lesson on CoddyKit — lesson 2 of 5. 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 Python For Kids learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Creating Classes and Objects
Welcome back to the Object-Oriented Programming (OOP) lesson! Today, we'll learn how to create your own classes and objects in Python. Classes and objects help you organize your code by bundling related properties and behaviors together.

2
What is a Class?
A class is like a blueprint for creating objects. It defines the properties (attributes) and behaviors (methods) that the objects created from the class will have.
- Attributes: Characteristics of the object (e.g., color, size).
- Methods: Actions the object can perform (e.g., move, speak).
Think of a class as a recipe and objects as the dishes made from that recipe.
3
Defining a Class in Python
You can define a class in Python using the class keyword, followed by the class name and a colon. Inside the class, you define attributes and methods.
Syntax:
class ClassName:
def __init__(self, attribute1, attribute2):
self.attribute1 = attribute1
self.attribute2 = attribute2
def method_name(self):
# Code for the method
pass
4
The __init__ Method
The __init__ method is a special method in Python classes. It's called when you create a new object from the class and is used to initialize the object's attributes.
Example:
# Defines the Person class with name and age attributes
class Person:
def __init__(self, name, age):
self.name = name # Sets the name of the person
self.age = age # Sets the age of the person
5
Creating Objects from a Class
Once you've defined a class, you can create objects (instances) from it by calling the class name like a function.
Example:
# Creates an object of the Person class
person1 = Person("Alice", 12)
# Accesses the object's attributes
print(person1.name) # Output: Alice
print(person1.age) # Output: 12
6
Adding Methods to a Class
Methods are functions defined inside a class that describe the behaviors of the objects created from the class.
Example:
# Defines the Person class with a greet method
class Person:
def __init__(self, name, age):
self.name = name # Sets the name of the person
self.age = age # Sets the age of the person
def greet(self):
print("Hello, my name is " + self.name + "!") # Prints a greeting message
# Creates an object of the Person class
person1 = Person("Bob", 15)
# Calls the greet method of the object
person1.greet() # Output: Hello, my name is Bob!
7
Example Program: Building a Simple Class
Let's create a simple Car class with attributes and methods to understand how classes and objects work.
Code:
# Defines the Car class
class Car:
def __init__(self, make, model, color):
self.make = make # Sets the make of the car
self.model = model # Sets the model of the car
self.color = color # Sets the color of the car
def display_info(self):
print("Car Make:", self.make) # Prints the make of the car
print("Car Model:", self.model) # Prints the model of the car
print("Car Color:", self.color) # Prints the color of the car
def paint(self, new_color):
self.color = new_color # Changes the color of the car
print("The car has been painted to", self.color) # Confirms the color change
# Creates an object of the Car class
my_car = Car("Toyota", "Corolla", "Red")
# Calls the display_info method
my_car.display_info()
# Output:
# Car Make: Toyota
# Car Model: Corolla
# Car Color: Red
# Calls the paint method to change the car's color
my_car.paint("Blue")
# Output: The car has been painted to Blue
# Displays the updated car information
my_car.display_info()
# Output:
# Car Make: Toyota
# Car Model: Corolla
# Car Color: Blue
8
Instance vs. Class Variables
In Python classes, variables can be categorized as:
- Instance Variables: Unique to each object instance.
- Class Variables: Shared across all instances of the class.
Let's see how to use both types of variables.
9
# Defines the Student class with a class variable
class Student:
school = "XYZ High School" # Class variable
def __init__(self, name):
self.name = name # Instance variable
def display_info(self):
print(self.name + " attends " + Student.school) # Prints student info
# Creates two objects of the Student class
student1 = Student("Alice")
student2 = Student("Bob")
# Calls the display_info method for both students
student1.display_info()
student2.display_info()
11
Great Job!
You've learned how to create classes and objects in Python, defining attributes and methods to organize your code effectively. Understanding classes and objects is a fundamental part of Object-Oriented Programming (OOP), helping you build more complex and reusable programs. Keep practicing by creating your own classes and experimenting with different attributes and methods!

Frequently asked questions
Is the “Creating Classes and Objects” lesson free?
Yes — the full text of “Creating Classes and Objects” is free to read here on the web, and the Python For Kids course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python For Kids course, upgrade to CoddyKit PRO.
What will I learn in “Creating Classes and Objects”?
Build your first Python class. You practise Python For Kids 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 Python For Kids?
No prior experience is required. Python For Kids on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 5, so you can start here or from the beginning and move at your own pace.
How long does the “Creating Classes and Objects” 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 Python For Kids lesson?
Yes. Every Python For Kids 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.
All lessons in this course
- Introduction to OOP
- Creating Classes and Objects
- Inheritance
- Encapsulation
- Polymorphism