Learning Python 3 with the Linkbot/For Loops

And here is the new typing exercise for this chapter:

and the ever-present output:

1 2 3 4 5 6 7 8 9 10

The output looks awfully familiar but the program code looks different. The first line uses the  function. The  function uses two arguments like this. is the first number that is produced. is one larger than the last number. Note that this program could have been done in a shorter way:

The range function returns an iterable. This can be converted into a list with the  function. Here are some examples to show what happens with the  command:

>>> range(1, 10) range(1, 10) >>> list(range(1, 10)) [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list(range(-32, -20)) [-32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21] >>> list(range(5,21)) [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] >>> list(range(5)) [0, 1, 2, 3, 4] >>> list(range(21, 5)) []

The next line  uses the   control structure. A  control structure looks like. is gone through starting with the first element of the list and going to the last. As  goes through each element in a list it puts each into. That allows  to be used in each successive time the   loop is run through. Here is another example (you don't have to type this) to demonstrate:

The output is:

The current item is: life The current item is: 42 The current item is: the universe The current item is: 6 The current item is: and The current item is: 7 The current item is: everything

Notice how the  loop goes through and sets item to each element in the list. So, what is  good for? The first use is to go through all the elements of a list and do something with each of them. Here's a quick way to add up all the elements:

with the output simply being:

The sum is: 20

Or you could write a program to find out if there are any duplicates in a list like this program does:

and for good measure:

Duplicate of 7 found

Okay, so how does it work? Here is a special debugging version to help you understand (you don't need to type this in):

with the output being:

l = [4, 5, 7, 8, 9, 1, 0, 7, 10]       l: [4, 5, 7, 8, 9, 1, 0, 7, 10] l.sort               l: [0, 1, 4, 5, 7, 7, 8, 9, 10] prev = l[0]            prev: 0 del l[0]               l: [1, 4, 5, 7, 7, 8, 9, 10] if prev == item:       prev: 0         item: 1 prev = item            prev: 1         item: 1 if prev == item:       prev: 1         item: 4 prev = item            prev: 4         item: 4 if prev == item:       prev: 4         item: 5 prev = item            prev: 5         item: 5 if prev == item:       prev: 5         item: 7 prev = item            prev: 7         item: 7 Duplicate of 7 found if prev == item:       prev: 7         item: 7 prev = item            prev: 7         item: 7 if prev == item:       prev: 7         item: 8 prev = item            prev: 8         item: 8 if prev == item:       prev: 8         item: 9 prev = item            prev: 9         item: 9 if prev == item:       prev: 9         item: 10 prev = item            prev: 10        item: 10

The reason I put so many  statements in the code was so that you can see what is happening in each line. (By the way, if you can't figure out why a program is not working, try putting in lots of print statements in places where you want to know what is happening.) First the program starts with a boring old list. Next the program sorts the list. This is so that any duplicates get put next to each other. The program then initializes a (ious) variable. Next the first element of the list is deleted so that the first item is not incorrectly thought to be a duplicate. Next a  loop is gone into. Each item of the list is checked to see if it is the same as the previous. If it is a duplicate was found. The value of  is then changed so that the next time the   loop is run through   is the previous item to the current. Sure enough, the 7 is found to be a duplicate. (Notice how  is used to print a tab.)

The other way to use  loops is to do something a certain number of times. Here is some code to print out the first 9 numbers of the Fibonacci series:

with the surprising output:

1 1 2 3 5 8 13 21 34

Everything that can be done with  loops can also be done with   loops but   loops give an easy way to go through all the elements in a list or to do something a certain number of times.

A Linkbot Example
In this example the Linkbot will play changing tones a fixed number of times while the duration remains constant. The Linkbot can also change tone and duration a fixed number of times. Here we are using a for loop running with in a fixed range of values.

setBuzzerFrequency.py

When you run this example play around with the loop range and time values to create interesting sounds.