Python Classes/Objects

Python · Functions & OOP

Theory

A class is a blueprint that describes what data an object holds and what it can do; an object (or instance) is one concrete thing built from that blueprint. Calling the class like a function, Employee('Asha'), creates a new object and automatically runs the special __init__ method, which sets up its attributes.

Inside methods, self refers to the specific object being worked on, so self.name = name stores the name on that object only. Two objects from the same class, Employee('Asha') and Employee('Ravi'), share the class's methods but keep separate attribute values. Attributes set on self are instance attributes; variables written directly in the class body (company = 'TechTown') are class attributes shared by all instances.

Why we use it

Classes let you create many similar objects without repeating setup code. Each object keeps its own state, so you can manage hundreds of employees, orders or accounts with one well-tested definition.

When to use it

Define a class when several values always travel together and need their own operations. If you only need to group a few values with no behaviour, a dict, a namedtuple or a dataclass may be lighter.

How it works

  1. Python reads class Employee: ...
  2. Calling Employee('Asha') creates an empty object
  3. __init__(self, 'Asha') runs on that new object
  4. self.name = 'Asha' stores an instance attribute
  5. The finished object is returned and assigned to emp
  6. emp.name gives 'Asha'

Syntax

obj = ClassName(args)

Example

emp = Employee('Asha')
print(emp.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