Computer Science Grade 9 20 min

Introduction to Classes and Objects: Defining Blueprints

Understand the basic concepts of classes and objects, and how to create them.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define the terms 'class' and 'object' and explain their relationship. Identify the difference between a class (blueprint) and an object (instance). Read and interpret the basic syntax for defining a simple class in Python. Create a class with specific attributes using an initializer method (`__init__`). Instantiate multiple unique objects from a single class. Access the attributes of an object using dot notation. Have you ever designed a character in a video game? 🎮 You pick the hair, eyes, and skills from a template, but your character is unique. That's exactly how classes and objects work! In this lesson, you'll learn how to create 'blueprints' called classes for things you want to represent in your code. Then,...
2

Key Concepts & Vocabulary

TermDefinitionExample ClassA blueprint or template for creating objects. It defines a set of attributes (data) and methods (behaviors) that the objects created from it will have.A `Dog` class could define that all dogs have a `name`, `breed`, and `age`. ObjectA specific instance created from a class. It has its own unique values for the attributes defined in the class.If `Dog` is the class, then `my_dog = Dog('Fido', 'Golden Retriever', 5)` creates an object named `my_dog` with specific data. InstanceAnother word for an object. When you create an object from a class, you are 'instantiating' the class.`fido` is an instance of the `Dog` class. `buddy` is another, separate instance of the `Dog` class. AttributeA piece of data or a variable that belongs to a clas...
3

Core Syntax & Patterns

Class Definition Syntax class ClassName: # class content goes here Use the `class` keyword followed by a name in PascalCase (e.g., MyClassName) and a colon. All code for the class must be indented underneath. Initializer (`__init__`) Method Syntax def __init__(self, param1, param2): self.attribute1 = param1 self.attribute2 = param2 Inside a class, define the `__init__` method to set up an object's starting attributes. The first parameter must always be `self`. Object Instantiation Syntax variable_name = ClassName(arg1, arg2) To create an object (an instance) from a class, you call the class name as if it were a function, passing the arguments required by the `__init__` method (except for `self`).

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
A `Robot` class needs to be defined. Each robot has a `name` given at creation and a `mode` which always starts as 'idle'. Which option correctly defines the class, creates an instance named 'R2', and prints its starting mode?
A.class Robot: def __init__(self, name, mode): self.name = name self.mode = 'idle' r = Robot('R2', 'idle') print(r.mode)
B.class Robot: name = 'R2' mode = 'idle' r = Robot() print(r.mode)
C.class Robot: def __init__(self, name): self.name = name mode = 'idle' r = Robot('R2') print(r.mode)
D.class Robot: def __init__(self, name): self.name = name self.mode = 'idle' r = Robot('R2') print(r.mode)
Challenging
Consider the `Player` class from the tutorial. If you create two players, `p1 = Player('Kim')` and `p2 = Player('Jo')`, and then execute `p1.health = 50`, what will `print(p2.health)` output?
A.50
B.100
C.An error, because health is constant.
D.None
Challenging
If a programmer misspells the initializer as `def _init_(self, name):` (one underscore) and then tries to create an object using `p = Player('Alex')`, what specific error will Python most likely raise?
A.TypeError: Player() takes no arguments
B.NameError: name '__init__' is not defined
C.SyntaxError: invalid syntax
D.AttributeError: 'Player' object has no attribute 'name'

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Object-Oriented Programming: Designing with Classes and Objects

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.