Understand the Python Code
We already know how a neuron calculates its value mathematically. Now let's see how to perform the same calculation using Python.
Our Simple Neuron
We will use the same example from the previous topic. Our neuron has two inputs, two weights, and one bias.
x1 = 2
x2 = 3
w1 = 0.5
w2 = 0.4
bias = 1
z = (x1 * w1) + (x2 * w2) + bias
print(z)
The output is:
3.2
Understand the Code Step by Step
Let's understand every important line instead of treating the code as something to memorize.
Step 1 — Define the Inputs
x1 = 2
x2 = 3
Here we create two input values.
In a real machine learning problem, these values could represent features from a dataset.
For example:
x1 = hours studied
x2 = attendance
We are using simple numbers here so we can clearly understand the neuron calculation.
Step 2 — Define the Weights
w1 = 0.5
w2 = 0.4
Each input has a corresponding weight.
So Python is storing the values that the neuron needs for its calculation.
Step 3 — Define the Bias
bias = 1
The bias is an additional value added after calculating the weighted inputs.
Step 4 — Calculate the Neuron Value
z = (x1 * w1) + (x2 * w2) + bias
This single line performs the main mathematical calculation.
Python replaces the variable names with their values.
z = (2 * 0.5) + (3 * 0.4) + 1
Now calculate each multiplication:
2 * 0.5 = 1.0
3 * 0.4 = 1.2
Then add them:
1.0 + 1.2 + 1 = 3.2
Step 5 — Print the Result
print(z)
The print() function displays the value
stored inside z.
3.2
So the complete Python program calculates the weighted sum plus bias and displays the result.
What Each Variable Means
Putting the Calculation Into a Function
Instead of writing the calculation every time, we can create a Python function.
def neuron_output(x1, x2, w1, w2, bias):
return (x1 * w1) + (x2 * w2) + bias
result = neuron_output(2, 3, 0.5, 0.4, 1)
print(result)
The output is:
3.2
How the Function Works
This line creates the function:
def neuron_output(x1, x2, w1, w2, bias):
The function expects five values:
Then this line performs the calculation:
return (x1 * w1) + (x2 * w2) + bias
Finally, we call the function:
result = neuron_output(2, 3, 0.5, 0.4, 1)
Python sends these values into the function:
x1 = 2
x2 = 3
w1 = 0.5
w2 = 0.4
bias = 1
The function calculates:
(2 × 0.5) + (3 × 0.4) + 1
= 3.2
Another Example
Let's change the values.
x1 = 5
x2 = 4
w1 = 0.2
w2 = 0.6
bias = 1
Calculate:
(5 × 0.2) + (4 × 0.6) + 1
= 1.0 + 2.4 + 1
= 4.4
Using the function:
result = neuron_output(5, 4, 0.2, 0.6, 1)
print(result)
Output:
4.4
This shows why using a function is useful. We can use different inputs, weights, and bias without rewriting the calculation.
Is This a Complete Neural Network?
No. This is only a very simple mathematical representation of one neuron.
A real neural network can contain many neurons, multiple layers, activation functions, and training algorithms.
The purpose of this example is to understand the calculation performed by an individual neuron before moving to larger neural networks.
Where Does the Activation Function Go?
Our current Python example calculates the value
z, but it does not yet apply an activation
function.
z = (x1 * w1) + (x2 * w2) + bias
Conceptually, the complete process is:
We will learn activation functions in the next lesson.
The Big Picture
x1 = 2
x2 = 3
w1 = 0.5
w2 = 0.4
bias = 1
z = (x1 * w1) + (x2 * w2) + bias
print(z)
The Python code is simply implementing the mathematics we already learned.
What You Should Remember
Python variables store the inputs, weights, and
bias. The calculation multiplies each input by
its weight, adds the results, adds the bias, and
stores the result in z.
Check Your Understanding
What does x1 represent?
The first input value.
What does w1 represent?
The weight associated with the first input.
What does this line do?
z = (x1 * w1) + (x2 * w2) + bias
calculates the weighted sum plus bias.
What does print(z) do?
It displays the calculated value.
Is z always the final neuron output?
Not necessarily. In a typical neural network,
z is passed through an activation function to
produce the neuron's output.