AI Concepts in Python

Neurons and Activation Functions in Python

An artificial neuron is a tiny function. It multiplies each input by a weight, adds a bias and passes the result through an activation function that decides how strongly it fires.

The concept in plain words

An artificial neuron is a tiny function. It multiplies each input by a weight, adds a bias and passes the result through an activation function that decides how strongly it fires.

Build an artificial neuron in plain Python: a weighted sum of inputs plus a bias, passed through sigmoid or ReLU.

Key ideas

  • Weights say how important each input is; the bias shifts the result up or down.
  • Without an activation, stacking neurons would still give only a straight-line (linear) model.
  • Sigmoid outputs a value between 0 and 1, so it suits probabilities.
  • ReLU is fast and is the usual choice for the hidden layers of modern networks.

When should you use it?

  • Understand what happens inside neural networks and AI libraries.
  • Learn the idea before you implement it in the matching challenges.
  • Prepare for machine learning courses, interviews and projects.

See it in action

Move the sliders to change the inputs, weights and bias. Watch the weighted sum z move along the sigmoid and ReLU curves.

Example code

Run code →
main.py
import math

# A neuron multiplies each input by a weight, adds them up and adds a bias.
def dot(a, b):
    return sum(x * y for x, y in zip(a, b))

def sigmoid(z):
    return 1 / (1 + math.exp(-z))   # squashes any number into (0, 1)

def relu(z):
    return max(0.0, z)              # keeps positives, turns negatives into 0

inputs = [0.5, -1.0, 2.0]
weights = [0.8, 0.2, -0.5]
bias = 0.1

z = dot(inputs, weights) + bias      # weighted sum
print("weighted sum z =", round(z, 3))
print("sigmoid(z) =", round(sigmoid(z), 3))
print("relu(z) =", relu(z))

# The activation decides how strongly the neuron "fires".
for value in [-2, 0, 2]:
    print(f"z={value:>2}  sigmoid={sigmoid(value):.3f}  relu={relu(value)}")

Expected output

weighted sum z = -0.7
sigmoid(z) = 0.332
relu(z) = 0.0
z=-2  sigmoid=0.119  relu=0.0
z= 0  sigmoid=0.500  relu=0.0
z= 2  sigmoid=0.881  relu=2

How it works

dot() computes the weighted sum z. Sigmoid squashes z into a value between 0 and 1, while ReLU keeps positive values and turns negatives into 0. The loop shows how each activation responds to a negative, zero and positive input.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the inputs or weights and predict the output before running.
  2. Print the intermediate values to follow each step of the calculation.
  3. Solve the linked challenges below to implement the idea yourself.
Run in Python IDE →

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges