For Loops
A for-loop allows us to repeat code for a finite amount of time. In Python, we can use the for-loop to iterate over a list. The for-loop will go through each element of the list one by one, starting with the first and ending with the last.
blue_foods = ['dasheen', 'yam', 'cassava', 'sweet potato']
# The 'in' keyword is used a again
# Read this as "for NEW_TEMPORARY_VARIABLE in LIST_OF_ITEMS"
for blue_food in blue_foods:
print(blue_food)
# On the console you'll see
# dasheen
# yam
# cassava
# sweet potato
# Function that prints a list of numbers after adding 2 to them
def add_two(numbers):
for n in numbers:
print(n + 2)
add_two([1,2,3,4,10]) # 3,4,5,6,12
# Let's do a function that prints out the values of a list of lists
# In this case, we'll nest the for loops to print them out
def print_double_list(double_list):
for dl in double_list:
for dl_item in dl:
print(dl_item)
print_double_list([[1,2],['bob',10,34.0],['c']])
# While Python already has a built in sum function, we can do one too!
def my_sum_1(items):
total = 0
for item in items:
total += item
return total
my_sum_1([1,2,4,5]) # 12Better coding with errors
A good coder knows how to deal with errors. Let's take some bad input for the my_sum_1 function in the previous section:
You'd get something like this:
So we can see that Python is complaining that you're trying to add a string with an int. It's not the best error message, if we expect the list to have only numbers why would there be a string? In this case we knew what was in the list, what if we didn't?
Let's write a better sum function that checks to see if the item being added is a number. If it's not, raise an error to let the user know what's in the list.
Now the error message is a lot clearer:
Exercises
For-loops are best suited to finite objects - true or false?
Write a function
productthat uses a for-loop to multiply every item in a list passed as an argument.What happens if the empty list is given? Modify the function such that if an empty list is given, 1 is returned
What happens if the list has data that aren't numbers? Raise an error if a non-number is detected.
Write a function
my_maxwhich determines the highest number found in a list. Let's break it down, you will be constantly comparing the current number to last recorded highest number. Think about, the highest number can appear anywhere in the list, right? So what do you compare the first number to? When you create amax_numvariable, first set it to -100000000000.
Last updated