+10 XP

If Statements

An if statement makes a decision. It runs a block of code only when a condition is True — and skips it when the condition is False.

Here is the structure:

python
if CONDITION:
    # code that runs when condition is True
else:
    # code that runs when condition is False

The colon : after the condition is required. The indented lines below it are what gets run.

A simple example — checking if a number is positive:

python
x = 10

if x > 0:
    print('positive')
else:
    print('not positive')

# prints: positive

x > 0 is the condition. Python checks if it's True or False, then runs the matching block.

Now the neuroscience part.

Neurons have a membrane voltage (think of it as electrical pressure inside the cell). When that voltage rises above a threshold — around −55 mV — the neuron fires an action potential, also called a spike.

Below the threshold: silence. Above it: fire.

python
V = -52          # current membrane voltage (mV)
threshold = -55  # firing threshold (mV)

if V > threshold:
    print('Neuron fires!')
else:
    print('Neuron is silent')

# -52 > -55 is True (less negative = higher voltage)
# prints: Neuron fires!

−52 is higher than −55 on the number line, so the condition is True and the neuron fires.

What is .append()?

When a neuron fires, you want to remember when it happened. .append() adds a new item to the end of a list — like writing a new entry in a diary.

spike_times is a list that collects every moment the neuron fires.

python
spike_times = []  # start with empty list

t = 42  # current time in ms
spike_times.append(t)  # add 42 to the list

t = 67
spike_times.append(t)  # add 67 to the list

print(spike_times)  # [42, 67]

.append(t) means: add the current time t to the end of the spike_times list.

Putting it all together — this is almost exactly how NMA simulates a neuron:

python
spike_times = []

for t in range(1000):                      # loop over each ms
    # Voltage changes based on input stimulus over time
    if 200 <= t < 700:                      # stimulus arrives from t=200 to t=700
        V = -50                              # voltage rises when stimulus is present
    else:
        V = -65                              # resting voltage
    
    threshold = -55
    
    if V > threshold:                       # did the neuron fire?
        spike_times.append(t)                # yes — record the time

print(f'Total spikes: {len(spike_times)}')

Now V depends on t! When stimulus is present (t=200–700), voltage rises above threshold and the neuron fires. This is how real neurons work: they respond to input that changes over time.

The pattern loop + if + append is the skeleton of almost every neuron simulation you will see in NMA. Master this and the rest will feel familiar.