Reference library

OOP & classes

Classes, instances, methods, dataclasses, and object-oriented design in Python.

8 matches
OOP & classes easy

Filtering data with a Python class helper

A beginner-friendly DataFilter class that filters lists of dictionaries by exact match, greater-than, and substring conditions.

filter oop class
Python
class DataFilter:
    """A beginner-friendly helper to filter lists of dictionaries."""
    
    def __init__(self, data):
        self.data = data
    
    def filter_by(self, key, value):
        """Return items where data[key] == value."""
        return [item for item in self.data if item.get(key) == value]
    
 …
14 0 Open
OOP & classes easy

Graph Class with Adjacency Dict in Python

Build an undirected graph class using a dictionary of adjacency lists with methods to add vertices, edges, remove edges, and query neighbors.

graph oop adjacency-list
Python
class Graph:
    def __init__(self):
        self.adjacency = {}

    def add_vertex(self, vertex):
        if vertex not in self.adjacency:
            self.adjacency[vertex] = []

    def add_edge(self, u, v):
        self.add_vertex(u)
        self.add_vertex(v)
        self.adjacency[u].append(v)
        self.adja…
12 0 Open
OOP & classes easy

How to Build an In-Memory CRUD Repository Class in Python

Define a Python Repository class that stores objects in a dictionary and supports create, read, update, delete, and list operations.

repository crud oop
Python
class Repository:
    def __init__(self):
        self._data = {}

    def create(self, key, value):
        self._data[key] = value
        return key

    def read(self, key):
        return self._data.get(key)

    def update(self, key, value):
        if key not in self._data:
            raise KeyError(f"Key '{ke…
14 0 Open
OOP & classes easy

How to Convert Data Types in Python with a Helper Class

This code defines a beginner-friendly OOP helper class for common data conversions like string to list, list to dict, JSON string, and CSV row, with an advanced subclass for numeric casting.

oop classes data-conversion
Python
class DataConverter:
    """A beginner-friendly helper class for common data conversions."""
    
    def __init__(self, data):
        self.data = data
    
    def to_list(self):
        """Convert string data (comma-separated) to a list."""
        if isinstance(self.data, str):
            return [item.strip() for…
14 0 Open
OOP & classes easy

How to Create a Data Formatter Class in Python

A beginner-friendly helper class to format lists, dictionaries, and stored records into readable strings.

oop class formatting
Python
class DataFormatter:
    """Helper class for beginners to format common data types."""
    
    def __init__(self, name="data"):
        self.name = name
        self.records = []
    
    def add_record(self, key, value):
        """Add a key-value record to the formatter."""
        self.records.append({"key": key, …
12 0 Open
OOP & classes easy

How to Implement Iterator Protocol on a Custom Class in Python

Create a custom iterable class by defining the __iter__ and __next__ methods, enabling use in for loops and list conversions.

iterator protocol class
Python
class Countdown:
    """Iterator that counts down from start to 0."""

    def __init__(self, start):
        self.start = start
        self.current = start

    def __iter__(self):
        return self

    def __next__(self):
        if self.current < 0:
            raise StopIteration
        value = self.current
 …
12 0 Open
OOP & classes easy

How to Implement a Stack Class in Python

A complete Stack class implemented with a Python list, featuring push, pop, peek, is_empty, size, and a readable string representation.

oop stack data-structures
Python
class Stack:
    def __init__(self):
        self._items = []

    def push(self, item):
        """Add an item to the top of the stack."""
        self._items.append(item)

    def pop(self):
        """Remove and return the top item. Raises IndexError if empty."""
        if self.is_empty():
            raise IndexE…
13 0 Open
OOP & classes easy

How to Validate Data Types in Python with a Class

A beginner-friendly Python class that checks if a value is a string, integer, float, list, or empty, using simple methods and isinstance checks.

class validation type checking
Python
class DataValidator:
    """A simple data validation helper for beginners."""
    
    def __init__(self, data):
        self.data = data
    
    def is_string(self):
        return isinstance(self.data, str)
    
    def is_integer(self):
        return isinstance(self.data, int) and not isinstance(self.data, bool)
…
13 0 Open

Browse by section

Each section groups closely related Python snippets.

OOP & classes — Python code examples

What you will find here

This page collects oop & classes snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.