Objects in Programming – The Core Building Block of OOP Explained

When I started coding, I kept throwing everything into separate variables. A person’s name in one, their age in another, their city somewhere else. It worked — but it was a mess.

Then I learned about objects. And suddenly, all that scattered data had a home.

If you’re just getting into basic coding concepts and objects feel confusing right now, don’t worry. I’ll break it down simply — real examples, clear explanations, no fluff.

📦 Objects

What Is an Object in Programming?

You already know what an object is — you just haven’t called it that yet. Let me show you exactly what I mean.


🔍 Analogy

Real-Life Analogy — You Already Know This

🤔 Think about a car. It has a color, a brand, a speed — and it can do things like start, stop, and accelerate. All of that information belongs to one thing: the car. That is exactly what an object is in programming.

Every object has two things — data (what it knows) and actions (what it can do). In coding, we call these properties and methods.

🚗

Car

Properties it holds

brand:“Toyota”
color:“red”
speed:120
start():action
👤

Person

Properties it holds

name:“Sara”
age:25
city:“Lahore”
greet():action
📱

Phone

Properties it holds

brand:“Apple”
storage:“128GB”
color:“black”
call():action
📖 Definition

So What Exactly Is an Object?

When I started coding, I stored everything in separate variables — name, age, city — all floating separately. Messy and hard to manage. Objects fixed that completely.

An object lets you group all related data — and actions — into one single place. That is all it is.

An object is a collection of related data and actions stored together under one name, where each piece of data has a label — called a key — and a value attached to it.

object property method key-value pair
⚠️ The Problem

Why Do We Need Objects?

Before I learned objects, my code looked like a cluttered drawer. Everything was in the right place — but finding anything was a nightmare. Let me show you what I mean.


😬 Without Objects

The Problem Without Objects

Say you want to store details about one person — name, age, and city. Without objects, you’d write three separate variables. Now imagine doing this for 50 users. That’s 150 loose variables with no connection.

❌ Messy Way — No Objects
userName = “Sara”
userAge = 25
userCity = “Lahore”

# …and 147 more for 50 users 😩
VS
✅ With Objects

How One Object Organizes Everything

One object holds all the data together — labeled, organized, and easy to access. Everything about one user lives in one place.

✅ Clean Way — Using an Object
user = {
  “name”: “Sara”,
  “age”: 25,
  “city”: “Lahore”
}

# Access instantly
print(user[“name”]) # Sara
One object. All related data. Instant access. That is the real reason every programmer uses objects — it keeps your code clean and your data connected.
🌍 Real Life

Real-World Use Cases of Objects

Every app you use today stores its data in objects behind the scenes. Here are some you’ll recognize:

👤

User Profiles

Name, email, age — all in one user object

🛍️

Products

Title, price, stock — one product object

📦

Orders

Items, total, address — grouped in one order

🎮

Game Characters

Health, score, level — all in a player object

⚙️ Two Parts

Properties and Methods — The Two Parts of Every Object

Every object in programming is made of exactly two things. Once you know what they are, reading and writing objects becomes second nature.


📋 Properties — What It Knows

A property is a piece of data stored inside an object. Think of it as a label with a value attached.

🔵
name: “Sara” — a string value
🔵
age: 25 — a number value
🔵
city: “Lahore” — another string
⚡ Methods — What It Does

A method is a function stored inside an object. It defines an action the object can perform.

🟣
greet() — says hello
🟣
login() — logs the user in
🟣
logout() — logs the user out
🧠 Easy way to remember: Properties = what an object IS. Methods = what an object DOES. A person IS named Sara and IS 25 years old. A person DOES greet and DOES login.
👁️ Visual

Object Structure Diagram

Here is what a complete object looks like in code — with both properties and methods clearly labeled:

person = {

  “name”: “Sara”,property
  “age”: 25,property
  “city”: “Lahore”,property

  “greet”: function() { … },method
  “login”: function() { … }method

}
Blue = Properties (data)
Purple = Methods (actions)
Properties store data. Methods perform actions. Together they make an object complete — a self-contained unit that knows things and can do things.
✍️ Create

How to Create an Object

Creating an object is easier than it looks. You just wrap your data in curly braces, give each piece a label, and you’re done.


🌐 Syntax

Object Syntax in JavaScript + Python

🗂️ Think of curly braces { } as a folder. Each line inside is a label: value pair. The label is the key, the value is the data you’re storing.
⚡ JavaScript
let user = {
  name: “Sara”,
  age: 25,
  city: “Lahore”
};
🐍 Python
user = {
  “name”: “Sara”,
  “age”: 25,
  “city”: “Lahore”
}
Same idea in both languages — curly braces, key-value pairs, commas between them. JS uses unquoted keys, Python uses quoted keys. That’s the only real difference.
📦 Empty vs Filled

Empty Object vs Pre-Filled Object

You can start with an empty object and add data later — or fill it right away. Both approaches are valid and used in real projects every day.

⬜ Empty Object
// JavaScript
let user = {};

# Python
user = {}
Start empty — add properties when your program runs
? key ? key ? key
✅ Pre-Filled Object
// JavaScript
let user = { name: “Sara” };

# Python
user = { “name”: “Sara” }
Values ready from the start — no waiting needed
✓ name ✓ age ✓ city
🔎 Access

How to Access Object Data

Creating an object is step one. Getting data out of it is step two. There are two ways to do it — and both are simple once you see them side by side.


🔵 Dot Notation
object.key

Put a dot after the object name, then write the key directly. Clean, short, and the most common way beginners use.

Use when: you know the key name in advance

