🕯️ Magic Note
The word “data structure” sounds complex, but it is just a fancy term for “how you organize information.” Choosing the right container is more important than knowing every detail about one container. A good programmer thinks first about what they need to store, then picks the right structure.
| Family | What They Hold | Examples |
|---|---|---|
| Simple (Primitive) | Single value (one thing at a time) | Numbers, Strings, Booleans |
| Compound (Collections) | Multiple values grouped together | Lists, Dictionaries, Tuples, Sets |
| Type | What It Stores | Example |
|---|---|---|
| Integer (int) | Whole numbers (no decimals) | 5, -12, 1000 |
| Float (float) | Decimal numbers | 3.14, -0.5, 2.0 |
| String (str) | Text (letters, words, sentences) | “hello”, ‘Python’, “123” |
| Boolean (bool) | True or False (yes/no) | True, False |
| NoneType (None) | Represents nothing or emptiness | None |
Python
# Simple data structures in action
age = 25 # integer
price = 19.99 # float
name = “Feloriya” # string
is_learning = True # boolean
result = None # NoneType (nothing yet)
| Structure | How It Works | Analogy | Example |
|---|---|---|---|
| List | Ordered, changeable, allows duplicates | A shopping list | [1, 2, 3, “apple”] |
| Dictionary | Key-value pairs (label → value) | A phonebook | {“name”: “Ali”, “age”: 25} |
| Tuple | Ordered, unchangeable, allows duplicates | A sealed envelope | (1, 2, 3) |
| Set | Unordered, no duplicates | A bag of unique marbles | {1, 2, 3} |
Python
# Compound data structures in action
fruits = [“apple”, “banana”, “cherry”] # list
person = {“name”: “Sara”, “age”: 30} # dictionary
colors = (“red”, “green”, “blue”) # tuple
unique_numbers = {1, 2, 3, 3, 3} # set becomes {1, 2, 3}
- Use a simple type (int, float, str, bool) when you only need to remember one thing
- Use a list when order matters and you may change the items later
- Use a dictionary when you need to look up values by a label or name
- Use a tuple when you have a fixed collection that should never change
- Use a set when you only care about unique items and order does not matter
Python
# A list containing dictionaries (a collection of people)
people = [
{“name”: “Ali”, “age”: 25},
{“name”: “Sara”, “age”: 30},
{“name”: “Reza”, “age”: 28}
]
# A dictionary containing a list (a person with multiple hobbies)
person = {
“name”: “Mina”,
“hobbies”: [“reading”, “coding”, “coffee”]
}
🕯️ Magic Note
Nesting is how you model the real world. A library has many shelves (list). Each shelf has many books (another list). Each book has a title, an author, and a year (dictionary). Python lets you build this exactly as you imagine it.
- Confusing lists with dictionaries, lists use numeric positions, dictionaries use keys
- Forgetting that strings are sequences too, each character has a position like a list
- Trying to change a tuple my_tuple[0] = 5 causes a TypeError
- Assuming sets preserve order, they do not, so do not rely on positions
- Using a list when you need fast lookup by name, use a dictionary instead
- 1. Numbers in Python (integers, floats, and math operations)
- 2. Variables (naming and storing your data)
- 3. Strings (text manipulation and magic)
- 4. Lists (ordered collections you can change)
- 5. Dictionaries (key-value pairs for fast lookup)
- 6. Tuples (immutable sequences for fixed data)
- 7. Sets (unique collections without order)
- What is the difference between a simple and a compound data structure?
- If you need to store a list of student names that might change, which structure do you use?
- If you need to look up a person’s phone number by their name, which structure is best?
- What happens if you put duplicate values into a set?
⚡ Whisper
Every spell needs a vessel to hold its power. Numbers rest in integers. Words live in strings. Collections dance in lists and whisper through dictionaries. Before you learn to conjure, learn what holds the magic. The right container is half the spell.