2.12. while Loop
The standard
while
conditional loop statement is similar to the
if
. Again, as with every code sub-block, indentation (and dedentation) are used to
while expression: while_suite The statement while_suite is executed continuously in a loop until the expression becomes zero or false; execution then continues on the first succeeding statement. Like if statements, parentheses are not required with Python while statements.
>>> counter = 0 >>> while counter < 3: ... print 'loop #%d' % (counter) ... counter += 1 loop #0 loop #1 loop #2
|
2.13. for Loop and the range() Built-in Function
The
for
loop in Python is more like a
foreach
iterative-type loop in a shell scripting language than a traditional
for
conditional loop that works like a counter. Python's
for
takes an
>>> print 'I like to use the Internet for:' I like to use the Internet for: >>> for item in ['e-mail', 'net-surfing', 'homework', 'chat']: ... print item ... e-mail net-surfing homework chat Our output in the previous example may look more presentable if we display the items on the same line rather than on separate lines. print statements by default automatically add a NEWLINE character at the end of every line. This can be suppressed by terminating the print statement with a comma ( , ).
print 'I like to use the Internet for:' for item in ['e-mail', 'net-surfing', 'homework', 'chat']: print item, print
The code required further modification to include an additional
print
statement with no arguments to flush our line of output with a terminating NEWLINE;
I like to use the Internet for: e-mail net-surfing homework chat Elements in print statements separated by commas will automatically include a delimiting space between them as they are displayed. Providing a string format gives the programmer the most control because it dictates the exact output layout, without having to worry about the spaces generated by commas. It also allows all the data to be grouped together in one placethe tuple or dictionary on the right-hand side of the format operator.
>>> who = 'knights' >>> what = 'Ni!' >>> print 'We are the', who, 'who say', what, what, what, what We are the knights who say Ni! Ni! Ni! Ni! >>> print 'We are the %s who say %s' % \ ... (who, ((what + ' ') * 4)) We are the knights who say Ni! Ni! Ni! Ni! Using the string format operator also allows us to do some quick string manipulation before the output, as you can see in the previous example.
We conclude our introduction to
>>> for eachNum in [0, 1, 2]: ... print eachNum ... 0 1 2
Within our loop,
eachNum
contains the integer value that we are displaying and can use it in any numerical calculation we wish. Because our range of numbers may
>>> for eachNum in range(3): ... print eachNum ... 0 1 2 For strings, it is easy to iterate over each character:
>>> foo = 'abc' >>> for c in foo: ... print c ... a b c The range() function has been often seen with len() for indexing into a string. Here, we can display both elements and their corresponding index value:
>>> foo = 'abc' >>> for i in range(len(foo)): ... print foo[i], '(%d)' % i ... a (0) b (1) c (2) However, these loops were seen as restrictiveyou either index by each element or by its index, but never both. This led to the enumerate() function (introduced in Python 2.3) that does give both:
>>> for i, ch in enumerate(foo): ... print ch, '(%d)' % i ... a (0) b (1) c (2) |