🟡 Bracket Notation
object[“key”]

Wrap the key in square brackets and quotes. More flexible — works even when the key name has spaces or special characters.

Use when: key has spaces or is stored in a variable

user.name “Sara” dot notation
user[“age”] 25 bracket notation
user.city “Lahore” dot notation
💻 Code Examples

Code Examples — JS + Python

Same object, same two ways to access — just slightly different syntax per language.

⚡ JavaScript
let user = { name: “Sara”, age: 25, city: “Lahore” };

// Dot notation
console.log(user.name); // “Sara”
console.log(user.age); // 25

// Bracket notation
console.log(user[“city”]); // “Lahore”
🐍 Python
user = { “name”: “Sara”, “age”: 25, “city”: “Lahore” }

# Python uses bracket notation only
print(user[“name”]) # Sara
print(user[“age”]) # 25
print(user[“city”]) # Lahore
SituationUse ThisExample
Key name is simpleDot notationuser.name
Key has spacesBracket notationuser["first name"]
Using PythonBracket notationuser["name"]
Default to dot notation in JavaScript — it’s cleaner. Switch to bracket notation when the key has spaces or you’re using Python.
🔧 Modify

Adding, Updating and Deleting Properties

Objects are not locked after you create them. You can add new data, change existing values, or remove what you no longer need — anytime.


➕ Add

Assign a value to a key that does not exist yet — the object creates it on the spot.

user.email = "sara@mail.com"
✏️ Update

Assign a new value to a key that already exists — it simply overwrites the old one.

user.age = 26
🗑️ Delete

Use the delete keyword before the property — it removes it completely.

delete user.city
Start
name: “Sara” age: 25 city: “Lahore”
After
name: “Sara” age: 26 ✏️ city ✗ email ➕
💻 Code Examples

Code Examples — JS + Python

⚡ JavaScript
let user = { name: “Sara”, age: 25, city: “Lahore” };

// ➕ Add new property
user.email = “sara@mail.com”;

// ✏️ Update existing property
user.age = 26;

// 🗑️ Delete a property
delete user.city;

console.log(user);
// { name: “Sara”, age: 26, email: “sara@mail.com” }
🐍 Python
user = { “name”: “Sara”, “age”: 25, “city”: “Lahore” }

# ➕ Add new property
user[“email”] = “sara@mail.com”

# ✏️ Update existing property
user[“age”] = 26

# 🗑️ Delete a property
del user[“city”]

print(user)
# {‘name’: ‘Sara’, ‘age’: 26, ’email’: ‘sara@mail.com’}
Add, update, delete — same syntax, different outcome. Assign to add or update. Use delete or del to remove. That is all you need.
⚡ vs

Objects vs Arrays — What’s the Difference?

Both store multiple values. But they do it differently — and knowing which to reach for makes your code much cleaner.


Simple Comparison

An array is a numbered list — great for items of the same kind. An object is a labeled collection — great for describing one thing with many details.

📦 Array — Numbered List
“Ali”[0]
“Sara”[1]
“Hamza”[2]
Access by index number
🗂️ Object — Labeled Data
name:“Sara”
age:25
city:“Lahore”
Access by key name
FeatureArrayObject
Access byIndex [0]Key “name”
Best forLists of itemsOne thing, many details
Order matters✅ YesNot always
Example[“Ali”,”Sara”,”Hamza”]{name:”Sara”, age:25}

📦 Use Array when:

✅ You have a list of similar items
✅ Order matters
✅ You need to loop through all items

🗂️ Use Object when:

✅ Items have different labels
✅ You describe one specific thing
✅ You access by name, not position
Simple rule: list of names → array. One person’s details → object. When in doubt, ask yourself: “Am I storing a list, or describing one thing?”
❓ FAQs

FAQs About Objects in Programming

Three questions beginners always ask — answered simply and clearly.


Q1

What is a class vs object in Python?

A class is a blueprint — it defines what an object should look like. An object is the actual thing built from that blueprint. Think of a class like a house plan, and the object as the actual house you live in.

📐 Class — The Blueprint
🟣 Defines structure
🟣 Written once
🟣 No data yet
🟣 Like a cookie cutter
📦 Object — The Real Thing
🔵 Built from the class
🔵 Has actual data
🔵 Can make many copies
🔵 Like the cookie itself
🐍 Python Example
# Class = blueprint
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

# Object = actual instance built from class
sara = Person(“Sara”, 25)
print(sara.name) # Sara
One class, unlimited objects. You write the class once, then create as many objects as you need — each with their own data.
Q2

What is the difference between object and array?

Both store multiple values — but they organize data differently. Arrays use numbered positions. Objects use named keys.

📦 Array — use for lists
Items accessed by index number.
Best for: same type of things.

names[0] → “Ali”
names[1] → “Sara”
🗂️ Object — use for details
Items accessed by key name.
Best for: describing one thing.

user.name → “Sara”
user.age → 25
Quick rule: storing a list of students → array. Storing details about one student → object. Simple as that.
Q3

Can objects have objects inside them?

Yes — and this is called a nested object. It is when one object contains another object as a value. This is how real-world data is structured in actual projects.

user = {
  “name”: “Sara”,
  “age”: 25,
  “address”: { <– object inside object
    “city”: “Lahore”,
    “country”: “Pakistan”
  }
}

# Access nested value
print(user[“address”][“city”])
→ “Lahore”

To get a nested value, you just chain the keys — go into the outer object first, then into the inner one.

Nested objects are everywhere in real projects — user profiles, API responses, product data. Once you understand the basic object, nested ones make complete sense.