|
Repetition StatementsSelection Statements Table Of Contents Defining Functions Page Contents (hide) Python provides two looping constructs — the The While LoopThe theSum = 0 i = 1 while i <= 100: theSum = theSum + i i = i + 1 print "The sum = ", theSum which would be equivalent to the following in Java int theSum = 0; int i = 0; while( i <= 100 ) { theSum = theSum + i; i = i + 1; } System.out.println( "The sum = " + theSum ); The value = 0 while value < 0 : value = int( raw_input( "Enter a postive integer: " ) ) Python, like Java, provides the For LoopsThe for <loop-var> in <object> : <statement-block> The body of the loop (statement-block) is executed once for each item in the ordered sequence. Before beginning each iteration, the current item of the ordered sequence is assigned to the loop variable (loop-var). Single Step IterationsThe Python // Java count-controlled loop. for( int i = 1; i <= 10; i++ ) System.out.println( i ); which prints the range of integers from 1 to 10, one per line. The equivalent in Python would look as follows for i in xrange( 1, 11 ) : print i The Variable Step IncrementsInstead of stepping or counting by 1, a third argument can be provided to the for i in xrange( 0, 51, 5 ) : print i Here, the loop prints the multiples of 5 between 0 and 50 including both 0 and 50. To construct a loop which decrements through the indices as illustrated in the following Java loop // Java loop for( int i = 10; i > 0; i-- ) System.out.println( i ); use arguments for the for i in xrange( 10, 0, -1 ) : print i Iterating From ZeroIncrementing loops which start with an index of 0 are very common. Therefore, the for i in xrange( 20 ) print i the loop prints the integer values from 0 through 19 one per line. Here, the argument to the Example ProgramThe following program illustrates the user of a loop to extract data from the user and compute an average value. Program: avgvalue.py File: avgvalue.py
# avgvalue.py # Reads a group of positive integer values from the user, # one at a time, until a negative value is entered. The average # value is then computed and displayed. # Initlize the counting variables. total = 0 count = 0 # Extract the values. value = int( raw_input( "Enter an integer value (< 0 to quit): " ) ) while value >= 0 : total = total + value count = count + 1 value = int( raw_input( "Enter an integer value (< 0 to quit): " ) ) # Compute the average. avg = float( total ) / count # Print the results. print "The average of the", count, "values you entered is", avg Processing StringsThe Program: countvowels.py File: countvowels.py
# countvowels.py # This program iterates through a string and counts the number of # vowels it contains. # Extract a string from the user. text = raw_input( "Enter a string to be evaluated: " ) # Iterate through the string. numVowels = 0 for ch in text : if ch in "aeiou" : numVowels = numVowels + 1 # Print the results. print "There are " + str( numVowels ) + " vowels in the string." You will notice that the keyword | |
© 2006 - 2008: Rance Necaise - Page last
modified on July 31, 2008, at 11:16 AM
|