0%

2- Data Structures in Python

Data structures are the containers that hold your information. A number, a name, a list of prices, or a phonebook of contacts. Before you write spells, you must understand what holds your magic.

Every program works with data. A game keeps track of your score. A weather app stores temperature numbers. A contact list saves names and phone numbers. But how does Python actually hold this information? The answer is data structures. Think of them as different types of containers. Some containers hold a single item, like a small box. Others hold many items, like a drawer or a bookshelf. Each container has its own rules, strengths, and purposes. In Python, data structures are divided into two main categories: simple (primitive) and compound (collections).

🕯️ 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.

The Two Families of Data Structures
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
Here is a simple way to remember the difference: A simple data structure is like a single envelope holding one letter. A compound data structure is like a drawer holding many envelopes, or a cabinet with labeled folders.
Simple Data Structures (Primitive Types)
These are the building blocks. Every piece of data you will ever use is made from these basic types.
TypeWhat It StoresExample
Integer (int)Whole numbers (no decimals)5, -12, 1000
Float (float)Decimal numbers3.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 emptinessNone

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)

💡 You can check the type of any data structure using the type() function. For example, type(25) returns . This is a helpful spell for debugging.
Compound Data Structures (Collections)
When you need to store more than one thing, you reach for a compound data structure. Each one organizes data differently.
StructureHow It WorksAnalogyExample
ListOrdered, changeable, allows duplicatesA shopping list[1, 2, 3, “apple”]
DictionaryKey-value pairs (label → value)A phonebook{“name”: “Ali”, “age”: 25}
TupleOrdered, unchangeable, allows duplicatesA sealed envelope(1, 2, 3)
SetUnordered, no duplicatesA 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}

⚠️ Sets automatically remove duplicates. If you create a set with {1, 2, 2, 3}, Python stores only {1, 2, 3}. This is useful but can surprise you if you expect to keep all values.
Choosing the Right Container
Different situations need different containers. Here is a simple guide:
  • 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
Data Structures Can Hold Each Other
This is where things become powerful. Compound data structures can contain other data structures. A list can hold dictionaries. A dictionary can hold lists. A list can even hold other lists. This is called nesting, and it allows you to represent complex real-world information.

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.

Common Mistakes
  • 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
⚠️ A common beginner mistake is using the wrong structure and then fighting against it. If you find yourself writing complex workarounds, stop and ask: “Should I be using a different data structure?” The right container makes your code simpler, not harder.
What You Will Learn in This Studio
Throughout this course, we will explore each data structure in detail. Here is the path ahead:
  • 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)
💡 Do not try to memorize every detail from this lesson. The goal here is to know that these containers exist. When you face a problem later, you will remember “I think I need a dictionary for this” and then you can revisit the detailed lesson on dictionaries.
Check Your Understanding
Answer these questions in your mind before moving to the next lesson:
  • 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.

Related posts