Object-Oriented Programming: Interactive Python Lessons

Learn Python OOP through practice: classes, objects, methods, inheritance, and encapsulation, built up step by step with interactive coding challenges.

73 lessons in this topic. Every lesson runs real Python in your browser with instant feedback.

advanced

Classes - Basic

Create your first class

advanced

Class Methods

Add methods to classes

advanced

BankAccount balance after deposits

Build a BankAccount (start 0) with a deposit method, deposit each of [100, 50, 25], and print the balance.

advanced

Rectangle area via a class (3×4)

Write a Rectangle class with an area() method. Print the area of a 3×4 rectangle.

advanced

Add two vectors with __add__

Give a Vector class an __add__ method. Print (3,4) + (1,2) as a tuple-like result.

advanced

Inheritance and polymorphism

Animal has speak(). Make Dog say 'Woof' and Cat say 'Meow'. Loop and print each.

advanced

Counter class bumped 5 times

Write a Counter class with bump(). Bump it 5 times and print its value.

advanced

Custom __len__ on a Bag

Give a Bag class __len__. Fill it with [100, 50, 25] and print len(bag).

advanced

Stack push/pop

Write a Stack with push and pop. Push each of [100, 50, 25], pop once, and print the value popped.

advanced

BankAccount balance after deposits ([10, 20, 30, 40])

Build a BankAccount (start 0) with a deposit method, deposit each of [10, 20, 30, 40], and print the balance.

advanced

Rectangle area via a class (5×6)

Write a Rectangle class with an area() method. Print the area of a 5×6 rectangle.

advanced

Add two vectors with __add__ ([10, 20, 30, 40])

Give a Vector class an __add__ method. Print (5,6) + (7,8) as a tuple-like result.

advanced

Counter class bumped 3 times

Write a Counter class with bump(). Bump it 3 times and print its value.

advanced

Custom __len__ on a Bag ([10, 20, 30, 40])

Give a Bag class __len__. Fill it with [10, 20, 30, 40] and print len(bag).

advanced

Stack push/pop ([10, 20, 30, 40])

Write a Stack with push and pop. Push each of [10, 20, 30, 40], pop once, and print the value popped.

advanced

BankAccount balance after deposits ([7, 7, 7])

Build a BankAccount (start 0) with a deposit method, deposit each of [7, 7, 7], and print the balance.

advanced

Rectangle area via a class (2×9)

Write a Rectangle class with an area() method. Print the area of a 2×9 rectangle.

advanced

Add two vectors with __add__ ([7, 7, 7])

Give a Vector class an __add__ method. Print (2,9) + (4,1) as a tuple-like result.

advanced

Counter class bumped 8 times

Write a Counter class with bump(). Bump it 8 times and print its value.

advanced

Custom __len__ on a Bag ([7, 7, 7])

Give a Bag class __len__. Fill it with [7, 7, 7] and print len(bag).

advanced

Stack push/pop ([7, 7, 7])

Write a Stack with push and pop. Push each of [7, 7, 7], pop once, and print the value popped.

advanced

BankAccount balance after deposits ([12, 8])

Build a BankAccount (start 0) with a deposit method, deposit each of [12, 8], and print the balance.

advanced

Rectangle area via a class (6×3)

Write a Rectangle class with an area() method. Print the area of a 6×3 rectangle.

advanced

Add two vectors with __add__ ([12, 8])

Give a Vector class an __add__ method. Print (6,3) + (2,5) as a tuple-like result.

advanced

Counter class bumped 4 times

Write a Counter class with bump(). Bump it 4 times and print its value.

advanced

Custom __len__ on a Bag ([12, 8])

Give a Bag class __len__. Fill it with [12, 8] and print len(bag).

advanced

Stack push/pop ([12, 8])

Write a Stack with push and pop. Push each of [12, 8], pop once, and print the value popped.

advanced

BankAccount balance after deposits ([5, 5, 5, 5])

Build a BankAccount (start 0) with a deposit method, deposit each of [5, 5, 5, 5], and print the balance.

advanced

Rectangle area via a class (8×2)

Write a Rectangle class with an area() method. Print the area of a 8×2 rectangle.

advanced

Add two vectors with __add__ ([5, 5, 5, 5])

Give a Vector class an __add__ method. Print (8,2) + (1,9) as a tuple-like result.

advanced

Counter class bumped 6 times

Write a Counter class with bump(). Bump it 6 times and print its value.

advanced

Custom __len__ on a Bag ([5, 5, 5, 5])

Give a Bag class __len__. Fill it with [5, 5, 5, 5] and print len(bag).

advanced

Stack push/pop ([5, 5, 5, 5])

Write a Stack with push and pop. Push each of [5, 5, 5, 5], pop once, and print the value popped.

advanced

BankAccount balance after deposits ([101, 51, 26])

Build a BankAccount (start 0) with a deposit method, deposit each of [101, 51, 26], and print the balance.

advanced

Rectangle area via a class (4×5)

Write a Rectangle class with an area() method. Print the area of a 4×5 rectangle.

advanced

Add two vectors with __add__ ([101, 51, 26])

Give a Vector class an __add__ method. Print (4,5) + (2,3) as a tuple-like result.

advanced

Custom __len__ on a Bag ([101, 51, 26])

Give a Bag class __len__. Fill it with [101, 51, 26] and print len(bag).

advanced

Stack push/pop ([101, 51, 26])

