Repetition

Repetition is one of the most important concepts to understand in any programing language. This is where computers shine. For Python there are two repetition operators available. These are while and for.

The while operation first validates a an expression and if it's true it then executes the indented statements. The repetition ( also known as loop ) will continue until the expression becomes a false. The for operators works differently. for assigns a variable name a value based on another sequence such as a list, tuple, dictionaries or anything that provides a range. The duration of the for loop is then based on the sequence that it is iterating through.

Let's dive quickly into an example that you can relate to in the networking world. Let's create a loop to build a series of ethernet interfaces using various different lists.


interfaces = ["1/1","1/2","1/3","2/1","2/2","2/3"]
ip_adds = ["1.1.1.1","1.1.2.1","1.1.3.1","1.1.60.1","1.1.70.1","1.1.80.1"]
masks = ["/24","/24","/20","/24","/22","/20"]

i = 0
while i < 6:
    print "interface %s \n ip address %s%s\n" % ( interfaces[i], ip_adds[i], masks[i])
    i = i +1

In this case what has been done is created three independent lists that contains information that is related by the index. Then the while loop is run for the size of that list to build the different interface configurations.

When executed:


% python test.py
interface 1/1
 ip address 1.1.1.1/24

interface 1/2
 ip address 1.1.2.1/24

interface 1/3
 ip address 1.1.3.1/20

interface 2/1
 ip address 1.1.60.1/24

interface 2/2
 ip address 1.1.70.1/22

interface 2/3
 ip address 1.1.80.1/20

While this example shows you how to accomplish this with the while loop, it would make more sense to do this with a for loop using a dictionary. If we build a dictionary data structure for these interface configurations, they could look like the following:


interfaces = {
    "eth1/1" :  { "ip": "1.1.1.1",
                 "mask": "/24",
                },
    "eth1/2" :  { "ip": "1.1.2.1",
                  "mask": "/24",
                },
    "eth1/3" :  { "ip": "1.1.3.1",
                  "mask": "/20",
                },
    "eth2/1" :  { "ip": "1.1.60.1",
                  "mask": "/24",
                },
    "eth2/2" :  { "ip": "1.1.70.1",
                  "mask": "/20",
                },
}

The interface name "ethx/x" becomes the dictionary key or index. Then each has a dictionary inside that contains the IP and the MASK defined as a dictionary inside the dictionary ( like a multi dimensional array ). Using the the same logic on the print statement we can create the interface configurations.


interfaces = {
    "eth1/1" :  { "ip": "1.1.1.1",
                 "mask": "/24",
                },
    "eth1/2" :  { "ip": "1.1.2.1",
                  "mask": "/24",
                },
    "eth1/3" :  { "ip": "1.1.3.1",
                  "mask": "/20",
                },
    "eth2/1" :  { "ip": "1.1.60.1",
                  "mask": "/24",
                },
    "eth2/2" :  { "ip": "1.1.70.1",
                  "mask": "/20",
                },
}

for key,data in interfaces.items():
    print "interface %s \n ip address %s%s\n" % ( key, data["ip"], data["mask"])

The following diagram shows better what happened to the dictionary as the for loop executed through the looped code.

Everytime that the for loop goes through the dictionary it picks 1 "row" of the dictionary or you could understand it as 1 item. This for loop will break it then in two separate pieces. In this case the key and then value, that for this example turns into another dictionary. The program can then reference the components directly by the index value name that was specified simplifying the understanding of what the code is doing.

Console 1