Lecture notes · Programming in Python · Lecture 17

Python Classes

MIT 6.100L Intro to CS and Programming using Python, lecture 17, explained in plain English

Want this lecture to stick? In LessonBound you get checked practice questions for every part, can ask about anything you don't get, and can listen with your screen off. Free, no ads.

Learn this lecture free

Watch or read the original lecture: MIT OpenCourseWare.

Part 1 · 0:00–10:00

This part introduces Python classes, the way programmers design their own kinds of objects by deciding what data each object holds and what it can do.

Key ideas

Words to know

instance
One specific object that was made from a type or blueprint, like one particular car rather than the idea of cars in general.
blueprint
A plan that says how to build a certain kind of object. The plan itself is not the object, just the instructions for making it.
interface
The list of things you can do with an object, or ways other code can work with it. It is like the buttons and controls on a device.
data abstraction
Deciding which pieces of information represent an object, and hiding the messy details of how that information is stored.

Example

Think of two real cars that were both built from the same car blueprint. The blueprint is not a car. It is the plan that says what each car should have, like length, width, height, and color. Each real car is an instance of that blueprint.

3 checked practice questions for this part are in the app.

Part 2 · 10:00–20:00

This part explains how Python classes bundle data and behaviors into one object, using queues, stacks, and a 2D coordinate as examples.

Key ideas

Words to know

class
A blueprint that tells Python what data an object holds and what it can do.
instance
One real object made from a class, such as one particular queue or one particular point.
Euclidean distance
The straight-line distance between two points on a flat plane.
first in, first out
A rule where the item that went in first is the first one taken out, like a line at a store.

Example

Think of a line at a store. People join at the back and leave from the front, so the first person in line is the first one served. A queue class works the same way: it stores the names in a list, and its behavior makes sure the oldest name is always removed first.

3 checked practice questions for this part are in the app.

Part 3 · 20:00–30:00

This part shows how to start building your own Python data type, called a class, with a setup function and a special word called self.

Key ideas

Words to know

method
A function that only works on objects of one particular type, such as a distance function that only works on coordinates.
self
A placeholder name that refers to the object being built or used. It is always the first input in a method.
instance
One real object made from a class blueprint, like one specific room built from a room plan.
__init__
The special setup function that runs when a new object is created. Its name has double underscores on each side.

Example

Think of a room blueprint that says a room has two chairs, a coffee table, and a sofa. While the plan is still just an idea, you refer to its parts as self.coffeetable. Once you build a real living room from the plan, you refer to its table as livingroom.coffeetable.

3 checked practice questions for this part are in the app.

Part 4 · 30:00–40:00

This part explains how to turn a class blueprint into real objects, how those objects store their own data, and how to write a method that works on an object.

Key ideas

Words to know

instance
One real object built from a class blueprint. Each instance can hold its own values, like two different living rooms built from the same plan.
constructor
The special setup code, called the init method, that runs when you create a new object and sets up its starting values.
dot notation
Writing a name, then a dot, then another name, such as c.x, to get a value that belongs to an object.
method
A function that belongs to a class and works on an object of that class.

Example

Think of a house plan that is used to build many houses. Each house built from the plan is an instance, and each can have its own paint color and furniture. The plan itself is not a house, just like a class is not an object until you build one from it.

3 checked practice questions for this part are in the app.

Part 5 · 40:00–47:34

This part shows how a method works on a Coordinate object, using a distance method as the example, and how the dot notation calls it.

Key ideas

Words to know

method
A function that belongs to an object. You call it on that object using a dot.
dot notation
Writing the object, then a dot, then the name of a method or data item, such as c.distance.
self
The name used inside a class for the object the method is working on. Python fills it in for you when you call the method.
parameter
A value you pass into a function or method through the brackets, so it can use that value.

Example

Think of a coffee machine with a brew button. You press brew on one specific machine, and that machine is the one that makes the coffee, so you do not have to tell it which machine to use. The object before the dot works the same way: it is the thing that does the work.

3 checked practice questions for this part are in the app.

← Lecture 15: Recursion · Lecture 18: More Python Class Methods →

Notes written by AI (Claude) from the lecture's official transcript, so check anything important against the lecture. MIT OpenCourseWare · CC BY-NC-SA 4.0 · not affiliated with or endorsed by MIT. These notes are shared under the same license.

LessonBound · Free lecture notes · Privacy · Terms · New York