Jump to content

User:Milanandreew/Python 3 Programming/Loops

From Wikibooks, open books for an open world

While loops

[edit | edit source]

This is our first control structure. Ordinarily the computer starts with the first line and then goes down from there. Control structures change the order that statements are executed or decide if a certain statement will be run. As a side note, decision statements (e.g., if statements), which you learned about in the last chapter, also influence whether or not a certain statement will run. Here's the source for a program that uses the while control structure:

a = 0
while a < 5:
    a += 1 # Same as a = a + 1 
    print(a)

So what does the program do? First it sees the line a = 0 and sets the variable a to zero. Then it sees while a < 5: and so the computer checks to see if a < 5. The first time the computer sees this statement, a is zero, and zero is less than 5. In other words, while a is less than five, the computer will run the indented statements. Here is another example of the use of while:

a = 1
s = 0
print('Enter Numbers to add to the sum.')
print('Enter 0 to quit.')
while a != 0:
    print('Current Sum: ', s)
    a = input('Number? ') 
    a = float(a)
    s += a
print ('Total Sum = ',s)

Notice how print('Total Sum =',s) is only run at the end. The while statement only affects the lines that are tabbed in (a.k.a. indented). The != means does not equal so while a != 0: means until a is zero run the tabbed in statements that are afterwards. Now that we have while loops, it is possible to have programs that run forever. An easy way to do this is to write a program like this:

while 1 == 1:
    print ('Help, I\'m stuck in a loop.')

This program will output Help, I'm stuck in a loop. until the heat death of the universe or until you stop it. The way to stop it is to hit the Control (or Ctrl) button and `c' (the letter) at the same time. This will kill the program. (Note: sometimes you will have to hit enter after the Control C.)

Examples

[edit | edit source]
# This program calculates the Fibonacci sequence
a = 0
b = 1
count = 0
max_count = 20
while count < max_count:
    count = count + 1
    # we need to keep track of a since we change it
    old_a = a
    old_b = b
    a = old_b
    b = old_a + old_b
    # Notice that the , end=' ' at the end of a call to print keeps it
    # from switching to a new line
    print(old_a, end=' ')
# Waits until a password has been entered.  Use control-C to break out without
# the password.

# Note that this must not be the password so that the 
# while loop runs at least once.
password = 'foobar'

# note that != means not equal
while password != 'unicorn':
    password = input('Password: ')
print('Welcome in')

For loops

[edit | edit source]

The next type of loop in Python is the for loop. Unlike in most languages, for requires some __iterable__ object like a Set or List to work.

onetoten = range(1,11)
for count in onetoten:
    print(count)

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

for count in range(1,11):
    print(count)

Here are some examples to show what happens with the range function:

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(21, 5)) # []

Another way to use the range() function in a for loop is to supply only one argument:

for a in range(10):
    print(a)

The above code acts exactly the same as:

for a in range(0, 10):
    print(a)

with 0 implied as the starting point.

The code would cycle through the for loop 10 times as expected, but starting with 0 instead of 1.

The next line for count in onetoten: uses the for control structure. A for control structure looks like for variable in list:.  list is gone through starting with the first element of the list and going to the last. As for goes through each element in a list it puts each into variable. That allows variable to be used in each successive time the for loop is run through. Here is another example to demonstrate:

demolist = ['life', 42, 'the universe', 'so long and','and', 'thanks for all the fish','everything']
for item in demolist:
    print('The current item is: {}'.format(item))

Notice how the for loop goes through and sets item to each element in the list. (Notice how if you don't want print to go to the next line add a comma at the end of the statement (i.e. if you want to print something else on that line). ) So, what is for good for? The first use is to go through all the elements of a list and do something with each of them. Here a quick way to add up all the elements:

list = [2,4,6,8]
sum = 0
for num in list:
    sum = sum + num
print ('The sum is: {}'.format(sum))

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

list = [4, 5, 7, 8, 9, 1,0,7,10]
list.sort()
prev = list[0]
del list[0]
for item in list:
    if prev == item:
        print ('Duplicate of ',prev,' Found')
    prev = item

How does it work? Here is a special debugging version:

l = [4, 5, 7, 8, 9, 1,0,7,10]
print ('l = [4, 5, 7, 8, 9, 1,0,7,10]','\tl:',l)
l.sort()
print ('l.sort()','\tl:',l)
prev = l[0]
print ('prev = l[0]','\tprev:',prev)
del l[0]
print ('del l[0]','\tl:',l)
for item in l:
    if prev == item:
        print ('Duplicate of ',prev,' Found')
    print ('if prev == item:','\tprev:',prev,'\titem:',item)
    prev = item
    print ('prev = item','\t\tprev:',prev,'\titem:',item)

Note: The reason there are so many print calls is because print calls can show the value of each variable at different times, and help debug the program. First the program starts with the 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 prev(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 for 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 prev is then changed so that the next time the for loop is run through prev is the previous item to the current. Sure enough, the 7 is found to be a duplicate. The other way to use for 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:

a = 1
b = 1
for c in range(1,10):
    print(a)
    n = a + b
    a = b
    b = n
print ('')

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

The break statement

[edit | edit source]

A while-loop checks its termination condition before each entry into the loop body, and terminates if the condition has gone False. Thus, the loop body will normally iterate zero, one or more complete times.

A for-loop iterates its body once for each value returned from the iterator expression. Again, each iteration is normally of the complete loop body.

Sometimes you want to conditionally stop the loop in the middle of the loop body. An example situation might look like this:

  1. Obtain the next value to check
  2. Is there in fact another value to check? If not, exit the loop with failure.
  3. Is the value what I'm looking for? If so, exit the loop with success.
  4. Otherwise, go back to step 1.

As you can see, there are two possible exits from this loop. You can exit from the middle of a Python while- or for-loop with the break-statement. Here is one way to write such a loop:

found = False # initial assumption
for value in values_to_check():
    if is_what_im_looking_for(value):
        found = True
        break
    #end if
#end for
# ... found is True on success, False on failure

The trouble with this is the asymmetry between the two ways out of the loop: one through normal for-loop termination, the other through the break. As a stylistic matter, it would be more consistent to follow this principle:

If one exit from a loop is represented by a break, then all exits from the loop should be represented by breaks.

In particular, this means that the loop construct itself becomes a “loop-forever” loop; the only out of it is via a break statement.

We can do this by explicitly dealing with an iterator yielding the values to be checked. Perhaps the values_to_check() call above already yields an iterator; if not, it can be converted to one by wrapping it in a call to the iter() built-in function, and then using the next() built-in function to obtain successive values from this iterator. Then the loop becomes:

values = iter(values_to_check())
while True:
    value = next(values, None)
    if value is None:
        found = False
        break
    #end if
    if is_what_im_looking_for(value):
        found = True
        break
    #end if
#end while
# ... found is True on success, False on failure

This uses the special Python value None to indicate that the iterator has reached its end. This is a common Python convention. But if you need to use None as a valid item in your sequence of values, then it is easy enough to define some other unique value (e.g. a custom dummy class) to represent the end of the list.


Exercise

[edit | edit source]
  1. Write a programme that computes and prints the 1000th prime number.

Solution