Python loops and lists
We should now be able to create some programs that are significantly more interesting thanks to Python Loops. If you’ve been following along, you should be aware that you can now use if-statements and boolean expressions to combine all the other things you’ve learned to have your programs do intelligent actions.
Programs must, however, perform repetitive tasks quickly. In this exercise, we’ll generate and output various lists using a for loop. You’ll begin to identify them as you complete the practice. I won’t reveal it to you now. You must resolve the issue.
You need a means to save the output of loops somewhere before you can use a for-Python loop. A list is the ideal tool for accomplishing this. A list is precisely what it sounds like—a collection of items arranged according to priority. The only difficult part is learning a new syntax. To start, this is how to create a list:
hairs = ['brown', 'blond', 'red'] eyes = ['brown', 'blue', 'green'] weights = [1, 2, 3, 4]
The [(left bracket, which “opens” the list, is what you use to begin the list. Then, just as with function parameters, add each item you wish to the list, separating each one with a comma. The list is concluded by adding a] (right bracket) at the end. Then, Python assigns the variable with the contents of this list.
Python Loops
We now will build some lists using some loops and print them out:
the_count = [1, 2, 3, 4, 5] fruits = ['apples', 'oranges', 'pears', 'apricots'] change = [1, 'pennies', 2, 'dimes', 3, 'quarters'] # this first kind of for- loop goes through a list for number in the_count: print "This is count %d" % number # same as above for fruit in fruits: print "A fruit of type: %s" % fruit # also we can go through mixed lists too # notice we have to use %r since we don't know what's in it for i in change: print "I got %r" % i # we can also build lists, first start with an empty one elements = [] # then use the range function to do 0 to 5 counts for i in range(0, 6): print "Adding %d to the list." % i # append is a function that lists understand elements.append(i) # now we can print them out too for i in elements: print "Element was: %d" % i
While Loop
Here’s a new loop that will completely blow your mind: the while-loop. As long as a boolean expression is True, a while-loop will continue to run the code block underneath it. Have you been following the terminology, I take it? That if we write a line and end it with a : (colon), then that tells Python to start a new block of code? Then we indent and that’s the new code. This is all about structuring your programs so that Python knows what you mean. If you still don’t understand that concept, go back and continue working with if statements, functions, and the for loop until you do.
As we did when we burned boolean expressions into your brain, we’ll later have some activities that will educate your brain to read these structures. Back to while-loops. Simply put, they conduct a test similar to an if-statement, but instead of running the code block only once, they loop back up to the while at the top. Until the phrase is false, it continues in this manner.
The issue with while-loops is that they occasionally do not stop. This is excellent if your goal is to simply keep looping indefinitely. If not, you almost always want your loops to come to a conclusion.
To avoid these problems, there’s some rules to follow:
- Make sure that you use while- loops sparingly. Usually a for- loop is better.
- Review your while statements and make sure that the thing you are testing will become False at some point.
- When in doubt, print out your test variable at the top and bottom of the while- loop to see what it’s doing.
In this exercise, you will learn the while- loop by doing the above three things:
i = 0 numbers = [] while i < 6: print "At the top i is %d" % i numbers.append(i) i = i + 1 print "Numbers now: ", numbers print "At the bottom i is %d" % i print "The numbers: " for num in numbers: print num