Lesson 31 of 34

Classes & Objects: Your Own Data Types

10 min 35 XP

A class is a blueprint. Each thing made from it is an object. Example: Student is the class; Arun and Priya are objects.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

__init__ runs automatically when an object is created. self means “this object”. self.name = name stores the name inside the object.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

By convention, class names start with a capital letter: Student, BankAccount.

Challenge

+35 XP

Write a class Book whose __init__ stores title and price. Create Book("Python Basics", 350) and print its title and price on one line.

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.