Conditionals#
What do we need to know to detect spikes using python?#

Present data in code (individual voltage values, manipulate them and store the results) - variables
Compare variables (voltage to threshold) - boolean operations
Perform different actions based on the value of a variable (only keep the position if the voltage exceeds the threshold) - if-else statements
Present and access data in a time series of voltage values - lists
Perform an action for each element in a sequence of values (inspect voltage values one-by-one) - for loops
Separate data and logic so we can use the same code for new recordings - functions
Apply this to multi data files
Plot and save the results
Comparing variables#
Until now, we know how to add, subtract, multiply, and divide variables.
However, to solve our spike detection problem, we need to compare values: We need to dermine whether the voltage exceeds the threshold!
This is done using comparison operators:
>greater than<less than==equal (not to be confused with=which assigns values to variables)>=greater or equal<=less or equal
a = 10
b = 10
result = a == b
result
True
The result of a comparison is not a number, but True or False.
We call the resulting variables boolean variables after George Boole.
Clicker question “comparisons”(Click me!)
Let’s define two variables: a = 10; b = 20. Which of these statements evaluate to True?
a == 10
a > b
b <= 20
b < 20
b = 3
Combining comparisons#
We can combine different comparisons with and or or, like so:
a > 10 and a < 20.
andreturns True only if both comparisons are Trueorreturns True if at least one of the two comparisons is True.
Most important simple variable types#
WIth the boolean type, we have now encountered all of the important so-called “simple” variable types (“simple” because they only hold a single value, not a collection of values):
integer numbers:
int- number without a decimal point (1, 2, 103241)floating point numbers:
float- number with a decimal point (3.141)string:
str: sequence of characters (“yes”, ‘no’)boolean variables:
bool- only two values:TrueorFalse(more about boolean variables later)
Perform different actions based on the value of a variable#
Yay - we can now figure out whether our voltage exceeds the threshold!
But that alone is pretty useless - we want to perform specific computations based on the outcome of this comparison, for instance, if the voltage value exceeds the threshold, then we want to print it’s time point.
This can be done using so-called “if-else” statements. The basic logic of these statements is this: if EXPRESSION is True, then we do one thing, else we do something else:
first_name = 'Anton'
if first_name == 'Anton':
print('The first name is "Anton".')
a = 5
else:
print('The first name is NOT "Anton".')
a = 3
print(a)
The first name is "Anton".
END
A couple of new things:
ifandelsekeywords.ifis followed by the expression whosTrue/Falsevalue determines what is done.:- marks the end of a special statement likeiforelse.Indentation marks code blocks that are executed after each statement, based on the
True/Falsevalue of the expression afterif.
What’s going on?#
We create a variable with name
first_nameand valueAnton.Python checks whether the value of the variable
first_nameequals (==)Anton(not to be confused with the=used for assigning values to variables).It does evalute to
True.
print(first_name)
print(first_name == 'Anton')
Anton
True
Since the condition for the
ifstatement isTrue, the indented code block afterifis executed. If the condition had evaluated toFalsethe indented code block afterelsewould have been executed.
How about this?
first_name = 'Benton'
if first_name == 'Anton':
print('First name starts with "A".')
else:
print('No!')
No!
We create a variable with name
first_nameand valueBenton.Python checks whether the value of the variable
first_nameequals (==)AntonIt does not - it evaluates to
False:
print(first_name)
print(first_name == 'Anton')
Benton
False
Since the condition for the
ifstatement isFalse, the code block afterifis skipped.Python moves to the
elseblock and executes the indented code block.
Multiple conditions can be checked by using elif statements (short for “else if”). Note, that the order of comparisons matters - python will only execute the first matching statement and skip the rest.
name = 'Benton'
if name == 'Anton':
print('Name starts with "A".')
elif name == 'Benton':
print('Name starts with "B".')
elif name == 'Canton':
print('Name starts with "C".')
else:
print('No!')
Name starts with "B".
Lastly, the else statement is optional. You can use if without the else:
today = "Monday"
if today == "Monday":
print("I hate Mondays!")
I hate Mondays!
Clicker question “if statements”(Click me!)
What will be printed?
size = 10
if size > 20:
print('too big!')
elif size < 20:
print('not too big!)
elif size == 10:
print('perfect')
else:
print('no result')
What do we need to know to detect spikes using python?#

Present data in code (individual voltage values, manipulate them and store the results) - variables
Compare variables (voltage to threshold) - boolean values
Perform different actions based on the value of a variable (only keep the position if the voltage exceeds the threshold) - if-else statements
Present and access data in a time series of voltage values - lists
Perform an action for each element in a sequence of values (inspect voltage values one-by-one) - for loops
Separate data and logic so we can use the same code for new recordings - functions
Apply this to multi data files
Plot and save the results
The core computation of the spike detection problem is solved!! (You will put the individual steps together during the exercise.)
Now we need to learn how to process the whole time series of voltage values!
We could manually read the numbers from the text file and enter different values of the voltage variable but this would be tedious.
So far all variables held single values - numbers or strings. How do we present sequences of numbers, like our time series of voltage values? And how do we work through a sequence and apply the spike detection to each individual value in the sequence?