Differences between revisions 1 and 9 (spanning 8 versions)
Revision 1 as of 2010-12-21 23:23:21
Size: 1250
Editor: host86-154-237-157
Comment: Added WhileLoop to compliment ForLoop as requested.
Revision 9 as of 2017-04-10 16:43:34
Size: 2297
Editor: SteveHolden
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
 = While loops =
 == Usage in Python ==
= While loops =
== Usage in Python ==
Line 6: Line 5:
''While'' loops, like the ForLoop, are used for repeating sections of code - but unlike a ''for'' loop, the ''while'' loop will not run ''n'' times, but until a defined condition is met. As the ''for'' loop in Python is so powerful, ''while'' is rarely used, except in cases where a user's input is required, for example - ''While'' loops, like the ForLoop, are used for repeating sections of code - but unlike a ''for'' loop, the ''while'' loop will not run ''n'' times, but until a defined condition is no longer met. If the condition is initially false, the loop body will not be executed at all.
Line 8: Line 7:
{{{#!python numbers=disable
n=raw_input('Please enter\'hello\':')
while n.strip()!='hello':
    n=raw_input('Please enter\'hello\':')
As the ''for'' loop in Python is so powerful, ''while'' is rarely used, except in cases where a user's input is required*, for example:

{{{
n = raw_input("Please enter 'hello':")
while n.strip() != 'hello':
    n = raw_input("Please enter 'hello':")
Line 13: Line 14:
However, the problem with the above code is that it's wasteful. In fact, what you will see a lot of in Python is the following:
Line 14: Line 16:
However, the problem with the above code is that it's wasteful. In fact, what you will see a lot of in Python is the following -

{{{#!python numbers=disable
{{{
Line 18: Line 18:
    n=raw_input('Please enter\'hello\':')
    if n.strip()=='hello':
    n = raw_input("Please enter 'hello':")
    if n.strip() == 'hello':
Line 22: Line 22:
As you can see, this compacts the whole thing into a piece of code managed entirely by the ''while'' loop. Having ''True'' as a condition ensures that the code runs until it's broken by n.strip() equalling 'hello'. Another version you may see of this type of loop uses ''1'' instead of ''True'' - this is exactly the same, though in Python 2.*, using while ''1'' is minutely faster, due to the ability to resign ''True'' to a different value. As you can see, this compacts the whole thing into a piece of code managed entirely by the ''while'' loop. Having ''True'' as a condition ensures that the code runs until it's broken by {{{n.strip()}}} equaling {{{'hello'}}}.

 * Another version you may see of this type of loop uses {{{while 1}}} instead of {{{while True}}}. In older Python versions ''True'' was not available, but nowadays is preferred for readability.
  * [[https://docs.python.org/3.0/whatsnew/2.3.html#optimizations|Starting with Py2.3]], the interpreter optimized {{{while 1}}} to just a single jump. Using ''1'' was minutely faster, since ''True'' was not a keyword and might have been given a different value, which the interpreter had to look up, as opposed to loading a constant. As a programmer, it is up to you which style to use - but always remember that readability is important, and that while speed is also important, readability trumps it except in cases where timings are significantly different.
  * [[https://docs.python.org/3.0/whatsnew/3.0.html#changed-syntax|Starting in Python 3]], {{{True}}}, {{{False}}}, and {{{None}}} are keywords, so using {{{while 1}}} no longer provides the tiny performance benefit used to justify it in earlier versions.
  * See also: http://stackoverflow.com/questions/3815359/while-1-vs-for-whiletrue-why-is-there-a-difference

* If this were Wikipedia, the above statement would be followed by "citation needed."

While loops

Usage in Python

  • When do I use them?

While loops, like the ForLoop, are used for repeating sections of code - but unlike a for loop, the while loop will not run n times, but until a defined condition is no longer met. If the condition is initially false, the loop body will not be executed at all.

As the for loop in Python is so powerful, while is rarely used, except in cases where a user's input is required*, for example:

n = raw_input("Please enter 'hello':")
while n.strip() != 'hello':
    n = raw_input("Please enter 'hello':")

However, the problem with the above code is that it's wasteful. In fact, what you will see a lot of in Python is the following:

while True:
    n = raw_input("Please enter 'hello':")
    if n.strip() == 'hello':
        break

As you can see, this compacts the whole thing into a piece of code managed entirely by the while loop. Having True as a condition ensures that the code runs until it's broken by n.strip() equaling 'hello'.

  • Another version you may see of this type of loop uses while 1 instead of while True. In older Python versions True was not available, but nowadays is preferred for readability.

    • Starting with Py2.3, the interpreter optimized while 1 to just a single jump. Using 1 was minutely faster, since True was not a keyword and might have been given a different value, which the interpreter had to look up, as opposed to loading a constant. As a programmer, it is up to you which style to use - but always remember that readability is important, and that while speed is also important, readability trumps it except in cases where timings are significantly different.

    • Starting in Python 3, True, False, and None are keywords, so using while 1 no longer provides the tiny performance benefit used to justify it in earlier versions.

    • See also: http://stackoverflow.com/questions/3815359/while-1-vs-for-whiletrue-why-is-there-a-difference

* If this were Wikipedia, the above statement would be followed by "citation needed."

WhileLoop (last edited 2017-04-10 16:43:34 by SteveHolden)

Unable to edit the page? See the FrontPage for instructions.