To iterate through a list, you first need to know the length of the list. You can find this number using the method len(list). When you use a for loop, the range: len(thisList) actually represents the index of the numbers in the list. Don’t forget, in lists, the first index is always 0! For-loops conveniently start their ranges at 0, and iterate len(thisList) times. So, by using this type of for loop, you iterate through every index of the list, and therefore every component of the list.

Look at this example problem:

Suppose that you are given a list, and you want to print all of the values in the list. Use a for loop with the range len(list). Your code should look something like this:

Look at this example problem:

Suppose that you are given a list, and you want to print all of the values in the list. Use a for loop with the range len(list). Your code should look something like this:

To go over the logic behind this in case you are confused:

- thisList[i] find the number that corresponds to the index i. So, if you want to print thisList[0], your code would find and print the number at index 0: in this case, 1.
- Another important thing to note is that the length of thisList is 10, meaning that there are 10 numbers in the list. However, the index ranges from 0 to 9-- there are 10 numbers, inclusive (including the first and last numbers). The for loop goes to the range - 1, and starts at 0. Therefore, the for-loop works perfectly! You will loop through all of the indexes in the list.

Say that you are trying to find the minimum number in a given list of 10 numbers. To solve this problem, you would need to iterate through all of the numbers in the list using for loops. As you iterate through the numbers, you should also have an if statement to check if that number is the minimum. (Have you seen a similar problem before? If not, go to the previous course set (For Loops) and look at “Using For Loops in Problems”!)

Let’s do another more complicated problem!

Pretend that you are really lazy this week, and don’t want to practice violin everyday. You decide to randomly practice every week. You want to flip a coin, and if the coin is heads, you will practice that day. If not, you won’t practice. Each day is independent of another. Instead of doing this with an actual coin, you decide to use Python to find the solution. Given the list [“mon”, “tues”, “wed”, “thur”, “fri”, “sat”, “sun”], how would you solve this problem?

First, let’s put the array into our code.

Pretend that you are really lazy this week, and don’t want to practice violin everyday. You decide to randomly practice every week. You want to flip a coin, and if the coin is heads, you will practice that day. If not, you won’t practice. Each day is independent of another. Instead of doing this with an actual coin, you decide to use Python to find the solution. Given the list [“mon”, “tues”, “wed”, “thur”, “fri”, “sat”, “sun”], how would you solve this problem?

First, let’s put the array into our code.

**An easier way to solve this problem is to modify each array to something like “mon: play!” or “mon: don’t play.”. At the end of the code, you would simply print the array to find out your practicing schedule for the week.**

As you can see, we need to loop through all of the String in the list twice. The first one is when we manipulate the Strings. Notice that we used the random module (called in Line 1 with import random) to find a random decimal number between 0 and 1. Then, we used an if-else statement to find if this “coin” was heads or tails. We changed the String in the list. The second for loop is just to print the list.

CHALLENGE: How would you modify this problems to randomly generate how many minutes you should practice on the days you practice? Your practice time should be between 30 and 90 minutes long, and your number should be a multiple of 10.

HINT: to convert an integer to a string, use the function str(integer)

HINT: Remember integer operators? (+, -, /, *, %) You may need to use these to solve this problem.

Try not to look at the solution until you solve the problem!

CHALLENGE: How would you modify this problems to randomly generate how many minutes you should practice on the days you practice? Your practice time should be between 30 and 90 minutes long, and your number should be a multiple of 10.

HINT: to convert an integer to a string, use the function str(integer)

HINT: Remember integer operators? (+, -, /, *, %) You may need to use these to solve this problem.

Try not to look at the solution until you solve the problem!