From zero OOP to building a real abstract-base-class hierarchy — in one Jupyter notebook.
This repository contains a self-contained, pedagogically-structured Jupyter notebook that teaches Object-Oriented Programming (OOP) in Python from the ground up. It is designed for learners who already know basic Python (functions, lists, loops) but have never written a class before.
The tutorial covers every core OOP concept, uses consistent, beginner-friendly examples throughout, and culminates in a capstone project that ties every concept together: a mini library management system with abstract base classes, composition, properties, and operator overloading.
Time commitment: ~2 hours of active reading + running cells + doing exercises.
|
|
0. Motivation — why do we need classes?
│
▼
1. Classes & Instances ─────── your first class, self, __init__
│
▼
2. Class Variables ─────────── shared state, the mutable-default trap
│
▼
3. Method Types ────────────── instance / class / static
│
▼
4. Inheritance ─────────────── super(), overriding, polymorphism, MRO
│
▼
5. Dunder Methods ──────────── __repr__, __eq__, __lt__, __add__, __len__ …
│
▼
6. Properties ──────────────── getter, setter, deleter, validation
│
▼
7. Encapsulation & ABCs ────── _private, @abstractmethod, composition
│
▼
8. Capstone: Library Management System
│
▼
9. Summary & further reading
Each section ends with a short hands-on exercise so you can test your understanding immediately.
- Python 3.10 or later
jupyter(or an IDE with native notebook support such as VS Code or PyCharm)
# 1. Clone the repo
git clone https://github.com/demirayonur/OOP-Tutorial.git
cd oop-python-tutorial
# 2. (Optional but recommended) create a virtual environment
python -m venv .venv
source .venv/bin/activate # macOS / Linux
# .venv\Scripts\activate # Windows
# 3. Install Jupyter
pip install jupyter
# 4. Launch
jupyter notebook OOP_in_Python_Tutorial.ipynbRead → Run → Break → Fix → Repeat.
- Read each markdown cell carefully. The explanations are written to build intuition, not just list facts.
- Run every code cell yourself. Don't just read the output.
- Break the code on purpose. Change a class variable to a list. Assign a negative value. See what happens.
- Fix it. That is where the real learning happens.
- Do the exercises before reading the solution in the next cell.
Each section is designed so you can stop, take a break, and come back without losing your place.
oop-python-tutorial/
├── OOP_in_Python_Tutorial.ipynb ← the tutorial (start here)
├── README.md ← you are here
└── .gitignore ← standard Python .gitignore
The final section builds a realistic mini-system that uses every concept from the tutorial:
- Abstract base class (
LibraryItem) with abstract methods - Inheritance tree:
PrintBook,EBook,AudioBook - Class-level ID counter auto-assigning unique IDs
- Properties with validation (page count, duration)
- State management (check-out / return logic)
- Dunder methods for equality, ordering, iteration, membership, and length
- Composition: a
Librarythat has items rather than is one - Alternative constructor via
@classmethod - Static helper via
@staticmethod
Do I need to install anything besides Jupyter?
No. The entire tutorial uses only the Python standard library (
abc, functools, dataclasses, collections.abc). No pip install ... of third-party packages is required.
Why Python 3.10+?
The tutorial uses modern type-hint syntax like
list[Book] and int | None (PEP 604), which require Python 3.10 or later. If you are on 3.9, the notebook still runs thanks to from __future__ import annotations, but upgrading is recommended.
I found a typo / have a suggestion. What should I do?
Open an issue or submit a pull request. See the Contributing section below.
Contributions are warmly welcomed! If you spot a typo, think an explanation could be clearer, or want to add an exercise:
- Fork the repository
- Create a feature branch (
git checkout -b improve/section-5-examples) - Commit your changes (
git commit -m 'Improve dunder section example') - Push to the branch (
git push origin improve/section-5-examples) - Open a Pull Request
For larger changes, please open an issue first to discuss what you would like to modify.
Happy coding. 🐍