LOOPS IN PYTHON

Table of Contents

  • For Loops
  • While Loops

  • Estimated Time Needed: 15 min

    For Loops

    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:

    
    
    In [1]:
    range(3)
    
    
    
    
    Out[1]:
    range(0, 3)

    :Example of range function.

    </h4>

    The 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:

    
    
    In [2]:
    dates = [1982,1980,1973]
    N=len(dates)
    
    for i in range(N):
         
        print(dates[i])
    
    
    
    
    1982
    1980
    1973
    

    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:

    Example of printing out the elements of a list.

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

    
    
    In [3]:
    for i in range(0,8):
        print(i)
    
    
    
    
    0
    1
    2
    3
    4
    5
    6
    7
    

    Write a for loop the prints out all the element between -5 and 5 using the range function.

    
    
    In [5]:
    for i in range(-5, 6):
        print(i)
    
    
    
    
    -5
    -4
    -3
    -2
    -1
    0
    1
    2
    3
    4
    5
    
    ``` for i in range(-5,6): print(i) ```

    In Python we can directly access the elements in the list as follows:

    
    
    In [6]:
    for year in dates:  
        print(year)
    
    
    
    
    1982
    1980
    1973
    

    For each iteration, the value of the variable years behaves like the value of dates[i] in the first example:

    Example of a for loop

    </h4>

    Genres=[ 'rock', 'R&B', 'Soundtrack' 'R&B', 'soul', 'pop'] Make sure you follow Python conventions.

    
    
    In [7]:
    Genres=[ 'rock', 'R&B', 'Soundtrack' 'R&B', 'soul', 'pop']
    for genre in Genres:
        print(genre)
    
    
    
    
    rock
    R&B
    SoundtrackR&B
    soul
    pop
    
    ``` Genres=[ 'rock', 'R&B', 'Soundtrack' 'R&B', 'soul', 'pop'] for Genre in Genres: print(Genre) ```

    We can change the elements in a list:

    
    
    In [8]:
    squares=['red','yellow','green','purple','blue ']
    
    for i in range(0,5):
        print("Before square ",i, 'is',  squares[i])
        
        squares[i]='wight'
        
        print("After square ",i, 'is',  squares[i])
    
    
    
    
    Before square  0 is red
    After square  0 is wight
    Before square  1 is yellow
    After square  1 is wight
    Before square  2 is green
    After square  2 is wight
    Before square  3 is purple
    After square  3 is wight
    Before square  4 is blue 
    After square  4 is wight
    

    Write a for loop that prints out the following list: squares=['red','yellow','green','purple','blue ']:

    
    
    In [9]:
    squares=['red','yellow','green','purple','blue ']
    for square in squares:
        print(square)
    
    
    
    
    red
    yellow
    green
    purple
    blue 
    
    ``` squares=['red','yellow','green','purple','blue '] for square in squares: print(square) ```

    We can access the index and the elements of a list as follows:

    
    
    In [10]:
    squares=['red','yellow','green','purple','blue ']
    
    for i,square in enumerate(squares):
        print(i,square)
    
    
    
    
    0 red
    1 yellow
    2 green
    3 purple
    4 blue 
    

    While Loops

    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:

    
    
    In [11]:
    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")
    
    
    
    
    1982
    1980
    1973
    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 :

    An Example of indices as negative numbers

    </h4>

    Write a while loop to display the values of the Rating of an album playlist stored in the list “PlayListRatings”. If the score is less than 6, exit the loop. The list “PlayListRatings” is given by: PlayListRatings = [10,9.5,10, 8,7.5, 5,10, 10]:

    
    
    In [17]:
    PlayListRatings = [10,9.5,10,8,7.5,5,10,10]
    i = 0
    while(not (PlayListRatings[i] < 6)):
        print(PlayListRatings[i])
        i += 1
    
    
    
    
    10
    9.5
    10
    8
    7.5
    
    ``` PlayListRatings = [10,9.5,10, 8,7.5, 5,10, 10] i = 0; Rating = PlayListRatings[i] while(Rating >= 6): print(Rating) i = i + 1 Rating = PlayListRatings[i] ```

    Write a while loop to copy the strings 'orange' of the list 'squares' to the list 'new_squares'. Stop and exit the loop if the value on the list is not 'orange':

    
    
    In [18]:
    squares=['orange','orange','purple','blue ','orange']
    new_squares=[];
    for square in squares:
        if square != 'orange':
            break
        else:
            new_squares.append(square)
    print(new_squares)
    
    
    
    
    ['orange', 'orange']
    
    ``` squares=['orange','orange','purple','blue ','orange'] new_squares=[]; i=0 while(squares[i]=='orange'): new_squares.append(squares[i]) i=i+1 ```

    About the Authors:

    Joseph Santarcangelo has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.

    Copyright © 2017 cognitiveclass.ai. This notebook and its source code are released under the terms of the MIT License.