Python OOP

Python · Functions & OOP

Theory

Object-Oriented Programming (OOP) is a way of organising code around objects - things that bundle data (attributes) with the behaviour that works on that data (methods). You describe a kind of thing once as a class, then create as many objects of it as you need. An Employee class might hold name and salary and offer a give_raise() method; every employee object carries its own values but shares the same methods.

OOP rests on four ideas: encapsulation (keep data and methods together and guard internal state), abstraction (expose a simple interface, hide the details), inheritance (a child class reuses a parent's code) and polymorphism (different classes respond to the same method call in their own way). Libraries you use daily are built this way: a pandas DataFrame is an object with data inside and methods like groupby().

Why we use it

As programs grow, loose variables and functions become hard to track. OOP groups related data and logic into self-contained units, so code mirrors real-world things, is easier to extend and is safer to change.

When to use it

Use classes when you model things that have both state and behaviour (accounts, employees, ML models) or need many similar objects. For a short script that just transforms data step by step, plain functions are often simpler.

How it works

  1. Identify a real-world thing (e.g. Employee)
  2. List its data: name, salary
  3. List its actions: give_raise(), describe()
  4. Write a class that holds both
  5. Create objects from the class
  6. Need a specialised kind (Manager)? Yes: Inherit and extend the class. No: Use the class as it is.
  7. Objects call methods on their own data

Syntax

class ClassName:
    def __init__(self, ...):
        ...

Example

class Employee:
    def __init__(self, name):
        self.name = name

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic