beginner~4h

Python Fundamentals for AI

Master variables, loops, lists, list comprehensions, and basic NumPy arrays used in AI pipelines.

3
Subtopics
1
Exercises
1
Projects
1
Quiz Qs
1
Flashcards

🎓 Learning objectives

  • Write clean loops, conditionals, and functions in Python
  • Use list comprehensions to transform arrays of text
  • Perform matrix slicing and math using NumPy

What is it?

Python is a high-level, dynamically-typed programming language that has become the universal language of AI and data science. Every major AI framework — PyTorch, TensorFlow, HuggingFace Transformers, LangChain — has a Python-first API. If you want to build, fine-tune, deploy, or evaluate LLMs, you need Python.

Why it exists

AI engineering requires seamlessly chaining text processing, numerical computation, HTTP requests, and ML framework calls. Python's readable syntax and massive ecosystem make it the only language where you can go from raw text to deployed model in one codebase without switching tools.

Problem it solves

Before Python dominated AI, practitioners had to write low-level C/C++ or switch between multiple languages for different tasks. Python provides a single unified environment: string manipulation and file I/O (built-in), numerical arrays (NumPy), tabular data (Pandas), neural networks (PyTorch), and HTTP APIs (requests/httpx) — all from the same REPL.

Intuition

If coding is writing instructions, Python is writing them in clear, simple English. But Python for AI is more than syntax — it is about knowing which libraries to reach for. The pattern you will use constantly: load data with Pandas, transform it with list comprehensions or NumPy, pass it to a model or API, parse the response.

If you come from Java/Spring Boot: Python feels like Java without the type ceremony. No class boilerplate, no main() method, no semicolons. A Spring @Service with 50 lines of boilerplate becomes a 5-line Python function. NumPy is like Java's Streams but for numerical arrays — and 100× faster for element-wise operations because it delegates to optimized C.

If you come from React/Frontend: Python is like JavaScript without the async complexity for most use cases. List comprehensions are like .map() and .filter(). Dict comprehensions are like Object.fromEntries(). The REPL is like your browser console. NumPy arrays are like typed arrays — similar concept, 10× more operations available.

Analogy

Python for AI is like a chef's knife: not the fanciest tool, but the one everything else depends on. NumPy is your cutting board (fast, efficient, the foundation for all numerical work). Pandas is your mise en place bowls (organized tabular data). PyTorch is your oven (where the real transformation happens). HuggingFace is your spice rack (pre-built models and datasets for every flavor).

Technical explanation

Key Python patterns for AI engineering:

NUMPY: n-dimensional array library with C-optimized operations. Core ops: np.dot() for matrix multiplication, np.reshape(), np.concatenate(). Vectorized operations run 100× faster than Python loops because they execute in compiled C on contiguous memory.

PANDAS: DataFrame for tabular data. df.apply(), df.groupby(), df.merge(). Essential for cleaning training data, analyzing evaluation results, and processing CSV/JSON datasets.

LIST COMPREHENSIONS: [f(x) for x in items if condition(x)] — Pythonic way to transform lists. For AI: [chunk(text, size=512) for text in documents if len(text) > 100].

TYPE HINTS: def embed(text: str) -> list[float]: — Not enforced at runtime but essential for readable AI code. LangChain and Pydantic use type hints for validation.

ASYNC/AWAIT: async def fetch_all(prompts: list[str]) — critical for calling LLM APIs concurrently. asyncio.gather() lets you run 10 API calls in parallel instead of sequentially.

CONTEXT MANAGERS: with open(file) as f — Python's resource management pattern. Used everywhere in AI: model loading, database connections, API sessions.

DECORATORS: @lru_cache, @retry — modify function behavior without changing their code. Common in AI for caching embeddings and adding retry logic to API calls.

Architecture

Python AI stack layers: Application layer: your Python scripts/FastAPI endpoints Framework layer: LangChain, LlamaIndex, HuggingFace Transformers Numerical layer: NumPy, SciPy (C-accelerated arrays) GPU layer: PyTorch/TensorFlow (CUDA kernels via Python bindings) System layer: CPython interpreter + C extensions

Workflow

  1. Install dependencies: pip install numpy pandas anthropic
  2. Load data: pd.read_csv() or json.load() or pathlib.Path().read_text()
  3. Clean/transform: list comprehensions, df.apply(), str.split()
  4. Compute: NumPy vectorized operations for math, no Python loops
  5. Call model/API: requests.post() or anthropic.Anthropic().messages.create()
  6. Parse response: response.json() or Pydantic model validation
  7. Save results: df.to_csv(), json.dump(), or database write

Example

import numpy as np import anthropic from pathlib import Path

Pattern 1: Vectorized text processing with NumPy

texts = ["lora fine-tuning", "rag pipeline", "agent patterns"] lengths = np.array([len(t.split()) for t in texts]) # [2, 2, 2] print(f"Avg words: {lengths.mean()}") # 2.0

Pattern 2: List comprehension for chunking

def chunk_text(text: str, size: int = 512) -> list[str]: words = text.split() return [" ".join(words[i:i+size]) for i in range(0, len(words), size)]

Pattern 3: LLM API call with type hints

def classify(text: str) -> str: client = anthropic.Anthropic() response = client.messages.create( model="claude-opus-4-5", max_tokens=10, messages=[{"role": "user", "content": f"Classify as positive/negative: {text}"}] ) return response.content[0].text.strip()

Real-world usage

Every major AI framework is Python-first: PyTorch (Meta), TensorFlow (Google), HuggingFace Transformers (1M+ users), LangChain, LlamaIndex. The entire AI research community publishes code in Python. Any AI engineering role requires daily Python.

Trade-offs

Python trades execution speed for developer speed; high-performance parts are written in C++ (NumPy, PyTorch) and called from Python. For AI tasks, 99% of compute happens in those C++ extensions — Python just orchestrates them.

Visual explanation

Python AI pipeline flow: [Raw Text/JSON] → [Python str/list ops] → [Pandas DataFrame] → [NumPy array] → [PyTorch Tensor] → [Model/API call] → [Parse output]

Advantages

  • Super readable syntax

  • Massive AI library ecosystem

  • Rapid prototyping

Disadvantages

  • Slower raw execution speed

  • Global Interpreter Lock limits multi-threading

Common mistakes

  • Using Python for loops for array operations instead of NumPy vectorized ops. A loop over 1M elements takes ~1s in Python; np.sum() on the same array takes ~1ms. Always prefer np.dot(), np.sum(), np.mean() over manual loops for numerical work.

  • Not using virtual environments. Installing packages globally causes version conflicts across projects. Use python -m venv .venv or conda create for each project. AI projects have specific dependency versions that conflict with each other.

  • Ignoring type hints. LLM application code can be complex — type hints make it self-documenting and enable IDE autocomplete. def embed(texts: list[str]) -> list[list[float]] is clearer than def embed(texts).

  • Using synchronous API calls when you need to process batches. Calling the LLM API 100 times sequentially takes 100× as long as calling it concurrently. Use asyncio.gather() with async API clients for batch operations.

  • Not using pathlib for file operations. os.path.join() and open() with string concatenation is error-prone. pathlib.Path("data") / "train.jsonl" is cleaner, OS-independent, and has built-in .read_text(), .write_text(), .exists() methods.

🎤 Interview questions

Explain the difference between a Python list and a NumPy array. Why are NumPy arrays faster?

📂 Subtopics

📝 Quiz

💬 Deep Dive with AI

Related concepts

tokenization-basicsembeddings-basics

Next to learn

probability-basicsneural-networks

Next Step

Continue to Probability Intuition for Language Models