📘 Python Lecture: Data Structures
1. Introduction to Data Structures
● A data structure is a way of organizing and storing data in a
computer so that it can be used efficiently.
● Think of it like containers for data:
○ A box for books 📚
○ A drawer for clothes 👕
○ A dictionary for words 📖
👉 The choice of data structure affects how fast and efficient a program is.
2. Why Do We Need Data Structures?
● To store large amounts of data in a systematic way.
● To access, modify, and manage data quickly.
● To perform operations like searching, sorting, inserting, and deleting
efficiently.
● To solve real-world problems (banking, shopping apps, search
engines, etc.).
3. Types of Data Structures
🔹 1. Primitive Data Structures
These are the basic building blocks.
● int → numbers
● float → decimal numbers
● str → text
● bool → True/False
Example:
age = 17
pi = 3.14
name = "Amit"
is_student = True
🔹 2. Non-Primitive (or Composite) Data Structures
Built using primitive data types.
(A) Linear Data Structures
Data is stored in a sequence.
● Lists → ordered, changeable/ Mutable
● Tuples → ordered, unchangeable/ Immutable
● Stacks → last in, first out (LIFO)
● Queues → first in, first out (FIFO)
👉 Example (list):
marks = [90, 85, 78, 92]
print(marks[0]) # 90
(B) Non-Linear Data Structures
Data is not in a sequence, but in hierarchical or interconnected form.
● Sets → unordered, unique values
● Dictionaries → key-value pairs
👉 Example (dictionary):
student = {"name": "Amit", "age": 17, "class": "XII"}
print(student["name"]) # Amit
4. Comparison of Data Structures
Data Ordere Mutabl Allows Example Use
Structure d? e? Duplicates?
List ✅ Yes ✅ Yes ✅ Yes Store marks of
students
Tuple ✅ Yes ❌ No ✅ Yes Store coordinates
(x, y)
Set ❌ No ✅ Yes ❌ No Store unique
subjects
Dictionary ❌ No ✅ Yes Keys ❌, Values Store student
✅ records
5. Real-Life Analogies
● List → To-do list 📝 (ordered, can change)
● Tuple → Date of birth 🎂 (fixed, cannot change)
● Set → Basket of fruits 🍎🍌🍊 (no duplicates)
● Dictionary → Phone directory 📱 (name → number)
6. Practice Questions
1. What is a data structure? Why is it important?
2. Differentiate between primitive and non-primitive data structures.
3. Give 2 examples each of linear and non-linear data structures.
4. Which data structure would you use to:
○ Store a shopping list?
○ Store roll numbers with names?
○ Store a student’s birthdate?
○ Store a set of unique books?