• Sometimes, you might want to repeat a given operation many times.


  • Repeated executions like this are performed by loops.


  • We will look at two types of loops, for loops and while loops.


  • Before we discuss loops lets discuss the range object. It is helpful to think of the range object as an ordered list.


  • For now, let's look at the simplest case.


  • If we would like to generate a sequence that contains three elements ordered from 0 to 2 we simply use the following command:


  • 
    
    
    # Use the range
    
    range(3)
    
     
    
  • Loops - Range


  • What is for loop? The for loop enables you to execute a code block multiple times.


  • For example, you would use this if you would like to print out every element in a list.


  • Let's try to use a for loop to print all the years presented in the list dates:


  • This can be done as follows:


  • 
    
    
    # For loop example
    
    dates = [1982,1980,1973]
    N = len(dates)
    
    for i in range(N):
        print(dates[i])     
    
     
    
  • The code in the indent is executed N times, each time the value of i is increased by 1 for every execution. The statement executed is to print out the value in the list at index i as shown here:


  • Loops - Range


  • In this example we can print out a sequence of numbers from 0 to 7:


  • 
    # Example of for loop
    
    for i in range(0, 8):
        print(i)
    
     
    
  • In Python we can directly access the elements in the list as follows:


  • 
    # Example of for loop, loop through list
    
    for year in dates:  
        print(year)   
    
     
    
  • For each iteration, the value of the variable years behaves like the value of dates[i] in the first example:


  • Loops - Range for lists


  • 
    
    
    We can change the elements in a list:
    
    # Use for loop to change the elements in list
    
    
    squares = ['red', 'yellow', 'green', 'purple', 'blue']
    
    for i in range(0, 5):
        print("Before square ", i, 'is',  squares[i])
        squares[i] = 'weight'
        print("After square ", i, 'is',  squares[i])
    
     
    
  • We can access the index and the elements of a list as follows:


  • 
    
    # Loop through the list and iterate on both index and element value
    
    squares=['red', 'yellow', 'green', 'purple', 'blue']
    
    for i, square in enumerate(squares):
        print(i, square)
     
    
  • What is while loop? As you can see, the for loop is used for a controlled flow of repetition.


  • However, what if we don't know when we want to stop the loop?


  • What if we want to keep executing a code block until a certain condition is met?


  • The while loop exists as a tool for repeated execution based on a condition. The code block will keep being executed until the given logical condition returns a False boolean value.


  • Let’s say we would like to iterate through list dates and stop at the year 1973, then print out the number of iterations. This can be done with the following block of code:


  • 
    
    
    
    # While Loop Example
    
    dates = [1982, 1980, 1973, 2000]
    
    i = 0
    year = 0
    
    while(year != 1973):
        year = dates[i]
        i = i + 1
        print(year)
    
    print("It took ", i ,"repetitions to get out of loop.")
    
     
    
  • It took 3 repetitions to get out of loop.


  • A while loop iterates merely until the condition in the argument is not met, as shown in the following figure:


  • Loops - while