Python Flow Control

Everything comes down to evaluating expressions. That means our code isn't going to be ran from the start to the end of the code sequentially. Our code will use flow control to decide what to do next. And with networking, we're likely to decide what to do next after looping through a set of data to

Python Flow Control
Python Flow Control

Everything comes down to evaluating expressions. That means our code isn't going to be ran from the start to the end of the code sequentially.

Our code will use flow control to decide what to do next. And with networking, we're likely to decide what to do next after looping through a set of data to meet a condition.

Flow Control Concepts

I feel it is important to know about flow control to make decisions and to repeat tasks. Things we will do when dealing with networks.

Flow control will start with some sort of condition that is performed by code we write. The result will evaluate to a True or False, a Boolean value.

Comparison Operators

When it comes down to a Boolean value, there needs to be a way to evaluate to a True or False.

There is a list of Comparison Operators:

OperatorDefinition
==Equal to
!=Not equal to
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to

Brings you back to grade school days, right?

I'll generate a few examples:

>>> 11 == 11
True

>>> 11 != 11
False

>>> 11 != 12
True

>>> 'Apple' == 'Orange'
False

>>> 5 < 10
True

>>> 100 > 200
False

>>> 90 <= 100
True

>>> 1000 >= 999
True

When I think about how I can bring this back to networking, I think of the following:

  • Is ethernet1/1 up?
  • Is channel utilization >= 50%
  • Is IP address 1.1.1.1 reachable?

You can say these all would evaluate to a True or False, right?

Flow Control Statements

Our Python scripts will make decisions based on network conditions.

A flow control statement will determine what to do with that condition.

Here are some conditions:

if

The if statement begins with the if keyword. It boils down to "If this is true/false, then execute this code."

The format would look like:

if interface_status == 'down':
    print('ethernet1/1 is down!')

I start my statement with the if keyword.

Then I use a variable, interface_status, to see if it is equal to down. This line ends with a colon :

If it is equal, then I issue a print statement to the screen. This print statement is a new block of code, indented.

else

An if clause can have an else statement. Because if our condition is not met, what should we do?

If the interface is not equal to (!=) down, what should we do? In that case, we execute the next block of code. That would look like:

if interface_status == 'down':
    print('ethernet1/1 is down!')
else:
    print('ethernet1/1 is up!')

elif

It is possible to have more than one clause to execute against. There could be multiple conditions to match on. It cannot simply be one or two conditions.

That's where the else if statement comes in, or elif.

If conditions turn out to be false, then we can go through each elif.

Where I can think I'd use this is determining ping response time:

ping_time = 43

if ping_time < 10:
    print('Excellent response time!')
elif ping_time <= 50:
    print('This is a good response time.')
elif ping_time <= 100:
    print('This is a fair response time.')
else:
    print('You have a bad network connection.')

If ping is under 10ms then that's Excellent.
If ping is less than or equal to 50ms, then it's good.
If the ping is less than or equal to 100ms, then it's just fair.
If its anything else, it's a bad connection.

This portion is starting to be a lot to take in. My mind was thinking about all sort of conditions and Boolean values. I started thinking about how this all ties back to network automation. It's slowly starting to click.

What I'll talk about next are Loops. Things are going to get interesting real quick.