Decisions

When working in programing languages the decision making logic is what tells the program to make decisions based on values and conditions. For example if the value of something is equal to something else then do something.

In Python the keyword is if that is the same used in many programing languages, but Python has some twists as we introduce the concept of Python indentation.

Different than from other languages, Python uses indentation to denote what is to be executed following a condition marked by the colon :. One way to view this is that a colon : indicates that you are entering a part of the program that is isolated from the rest of the program unless a condition is met. The spaces are very important and many Python IDE ( integrated development environments ) and editors assist in keeping these correct.

The spacing in indentation is very important to understand because it will cause an error if the indentation is incorrect. If you look at the following example you can see this. A single space on the second print returns a fault. The reason for this is because there is no conditional statement before the indentation and Python uses the spacing for the structure of the language.


>>> car = 0
>>> if car == 0:
...     print "Yes"
...      print "NO"
...
  File "<input>", line 3
    print "NO"
   ^
IndentationError: unexpected indent

Conditionals are very important to understand as they will provide you with the ability to execute code based on meeting criteria. Conditionals are evaluated or are executed based on an expression that returns True or False.

Yet the conditional will only execute on a True condition! So if you want a false condition to execute some code, you would have to use the negate to invert it to a true. Let's go through a series of examples on this.

Looking at the example on the left we will do a very simple conditional. We have two different IP addresses and want to validate if the last octect of the IP and compare them. In this case we use the string split method the break the IP address into a list based on the dot.


ip1 = "10.1.1.1"
ip2 = "10.1.1.2"

ip1list = ip1.split(".")
ip2list = ip2.split(".")

if ip1list[3] < ip2list[3]:
    print "yes"

Another construct in Python is the else condition. In this case you can view this as the alternative if a condition is not meet. In Python this is done via the else and the elif operators.

The else operator is simple to understand. The condition that first is met in the if statement is evaluated. Once that is done, if the expression turns out to be false it will hit the else construct.


ip1 = "10.1.1.1"
ip2 = "10.1.1.2"

ip1list = ip1.split(".")
ip2list = ip2.split(".")

if int(ip1list[3]) > int(ip2list[3]):
    print "yes"
else:
    print "no"

The elif operator control makes it possible to do another evaluation if the original expression returns false. Looking at this example we can see that we have a list of IP addresses. The program splits the string on the . and returns the last octect with index of 3 to the string.

Then the if and elif operations happens on the values of the last digits of the IP address


allip = ["10.1.1.10", "10.1.1.9", "10.1.1.15"]

ip1 = allip[0].split(".")[3]
ip2 = allip[1].split(".")[3]
ip3 = allip[2].split(".")[3]

if int(ip1) > int(ip2):
    print "ip1 is bigger than ip2"
elif int(ip2) > int(ip3):
    print "ip2 is bigger than ip3"

Since Python doesn't have a switch operator ( common in many other languages ) python would use the if elif to replace that functionality is the if and elif operators.

In this case we are simulating if the user picked an option from a menu. These options come into the if/elif tree until all possible options are hit. Once the proper match is met then the code related to that condition is executed ( in this case a print statement )


options = ["insert", "update", "delete","trim"]

selected_option=options[2]

if selected_option=="insert":
    print "user selected insert"
elif selected_option=="update":
    print "user selected update"
elif selected_option=="delete":
    print "user selected delete"
elif selected_option=="trim":
    print "user selected trim"

% python test.py
user selected delete

If statements can also be nested. In this example two separate checks are being completed. The first checks to see if the user selected a insert. Then once that is passed the program takes the input MAC address and splits it to validate the OID.


options = ["insert", "update", "delete","trim"]
ether  = ["00:01:0c:15:15:15","00:01:0c:19:06:21","00:01:0c:56:11:15"]

selected_option = options[0]
selected_ether = ether[1]


if selected_option == "insert":
    ethaddr = selected_ether.split(":")
    if ethaddr[0]=="00" and ethaddr[1]=="01" and ethaddr[2]=="0c":
        print "This MAC {} has a Cisco OID!".format(selected_ether)
Console 1