[AI ML| Learning Notes Day 1| Python for AI/ML — Foundations That Actually Matter]
Context
This refresher was not about Python syntax.
It was about how Python behaves under the hood and why certain constructs are preferred in ML systems and interviews.
1️⃣ Why Comprehensions vs Normal Loops
Example
# Normal loop
out = []
for x in nums:
out.append(x * x)
# Comprehension
out = [x * x for x in nums]
Important Clarification (FAANG Gotcha)
Comprehensions are NOT vectorized.
The computation still happens in Python, not C.
They are faster because they reduce interpreter overhead, not because they use C-level math.
When to Use
✅ Feature transformations
✅ Data cleaning
❌ Heavy numerical computation
2️⃣ Comprehension vs Generators
Example
# List comprehension
squares = [x*x for x in nums]
# Generator
squares = (x*x for x in nums)
ML Context
Use generators when:
Reading large files
Streaming logs
Training pipelines
Use comprehensions when:
You need random access
Data fits in memory
Interview Insight
Generators trade memory for control flow complexity.
3️⃣ Comprehension vs NumPy Vectors
Example
# Python
[x + y for x, y in zip(a, b)]
# NumPy
a + b
FAANG-Level Insight
NumPy moves the loop into C.
Comprehensions only make Python loops less bad.
Rule of Thumb
Use NumPy for:
Linear algebra
ML training
Numerical pipelines
Use comprehensions for:
Preprocessing
Feature extraction
Control-heavy logic
4️⃣ Counter (collections.Counter)
Why Counter Exists
from collections import Counter
freq = Counter(nums)
Missing keys return
0Built for frequency counting
Extremely common in ML & NLP
Common Use Cases
Token frequency
Histogram building
Sliding window problems
Frequency-based filtering
5️⃣ Counter vs defaultdict
defaultdict
from collections import defaultdict
freq = defaultdict(int)
Interview Insight
Counter is semantic clarity.
defaultdict is structural flexibility.
6️⃣ defaultdict vs Normal dict
Normal dict
if key not in d:
d[key] = 0
d[key] += 1
defaultdict
d[key] += 1
Why defaultdict Is Preferred
Fewer conditionals
Less bug-prone
Cleaner logic
ML Context
Used heavily in:
Graphs
DP
Sliding window
Counting-based problems
7️⃣ list vs tuple (ML Perspective)
When to Use tuple
Coordinates
Model configs
Dictionary keys
Fixed parameters
FAANG Insight
Use tuple to express intent:
“This should never change.”
Part 2: Algorithmic Thinking with Performance Optimization
These problems were vehicles for learning optimization, not the goal.
1️⃣ Two Sum
Brute Force — O(n²)
Try all pairs
Optimal — Hash Map — O(n)
Insight
Replace repeated scans with constant-time lookup
📌 Pattern
Cache what you’ve already seen.
2️⃣ Length of Longest Substring Without Repeating Characters
This is the foundational sliding window problem.
Brute Force — O(n²)
For every start index:
Expand substring
Track duplicates using a set
📌 Why it fails
Restarting work from scratch repeatedly
Optimal — Sliding Window + Hash Set / Map — O(n)
Core Idea
Maintain a window with unique characters
Expand right pointer
If duplicate appears:
Shrink from the left until valid
Why it’s O(n)
Each character enters and leaves the window once
📌 Critical Insight
Sliding window works when pointers only move forward.
This pattern appears everywhere:
Streaming ML
NLP token windows
Time-series analysis
3️⃣ Product of Array Except Self
Brute Force — O(n²)
Multiply all except current index
Better — Prefix & Suffix Arrays
Prefix product from left
Suffix product from right
Optimal — O(1) Extra Space
Store prefix in output
Track suffix in a variable
📌 FAANG Insight
Space optimization = reuse memory, not new tricks.
🔑 Key Takeaways from Day 1
Python choices directly affect ML performance
ML Python Lab in class today > > > > Youtube/ Udemy / Coursera Python Tutorials
NumPy is not optional — it’s foundational
Sliding window is a core real-world pattern
Hashing is your first optimization weapon
LeetCode builds engineering intuition, not tricks
🚀 What’s Next
Next class:
NumPy internals
Vectorization
Memory layout
Performance intuition for ML workloads
This is where ML engineering actually begins.








The NumPy vs comprehensions breakdown is super useful, I always wondered why people say "just use NumPy" without explaining the actual difrence. That sliding window pattern explanation really clicked for me, been struggling with those problems for weeks. I dunno why more tutorials don't focus on the "why" like this. Definately bookmarking this for interview prep!