Conditionals#
Exercise 1: Comparisons#
Let’s create some variables:
a = 10
b = 20
c = 10
Which of these expressions evaluate to True
? Predict the result without running the code, then run the code and try to understand the result.
a < b
a < c
a <= c
a == c
a + c >= b
a - c == b - 2 * c
We can also compare variables that hold text. Which one of these four statements evaluates to True
?
bird = 'duck'
bird == 'duck'
bird == 'goose'
bird == "duck"
Exercise 2: if statements#
Feeding up Keiko#
Mabel and Jolanda, our friends from marine biology, need our help, again!
They want to know whether their favorite whale, Keiko, is heavier then their second favorite whale, Moby.
The code below is not producing the correct result. What is wrong with the code? Can you fix it?
# whale 1
mass_keiko = 41928 # kg
# whale 2
mass_moby = 43872 # kg
# FIX THIS CODE
if mass_keiko > mass_moby:
print('Keiko is heavier than Moby. Stop giving her extra rations!')
else:
print('Keiko is so skinny! She needs extra rations!')
Keiko is so skinny! She needs extra rations!
Are the whales too heavy for shipping?#
Mabel and Jolanda keep pestering us about their whales!
They want to ship the whales, but the container can only carry them if their combined mass is below 85 tons.
Write a program that calculates the combined mass of the two whales and checks if it is below 85 tons.
If the combined mass is below 85 tons, the program should print “THE WHALES ARE READY FOR SHIPPING”. If the combined mass is above 85 tons, the program should print “THE WHALES ARE TOO HEAVY FOR SHIPPING”.
Modify the values of the individual masses to test your program. For instance, set the masses so that the combined mass is below 85 tons or above 85 tons.
# whale 1
mass_keiko = 41928 # kg
# whale 2
mass_moby = 43872 # kg
# Your solution here
c_mass = 85_000 # mass_keiko + mass_moby
if c_mass < 85000:
print('a')
elif c_mass > 85000:
print('b')
else:
print('c')
c
Now modify your program above to print “A WONDER HAPPENED - THE WHALES WEIGH EXACTLY 85 TONS!!!” if their combined mass is exactly 85 tons.
Exercise 3: Spike detection#
Back to spike detection: We now have all the ingredients for detecting spikes given the voltage value at an individual time point.
Write a program that prints “spike” if the value of the voltage
variable exceeds a threshold value of 0V.
The value of the voltage
variable is currently 10. Ensure that your program is working correctly by changing the value (to -10 or 0 or +10).
voltage = 10 # V
threshold = 0 # V
if voltage > threshold:
print ('spike')
else:
print ('nothing')
nothing
We noticed that neurons often die during our electrophysiology experiments. Let’s assume the membrane voltage of a dead cell is +100V.
Modify your code above to also print “Oh no, the cell died!!!” when the voltage is above +100V.
Test your program by changing the value of the voltage
variable.
# your code here