Loops#
Exercise 1: Fix the error#
weights = [1243891290, 1234129401, 14910395130, 1340981394]
# your solution here
for index in range(10):
item = weights[index]
print(item)
1243891290
1234129401
14910395130
1340981394
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[11], line 5
3 # your solution here
4 for index in range(10):
----> 5 item = weights[index]
6 print(item)
IndexError: list index out of range
Exercise 2: Fix the error#
Below code only prints one list element. Which is it? Can you fix the code so that each list element is printed?
weights = [1243891290, 1234129401, 14910395130, 1340981394, 1231032912, 90000000]
# your solution here
for idx in range(len(weights)):
item = weights[idx]
print(item)
1243891290
1234129401
14910395130
1340981394
1231032912
90000000
Exercise 3: Printing using a for loop#
Use a for loop to each list element in a separate line:
ages = [12, 43, 89, 64, 52]
# your solution here
Exercise 4: Convert units#
We have a list of weights in grams. Use a for loop to convert the weights to tons and print them one by one.
grams = [1243891290, 1234129401, 14910395130, 1340981394]
# your solution here
Exercise 5: Generating ranges with a specific content#
Create a range statement with
2 elements
10 elements
These elements: [0, 1, 2, 3, 4, 5, 6]
Elements starting at 3 and ending with (including 6): [3, 4, 5, 6]
Every 3rd element, up to 10: [0, 3, 6, 9]
All EVEN numbers (including zero) up to and including 8: [0, 2, 4, 6, 8]
All ODD numbers up to 8: [1, 3, 5, 7]
Test your result by printing the contents of the range-object. You can do so by first casting it to a list, and then printing it.
# your solution here
r = range(1,8, 2)
print(list(r))
[1, 3, 5, 7]
Exercise 6: Ranges and loops#
The following code is supposed to generate a list that only contains the items at even indices (indices [0, 2, 4, …]) from the list all_values
.
Does it produce the correct result? If not, fix it!
all_values = [34, 5, 11, 83, 432, 3, 50, 780]
even_values = []
for index in range(0, len(all_values), 2):
item = all_values[index]
even_values.append(item)
print(even_values)
[5, 83, 3, 780]
Exercise 7: Print every other element of the list, starting with the second#
Use a for loop and range.
weights = [567803, 1243891290, 1234129401, 14910395130, 1340981394, 1231032912, 90000000]
# your solution here:
# the code should print the following numbers [1243891290, 14910395130, 1231032912]
for index in range(1, len(weights), 2):
print(weights[index])
1243891290
14910395130
1231032912
Bonus: Now achieve the same result, but use slice indexing instead:
# your solution here:
for weight in weights[1::2]:
print(weight)
1243891290
14910395130
1231032912
Exercise 8: Convert units and append the results to a new list#
We have a list of weights in grams - create a new list, called tons
that contains the weights in tons.
grams = [1243891290, 1234129401, 14910395130, 1340981394]
# your solution here
Exercise 9: Filtering a list (1)#
Print all list elements greater than 10:
a = [34,5, 432, 3, 780]
# your solution here
for idx in range(len(a)):
if a[idx] > 10:
print(a[idx])
34
432
780
Exercise 10: Filtering a list (2)#
Create a new list that contains all elements greater than 10:
a = [34, 5, 432, 3, 780]
# your solution here
greater = []
for num in a:
if num > 10:
greater.append(num)
print(greater)
[34, 432, 780]
Exercise 11: Filtering a list (3)#
Create a new list that contains all elements greater than 10 and less than 100:
a = [34, 5, 11, 83, 432, 3, 50, 780]
# your solution here
greater = []
for num in a:
if num > 10 and num < 100:
greater.append(num)
print(greater)
[34, 11, 83, 50]
Exercise 12: Slice indexing#
The following code is supposed to generate a list that only contains the items at even indices (indices [0, 2, 4, …]) from the list all_values
.
Does it produce the correct result? If not, fix it!
all_values = [34, 5, 11, 83, 432, 3, 50, 780]
even_values = []
for index in range(0, len(all_values), 2):
item = all_values[index]
even_values.append(item)
print(even_values)
[34, 11, 432, 50]
Exercise 13: Updating a variable#
The following code is supposed to compute the sum of the values in the list all_values
.
Does it produce the correct result? If not, fix it!
all_values = list(range(10))
print(all_values)
# fix this code:
sum_of_values = 0
for value in all_values:
sum_of_values += value
sum_of_values
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
45
Exercise 14: Write a spike detector#
Now write a spike detector:
Iterate over the voltage values in
voltages
using a for loop. You will need a range object.Compare each voltage value to the
spike_threshold
,Append the indices of voltage values that exceed
spike_threshold
to the listspike_indices
Before you start coding: What are the expected results? How many spikes does voltages
contain? At which indices?
voltages = [1, 1, 10, 1, 1, 10, 1]
spike_threshold = 5
spike_indices = []
# your solution here
spike_indices = []
for idx in range(len(voltages)):
BIG_V = voltages[idx]
if BIG_V > spike_threshold:
spike_indices.append(idx)
print(spike_indices)
[2, 5]