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.
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.
Real-Life Analogy — You Already Know This
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
Person
Properties it holds
Phone
Properties it holds
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.
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.
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.
userAge = 25
userCity = “Lahore”
# …and 147 more for 50 users 😩
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.
“name”: “Sara”,
“age”: 25,
“city”: “Lahore”
}
# Access instantly
print(user[“name”]) # Sara
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
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.
A property is a piece of data stored inside an object. Think of it as a label with a value attached.
A method is a function stored inside an object. It defines an action the object can perform.
Object Structure Diagram
Here is what a complete object looks like in code — with both properties and methods clearly labeled:
“name”: “Sara”,property
“age”: 25,property
“city”: “Lahore”,property
“greet”: function() { … },method
“login”: function() { … }method
}
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.
Object Syntax in JavaScript + Python
name: “Sara”,
age: 25,
city: “Lahore”
};
“name”: “Sara”,
“age”: 25,
“city”: “Lahore”
}
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.
let user = {};
# Python
user = {}
let user = { name: “Sara” };
# Python
user = { “name”: “Sara” }
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.
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
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
Code Examples — JS + Python
Same object, same two ways to access — just slightly different syntax per language.
// Dot notation
console.log(user.name); // “Sara”
console.log(user.age); // 25
// Bracket notation
console.log(user[“city”]); // “Lahore”
# Python uses bracket notation only
print(user[“name”]) # Sara
print(user[“age”]) # 25
print(user[“city”]) # Lahore
| Situation | Use This | Example |
|---|---|---|
| Key name is simple | Dot notation | user.name |
| Key has spaces | Bracket notation | user["first name"] |
| Using Python | Bracket notation | user["name"] |
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.
Assign a value to a key that does not exist yet — the object creates it on the spot.
user.email = "sara@mail.com"Assign a new value to a key that already exists — it simply overwrites the old one.
user.age = 26Use the delete keyword before the property — it removes it completely.
delete user.cityCode Examples — JS + Python
// ➕ 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” }
# ➕ 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’}
delete or del to remove. That is all you need.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.
| Feature | Array | Object |
|---|---|---|
| Access by | Index [0] | Key “name” |
| Best for | Lists of items | One thing, many details |
| Order matters | ✅ Yes | Not always |
| Example | [“Ali”,”Sara”,”Hamza”] | {name:”Sara”, age:25} |
📦 Use Array when:
🗂️ Use Object when:
FAQs About Objects in Programming
Three questions beginners always ask — answered simply and clearly.
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 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
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.
Best for: same type of things.
names[0] → “Ali”names[1] → “Sara”Best for: describing one thing.
user.name → “Sara”user.age → 25Can 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.
“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.
