Skip to content
English

Switch theme

Math & Machine Learning Foundations

First Step into AI Coding: Python, NumPy and Your Dev Environment (venv, Git, Jupyter)

Beginner 25 min For: Beginners / Developers / Researchers

Key takeaways

  • ▸AI code uses a small slice of Python: lists, dicts, functions, classes, loops and imports. Get fluent with those first.
  • ▸NumPy is built around the ndarray, a multi-dimensional array of one data type. Replacing per-element loops with whole-array operations (vectorisation) makes code shorter and usually much faster.
  • ▸Broadcasting compares shapes from the last dimension backwards; two dimensions are compatible when they are equal or one of them is 1. Understand this and most model code becomes readable.
  • ▸Use one virtual environment per project (python -m venv or uv) and keep your code in Git.
  • ▸Experiment in Jupyter or Colab, then move reusable code into .py files.

The short answer

The coding foundation for AI is three things: writing Python, computing with arrays in NumPy, and a clean, reproducible dev environment. In this unit of the Maths & Code stage that takes about 12 hours, and everything after — PyTorch, model APIs, RAG — builds on it.

Step 1: just enough Python

The official Python Tutorial is the authoritative starting point. These are the pieces you meet most often in AI code:

Syntax What it looks like in AI code
list a batch of samples, a sequence of tokens: batch = [x1, x2, x3]
dict configs and API request bodies: {"model": "...", "messages": [...]}
functions with defaults def train(lr=1e-3, epochs=10): ...
classes model definitions: class MyNet(nn.Module): ...
list comprehensions lengths = [len(t) for t in texts]
imports import numpy as np

A good route: read chapters 3–9 of the tutorial (control flow, data structures, modules, classes), then do the Kaggle Learn Python course and practise right in the browser.

Step 2: NumPy and vectorisation

NumPy is the foundation of numerical Python. Its core object, the ndarray, is a multi-dimensional array of a single type; every array has a shape and a dtype (NumPy basics). The NumPy paper describes it as the foundation of the whole scientific Python ecosystem, with other array libraries — deep-learning tensors included — offering NumPy-like interfaces (Harris et al., 2020).

Vectorisation means you don't loop over elements in Python; you give one instruction for the whole array.

import numpy as np

x = np.random.rand(1_000_000)

# Loop: every element goes through the Python interpreter
y_loop = [v * 2 + 1 for v in x]

# Vectorised: one statement, executed in NumPy's compiled code
y_vec = x * 2 + 1

Both give the same numbers, but the vectorised version hands the loop to NumPy's compiled internals and skips per-element interpreter overhead. Compare them yourself with %timeit in Jupyter.

Broadcasting

How do you add arrays of different shapes? NumPy's rule: compare dimensions starting from the last (rightmost) one and move left; two dimensions are compatible when they are equal or one of them is 1. Otherwise you get an error (official docs).

images = np.random.rand(32, 28, 28)   # 32 images of 28×28
mean = images.mean(axis=0)            # shape (28, 28)
centered = images - mean              # (32,28,28) vs (28,28): aligned from the right, compatible

Model code is full of this: subtracting a mean from a batch, adding a bias vector, a row-wise softmax.

A tiny example: linear regression in pure NumPy

Put it together and fit a line (linear regression) with gradient descent. The idea is explained in How Machines Learn.

import numpy as np

rng = np.random.default_rng(0)
X = rng.random((200, 1))
y = 3 * X[:, 0] + 2 + 0.1 * rng.standard_normal(200)

w, b, lr = 0.0, 0.0, 0.5
for step in range(500):
    pred = X[:, 0] * w + b          # vectorised prediction
    err = pred - y
    grad_w = 2 * (err * X[:, 0]).mean()
    grad_b = 2 * err.mean()
    w -= lr * grad_w
    b -= lr * grad_b

print(round(w, 2), round(b, 2))     # close to 3 and 2

The only loop is over training steps; the work on all 200 samples inside each step is vectorised. In the next lesson, PyTorch and Autograd, the framework computes grad_w and grad_b for you.

Step 3: your dev environment

Virtual environments

Projects often need conflicting library versions. Python's built-in venv creates a separate directory per project; packages installed there are isolated by default from the base environment and from other virtual environments (venv docs):

python -m venv .venv
source .venv/bin/activate        # Windows: .venv\Scripts\activate
pip install numpy jupyter

uv is an alternative that handles environments, installs and version locking in one faster tool.

Git

Put code, experiment configs and small datasets in Git. The first three chapters of Pro Git cover what you need: init, add, commit, branch, push. MIT's Missing Semester teaches the shell, scripting, Git and debugging tools together and is a great catch-up course.

Jupyter and Colab

A Jupyter notebook keeps code, output and notes on one page — ideal for exploring data and running experiments. Without a suitable computer, use Google Colab in the browser (FAQ). A good habit: prototype in a notebook, then move stable functions into .py files so you can reuse them and diff them in Git.

Common mistakes

  • "I'll finish learning Python before touching AI." No need. Know the table above and learn the rest while building.
  • Triple for-loops in NumPy. They can almost always be rewritten as matrix operations or broadcasting — and that rewrite is the best practice you can get.
  • One global environment for everything. You will hit version conflicts sooner or later, and nobody can reproduce your results.
  • Sprinkling reshape until the error goes away. print(a.shape) first, align shapes from the right by the broadcasting rule, and know what each dimension means.

Try it yourself

  1. Finish every exercise in Kaggle Learn Python.
  2. Do the first 50 problems of numpy-100.
  3. Unit project: without loops, use only NumPy to normalise a batch of images, do a matrix multiplication and a row-wise softmax, then compare speed with a loop version using %timeit.
  4. Put the project in a fresh virtual environment and a Git repo with a requirements.txt (or a uv lockfile) so anyone can reproduce it.

For a compact review, read Stanford CS231n's Python Numpy Tutorial, one page covering the Python and NumPy a deep-learning course needs.

FAQ

Do I have to learn Python to learn AI?

Not to use AI products. But as soon as you call model APIs, build RAG or fine-tune, the major frameworks (PyTorch, Hugging Face, scikit-learn) and almost every official example are in Python, so it is the easiest path.

How do NumPy and PyTorch relate?

Both are multi-dimensional array libraries with very similar syntax. NumPy runs mainly on the CPU; PyTorch tensors can also live on a GPU and support automatic differentiation. Learn NumPy first and the move to PyTorch is almost seamless.

venv, conda or uv?

venv ships with Python and is the simplest; uv is a faster package and project manager; conda suits scientific stacks that need non-Python dependencies. Pick one and stick to it — what matters is a separate environment per project.

What if my computer is weak?

Use Google Colab: Python notebooks run in the browser, and there is a free GPU runtime with usage limits.

Software used in this lesson

Sources & further reading

  1. The Python Tutorial (official)
  2. Python docs: Data Structures
  3. Python docs: venv — creation of virtual environments
  4. NumPy: the absolute basics for beginners
  5. NumPy quickstart
  6. NumPy docs: Broadcasting
  7. Harris et al. (2020), Array Programming with NumPy (Nature)
  8. Stanford CS231n, Python Numpy Tutorial
  9. MIT, The Missing Semester of Your CS Education
  10. Pro Git (official Git book)
  11. Project Jupyter documentation
  12. uv documentation
  13. Google Colab FAQ

Data updated: Learn AI curriculum · Lesson news(UTC+8)