Problem Solving With Python: Program 2

The Problem Statement:

Program 1 showed us how to iterate over a range of numbers with the use of a for loop and the range function. Suppose instead of a range of number we have a bunch of numbers that are contained in data structure called a list. Given a list of numbers, how do we iterate over it using a for loop?

The task, just like program 1, will be to write a Python program that displays a list of numbers from 4 to 9. Next to each number, the program will also display its square. But unlike program one where we used the range function to generate the range of numbers along with their square, we instead will use a list of number and iterate over it in three different ways.

The following is the expected approximate output of program 2. The output is repeated four times. Note that for the last example we will iterate over the number in reverse order:

    4 16
    5 25
    6 36
    7 49
    8 64
    9 81
    --END---
    4 16
    5 25
    6 36
    7 49
    8 64
    9 81
    --END---
    4 16
    5 25
    6 36
    7 49
    8 64
    9 81
    --END---
    9 81
    8 64
    7 49
    6 36
    5 25
    4 16
    --END---
    

The Solution:
    

def program_2():

    """LOOP 1"""
    for number in 4, 5, 6, 7, 8, 9:
        print(number, number * number)
    print("--END---")

    """LOOP 2"""
    for number in [4, 5, 6, 7, 8, 9]:
        print(number, number * number)
    print("--END---")

    """LOOP 3"""
    list_of_numbers = [4, 5, 6, 7, 8, 9]
    for number in list_of_numbers:
        print(number, number * number)
    print("--END---")

    """LOOP 4"""
    reverse_list_of_numbers = list_of_numbers[::-1]
    for number in reverse_list_of_numbers:
        print(number, number * number)
    print("--END---")


if __name__ == '__main__':
    program_2()



LOOP 1:

The range of values are from 4 to 9. The comma is used to separate the numbers so that the for ... in loop can distinguish between the unique numbers and consume them.

LOOP 2:

The list of values is explicitly defined by the usage of the opening and closing brackets. That is, the use of [ (opening bracket) and ] (closing bracket)

LOOP 3:

We can also assign the list of values to a variable called list_of_numbers and then use the variable in the for loop. The use of the variable will allow us to reuse the list of number in the subsequent, and last, for loop.

LOOP 4:

We reverse the list of numbers with the use of the so called slice operator. The slice operator is a shorthand notation that is used when we want it iterate over a list in a custom order. There are many ways to use the slice operator. We show only one example in this tutorial. The slice operator, [::-1], iterates over the list_of_number in a reverse order; returning a new list of numbers but in reverse order. This reverse list of numbers is captured by the reverse_list_of_numbers variables, which on the next line is used by the for loop. More on the slice operator in a future tutorial.

Page with links to the entire series can be found here

Are you having any issues running or understanding the program? Please, add a comment explaining where you are stuck or where the tutorial is not clear so we can improve it.

No comments:

Post a Comment