A Basic Neural Net w/ Python in 16 Lines of Code

Adith Raghav
3 min readFeb 5, 2021


The source code for this blog is posted on my website here:

FreeKode — Python & AI: Basic Neural Network Using Just the Random Module (freekode.centeltech.com)

What is a neural network? Everybody always talks about it. It isn’t complicated at all, however it might seem at first glance.

If you know a bit of Python, then you’re ready for this.

Let’s go!

What is a neural network?

A neural network is a computer system modelled on the human brain or the human nervous system.

Neural Networks are usually made of 3 layers (where each layer is a group of neurons, arranged that way for a specific task):

The Input Layer (which gets the input from the sense organs),

The Hidden Layer (processes the input), and

The Output Layer (which executes a command given by the hidden layer)

A neuron

A neuron is the structural and functional unit of a neural network. All neural networks, whether inside the brain or out in the computer, is made of neurons. A neuron can have any number of inputs: In our brain, a neuron gets the input from other neurons through nerve-like thin tubes called dendrites.

The cyton is the cell body of the neuron, where it processes the input.

The axon is a thick tube sticking out of the cyton which spreads into smaller and smaller tubules, which becomes the dendrites for other neurons.

Each input to the neuron has a weight property attached to it. The weights of each dendrite add up to a bias . You can think of a weight as the urgency of something. The bias is a kind of threshold. When all the dendrites’ weights add up to this threshold, the input gets processed in the cyton.

Let’s get started

First, we need to import the random module. As this is a Python standard module, there is no need to install it. This will be the only module we will import here.

from random import seed, random

Next, we will define a function called initialize_network() . It will take the parameters: n_input, n_hidden, and n_output, for the number of neurons in the input layer, the hidden layer, and the output layer, respectively.

def initialize_network(n_input, n_hidden, n_output):

Now we will create an empty list called network, to which we will append our input, hidden, and output layer.

network = []

Let’s create those layers now. We don’t need to create the input layer, so will only do the hidden and output layers.

Let’s create the hidden layer. We will create a dictionary in this list, to attach the weight to the input. The weights are going to be randomly generated.

hidden_layer = [{'weights': [random() for i in range(n_inputs+ 1)]} for i in range(n_hidden)]

Now, we are going to append this to our networks list.


Let’s create the output layer now.

output_layer = [{'weights': [random() for i in range(n_hidden + 1)]} for i in range(n_outputs)]

Let’s append this layer to the network list.


We’ve created our function now. Time to check it out!

Let’s generate a seed value using the random module’s seed function we imported at the top.


Now, we created our function, but we haven’t called it yet. Let’s do that.

network = initialize_network(10, 5, 10)

We invoked our function and everything, but it still doesn’t output anything.

for layer in network:

And that’s it! We created a neural network with Python in just 16 lines of code, no TensorFlow, no SciKit Learn, or anything like that!

Until next time then!



Adith Raghav

I am an 11 year old boy and the founder of www.centeltech.com — teaching kids about HTML and CSS. I also have a website called https://freekode.centeltech.com.