2026-06-08 · 9 min read
How to build a neural network from scratch in Python (no math degree required)
Most guides on building a neural network from scratch in Python open with calculus and matrix notation. That is exactly backwards. You can understand — and build — a neural network starting from a single neuron that does nothing more than multiply, add, and compare. This guide walks that path, with runnable Python at every step. No math degree required.
What a neural network actually is
A neural network is a stack of tiny decision-makers called neurons. A single neuron takes a few numbers as input, multiplies each by a weight (how much that input matters), adds them up along with a bias, and then decides whether to “fire” based on that total. That is the entire idea. Everything else — layers, activations, backpropagation — is built on top of this one move.
Step 1: A single neuron in 6 lines of Python
Here is a complete artificial neuron. No libraries, no magic:
def neuron(inputs, weights, bias):
total = bias
for x, w in zip(inputs, weights):
total += x * w
return 1 if total >= 0 else 0 # step activation
# weights/bias that compute logical AND
print(neuron([1, 1], [1, 1], -1.5)) # -> 1
print(neuron([1, 0], [1, 1], -1.5)) # -> 0That neuron behaves like an AND gate: it outputs 1 only when both inputs are 1. The intelligence lives entirely in the numbers [1, 1] and -1.5. Change them, and the neuron makes a different decision. Learning is just the computer finding those numbers itself.
Step 2: Teach the neuron to learn (the perceptron rule)
Picking weights by hand does not scale. The perceptron learning rule lets the neuron correct itself. Show it an example, let it guess, and nudge each weight in the direction that would have made it right:
data = [([0,0],0), ([0,1],0), ([1,0],0), ([1,1],1)] # AND
w, b, lr = [0.0, 0.0], 0.0, 0.1
def predict(x):
return 1 if b + sum(xi*wi for xi, wi in zip(x, w)) >= 0 else 0
for _ in range(10): # epochs
for x, target in data:
error = target - predict(x)
for i in range(len(w)):
w[i] += lr * error * x[i]
b += lr * error
print(w, b) # the neuron found its own weightsThe key line is w[i] += lr * error * x[i]. If the guess was already correct, error is zero and nothing changes — the neuron is self-correcting. The lr (learning rate) controls how big each nudge is. This exact idea scales all the way up to training giant models.
Step 3: Discover why one neuron isn't enough
Try to make that same neuron learn XOR (output 1 when the inputs differ) and it will fail, no matter how long you train it. This is the single most important lesson in deep learning. A single neuron can only draw one straight lineto separate 1s from 0s. AND and OR can be split by one line; XOR cannot — its two “1” answers sit on opposite corners. No straight line separates them.
The fix is the whole reason neural networks exist: stack neurons into layers. One layer carves the input space into pieces; the next layer combines those pieces. Two simple lines can do what one cannot. That middle layer is called a hidden layer.
Step 4: From a neuron to a real network
To train a multi-layer network you need backpropagation — a way to figure out how much each weight, even deep inside the network, contributed to the final error, so you know which direction to nudge it. It sounds intimidating, but it is just the chain rule from calculus applied carefully, and you can compute it by hand on a two-neuron network before ever touching a framework. Once you have that, a working network in numpy is a short step away:
import numpy as np
def sigmoid(z): return 1 / (1 + np.exp(-z))
# 2 inputs -> 2 hidden -> 1 output, trained with backprop,
# is enough to finally solve XOR. (Full walkthrough in the course.)The right order to learn this
- One neuron: multiply, add, compare.
- The learning rule: let it find its own weights.
- The XOR wall: understand why layers are necessary.
- Backpropagation: how a layered network learns.
- A real numpy network: put it all together.
If you follow that order, machine learning stops feeling like a wall of symbols and starts feeling like programming — because that is what it is. The math becomes the explanation for something you have already built, not a barrier in front of it.
Do it interactively (free)
ML Bites is built around exactly this path. You write and run the Python above directly in your browser — no installs — and an AI tutor sits in every code cell to explain your errors, grade your exercises, and answer your questions in plain English. The first three lessons are free, no signup.
FAQ
Do I need to know calculus to build a neural network in Python?
No. You can build and train a working neuron with only basic Python (variables, loops, lists). Calculus helps you understand backpropagation later, but it is not the starting point.
Why can't a single neuron learn XOR?
A single neuron can only separate data with one straight line. XOR is not linearly separable — its positive cases sit on opposite corners — so it requires at least one hidden layer of neurons.
What is the simplest neural network I can build?
A single perceptron: inputs × weights, plus a bias, passed through a step function. In Python it is about six lines, and it can already compute logic gates like AND and OR.