Skip to main content
Engineering LibreTexts

10.1: Introduction

  • Page ID
    26358
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The ability to repeat a series of instructions with controlled variance is an extremely powerful computing tool. There are a few different ways to do this in Python, each with their own strengths.

    The first loop control structure is the while loop. At first glance this looks something like an if statement:

    while control expression:
         statement block
    

    The statement block, which might be many lines long, will be repeated as long as the control expression is true. Like the if statement, this block must be indented. The control expression is basically the same as those used in if statements: They tend to be simple variable tests. Further, it is important that one or more of the variables used in the control expression change during the looping process otherwise the loop will try to run forever. For example:

    x=1
    while x<10:
         print( x )
    

    The first time the while loop is entered, x is checked to see if it is less than 10, which it is. Consequently, the print statement is executed and the value 1 is printed. This is the last statement of the block so program flow loops back to the control expression. x is checked again to see if it’s less than 10. It is, so the print statement will be executed again. Because x never changes, this loop never stops. The result of this bit of code is the number 1 printed over and over and over, not stopping until the program is aborted (if this happens accidentally, a loop may be aborted by holding down the Ctrl key and pressing the C key). Consider this change:

    x=1
    while x<10:
         print( x )
         x=x+1
    

    Now x is incremented by 1 each time through the loop. On the tenth time starting the loop the test will fail because x will no longer be less than 10 (it will equal 10). The loop terminates and program flow picks up at the first line after the loop’s statement block. It is also possible to use the and and or operators in order to check for multiple conditions.

    A second technique to create a loop is through the for statement. The template looks similar to the while structure:

    for variable in value list:
         statement block
    

    value list can be a simple listing of values such as:

    for x in 1,3,25,17:
    

    This loop will execute four times. The first time through x will take on the value 1, the second time 3, the third time 25, and 17 for the final iteration. This is very convenient if a variable needs to cycle through a set of very specific values, particularly if those values are not in ascending or descending sequence or related in some obvious way (such as doubling each time). On the other hand, if the variable is changing by a specific amount each time, the range() function can be used to simplify the value list. The arguments to the range() function are variable. The simplest version just specifies the number of values, starting from zero. For example:

    for x in range(5):
         print( x )
    

    The code above will print the numbers 0, 1, 2, 3 and 4. Separate starting and ending values may also be used:

    for x in range(3,7):
         print( x )
    

    This will print out the values 3, 4, 5, and 6. Next, an increment value may be included:

    for x in range(3,11,2):
         print( x )
    

    This will print out the values 3, 5, 7 and 9. Note that in all cases the termination value is not included in the resulting sequence. Finally, negative values may be used to decrement rather than increment:

    for x in range(5,2,-1):
         print( x )
    

    This results in the values 5, 4 and 3 being printed.

    The choice of whether to use a while or a for, and whether or not to use range(), will depend on the requirements of the variable (or variables) being used. Indeed, it is possible to structure virtually any loop as a while loop but it is not always the most efficient way to do it. Here are some general rules: If there is a single variable that needs to cycle through a set of values which are based on a simple increment or decrement (i.e., adding or subtracting the same amount each time), a for with range() is the obvious choice. If the values are unique and cannot be created through a simple increment, decrement or other direct formula (for example, a series of measured resistor values), a for with value list is probably appropriate. Finally, if the variable changes in an obvious fashion which is not a simple increment (such as doubling) or if there are multiple conditions/variables that need to be examined, the while is most likely the way to go.


    This page titled 10.1: Introduction is shared under a not declared license and was authored, remixed, and/or curated by James M. Fiore.

    • Was this article helpful?