3.3 Exercises#
Exercise 1: Conditionals#
Can you rewrite the code below by only using one if, one elif, and one else
statement to improve readability?
points = 12
if points > 15:
print("Points are more than 10.")
if points == 10:
print("Points are exactly 10.")
if points < 10:
print("Points are less than 10.")
if points > 10:
print("Points are more than 10.")
if points <= 5:
print("Points are less than 10.")
Exercise 2: Loops#
You are provided with a list of integers called numbers
. Your task is to:
Write a for loop which calculate the sum of all items in the list. Print the result.
Again count the sum of elements, but exclude the item at index 3.
Write a for loop to count how many numbers in the list are even. Print the count.
Hints:
You might find using a counting variable useful for all three exercises.
You can use the
enumerate()
function and the!=
operator if you work with indices in a loop.The modulo
%
operator will give you the remainder of a division. So the expressionnumber % 2 == 0
will beTrue
if number is even (there is no remainder), or else it will beFalse
(there is a remainder).
numbers = [10, 15, 8, 23, 42, 4, 16, 9, 7, 30]
Exercise 1: Reaction Time Data Analysis#
Given a list of reaction times (in milliseconds) from a cognitive task, calculate the average reaction time, and count how many trials had reaction times faster than a threshold (e.g. 300 ms).
reaction_times = [250, 320, 300, 450, 280, 310, 290, 510, 305, 330]
threshold = 300
Exercise 2: Counting Spikes in Neural Data#
Assume you have a list of voltage readings from a neuron over time. A spike occurs whenever the voltage is above a threshold (e.g., 50 mV). Write code to count the number of spikes.
voltage_readings = [45, 52, 49, 55, 60, 48, 47, 53, 49, 50, 65]
threshold = 50
Exercise 3: Emotional Word Count#
You are provided with a list of words from a psychological text analysis of participants’ speech. Count how many words are classified as “positive” or “negative” based on pre-defined lists.
Hint: you can use in
to check if an item belongs to a list, e.g. if item in list
…
words = ["peaceful", "happy", "sad", "joy", "angry", "calm", "excited", "joyful", "miserable"]
positive_words = ["happy", "thankful", "joy", "calm", "excited", "joyful", "peaceful", "content"]
negative_words = ["sad", "depressed", "angry", "miserable", "anxious"]
Exercise 4: Stimulus Response Check#
Suppose a participant’s responses to a series of stimuli are recorded as “yes” or “no”. Write a loop to check if there are any cases where the participant said “yes” three times in a row. If so, print a message that they reached a “high response” period.
Hint: You can use break
to exit the for loop if you found a high response period.
responses = ["no", "yes", "yes", "yes", "no", "no", "yes", "no"]
Exercise 5: Simulating a Stroop Task#
In a simplified Stroop task, participants must say the color of the text, not the word. You have a list of color-word pairs; print only the color for each item.
Hint: If you iterate over pairs of values, you need to make sure to then handle the values within the pair correcly (e.g. by putting them into individual variables with tuple unpacking).
color_word_pairs = [("red", "BLUE"), ("green", "RED"), ("blue", "GREEN"), ("yellow", "BLUE")]
Exercise 6: Neuron Firing Rate Simulation#
Given a list of neuron firing intervals (in milliseconds) for a neuron over time, calculate the individual firing rates as well as the average firing rate. The firing rate of a neuron is the reciprocal of the firing interval in seconds (e.g. a firing interval of 100 ms would mean a firing ratr of \(\frac{1}{0.1s} = 10 Hz\)).
intervals = [100, 200, 150, 120, 180, 90] # in ms
Voluntary Exercise 1: More loops#
Please use a loop to iterate through the list of numbers. For each number, determine if it is prime and print whether it is prime or not.
numbers = [2, 3, 4, 5, 10, 11, 15, 17, 18, 23, 29, 30]
Voluntary Exercise 2: Experimental Data Normalization#
Given a list of participants’ scores on a memory test, normalize the scores to a range of 0 to 1 by dividing each score by the maximum score.
Hint: Create and fill a new list that contains the normalized scores.
scores = [45, 55, 80, 90, 60, 70]
Voluntary Exercise 3: Analyzing Sleep Patterns#
Each item in the sleep_stages list represents 15 minutes of sleep categorized into different stages: "Awake"
, "Light Sleep"
, "Deep Sleep"
, and "REM"
. The task is to analyze sleep patterns by calculating the total time spent in each stage, finding the longest continuous period of deep sleep, and determining if at least 20% of the sleep time was spent in REM.
Calculate the total time spent in each sleep stage.
Identify the longest continuous period spent in “Deep Sleep.”
Determine if the participant achieved at least 20% of the total sleep time in “REM” sleep.
Hint: You could, for example, create a dictionary to hold the counts of each state before calculating the total time spent in each state.
# Simulated sleep stages data for 8 hours (32 intervals of 15 minutes each)
sleep_stages = [
"Awake", "Light Sleep", "Light Sleep", "Deep Sleep", "Deep Sleep", "Deep Sleep",
"REM", "REM", "Light Sleep", "Light Sleep", "Awake", "Deep Sleep",
"Light Sleep", "Deep Sleep", "REM", "REM", "Deep Sleep", "Light Sleep",
"Awake", "Light Sleep", "Deep Sleep", "Deep Sleep", "REM", "REM",
"Light Sleep", "Light Sleep", "REM", "Light Sleep", "Awake", "Light Sleep",
"Awake", "Awake"
]