Write a Stack with push and pop. Push each of [101, 51, 26], pop once, and print the value popped.

advanced

BankAccount balance after deposits ([11, 21, 31, 41])

Build a BankAccount (start 0) with a deposit method, deposit each of [11, 21, 31, 41], and print the balance.

advanced

Rectangle area via a class (6×7)

Write a Rectangle class with an area() method. Print the area of a 6×7 rectangle.

advanced

Add two vectors with __add__ ([11, 21, 31, 41])

Give a Vector class an __add__ method. Print (6,7) + (8,9) as a tuple-like result.

advanced

Custom __len__ on a Bag ([11, 21, 31, 41])

Give a Bag class __len__. Fill it with [11, 21, 31, 41] and print len(bag).

advanced

Stack push/pop ([11, 21, 31, 41])

Write a Stack with push and pop. Push each of [11, 21, 31, 41], pop once, and print the value popped.

advanced

BankAccount balance after deposits ([8, 8, 8])

Build a BankAccount (start 0) with a deposit method, deposit each of [8, 8, 8], and print the balance.

advanced

Rectangle area via a class (3×10)

Write a Rectangle class with an area() method. Print the area of a 3×10 rectangle.

advanced

Add two vectors with __add__ ([8, 8, 8])

Give a Vector class an __add__ method. Print (3,10) + (5,2) as a tuple-like result.

advanced

Counter class bumped 9 times

Write a Counter class with bump(). Bump it 9 times and print its value.

advanced

Custom __len__ on a Bag ([8, 8, 8])

Give a Bag class __len__. Fill it with [8, 8, 8] and print len(bag).

advanced

Stack push/pop ([8, 8, 8])

Write a Stack with push and pop. Push each of [8, 8, 8], pop once, and print the value popped.

advanced

BankAccount balance after deposits ([13, 9])

Build a BankAccount (start 0) with a deposit method, deposit each of [13, 9], and print the balance.

advanced

Rectangle area via a class (7×4)

Write a Rectangle class with an area() method. Print the area of a 7×4 rectangle.

advanced

Add two vectors with __add__ ([13, 9])

Give a Vector class an __add__ method. Print (7,4) + (3,6) as a tuple-like result.

advanced

Custom __len__ on a Bag ([13, 9])

Give a Bag class __len__. Fill it with [13, 9] and print len(bag).

advanced

Stack push/pop ([13, 9])

Write a Stack with push and pop. Push each of [13, 9], pop once, and print the value popped.

advanced

BankAccount balance after deposits ([6, 6, 6, 6])

Build a BankAccount (start 0) with a deposit method, deposit each of [6, 6, 6, 6], and print the balance.

advanced

Rectangle area via a class (9×3)

Write a Rectangle class with an area() method. Print the area of a 9×3 rectangle.

advanced

Add two vectors with __add__ ([6, 6, 6, 6])

Give a Vector class an __add__ method. Print (9,3) + (2,10) as a tuple-like result.

advanced

Counter class bumped 7 times

Write a Counter class with bump(). Bump it 7 times and print its value.

advanced

Custom __len__ on a Bag ([6, 6, 6, 6])

Give a Bag class __len__. Fill it with [6, 6, 6, 6] and print len(bag).

advanced

Stack push/pop ([6, 6, 6, 6])

Write a Stack with push and pop. Push each of [6, 6, 6, 6], pop once, and print the value popped.

advanced

BankAccount balance after deposits ([102, 52, 27])

Build a BankAccount (start 0) with a deposit method, deposit each of [102, 52, 27], and print the balance.

advanced

Add two vectors with __add__ ([102, 52, 27])

Give a Vector class an __add__ method. Print (5,6) + (3,4) as a tuple-like result.

advanced

Custom __len__ on a Bag ([102, 52, 27])

Give a Bag class __len__. Fill it with [102, 52, 27] and print len(bag).

advanced

Stack push/pop ([102, 52, 27])

Write a Stack with push and pop. Push each of [102, 52, 27], pop once, and print the value popped.

advanced

BankAccount balance after deposits ([12, 22, 32, 42])

Build a BankAccount (start 0) with a deposit method, deposit each of [12, 22, 32, 42], and print the balance.

advanced

Rectangle area via a class (7×8)

Write a Rectangle class with an area() method. Print the area of a 7×8 rectangle.

advanced

Add two vectors with __add__ ([12, 22, 32, 42])

Give a Vector class an __add__ method. Print (7,8) + (9,10) as a tuple-like result.

advanced

Custom __len__ on a Bag ([12, 22, 32, 42])

Give a Bag class __len__. Fill it with [12, 22, 32, 42] and print len(bag).

advanced

Stack push/pop ([12, 22, 32, 42])

Write a Stack with push and pop. Push each of [12, 22, 32, 42], pop once, and print the value popped.

advanced

BankAccount balance after deposits ([9, 9, 9])

Build a BankAccount (start 0) with a deposit method, deposit each of [9, 9, 9], and print the balance.

advanced

Rectangle area via a class (4×11)

Write a Rectangle class with an area() method. Print the area of a 4×11 rectangle.

advanced

Add two vectors with __add__ ([9, 9, 9])

Give a Vector class an __add__ method. Print (4,11) + (6,3) as a tuple-like result.

advanced

Counter class bumped 10 times

Write a Counter class with bump(). Bump it 10 times and print its value.