Python3 // Lesson 2: Conditionals and Loops

  1. Conditionals
    Length is 8 minutes
  2. Boolean Logic (Booleans, Ternary Operator, None)
    Length is 8 minutes
  3. Python For Loops
    Length is 5 minutes
  4. For Loops (Enumerate)
    Length is 6 minutes
  5. For Loops (Break and Continue)
    Length is 9 minutes
  6. While Loops
    Length is 5 minutes
  7. Loops Miscellaneous
    Length is 6 minutes

Additional Content:

How To Write Conditional Statements in Python 3​​

How To Construct For Loops in Python 3

How To Construct While Loops in Python 3


Reference code for these exercises is posted on GitHub at:

1. Read the “show_vlan.txt” file into your program. Loop through the lines in this file and extract all of the VLAN_ID, VLAN_NAME combinations. From these VLAN_ID and VLAN_NAME construct a new list where each element in the list is a tuple consisting of (VLAN_ID, VLAN_NAME). Print this data structure to the screen. Your output should look as follows:

[('1', 'default'),
 ('400', 'blue400'),
 ('401', 'blue401'),
 ('402', 'blue402'),
 ('403', 'blue403')]

2. Read the contents of the “show_arp.txt” file. Using a for loop, iterate over the lines of this file. Process the lines of the file and separate out the ip_addr and mac_addr for each entry into a separate variable.

Add a conditional statement that searches for ‘’. If is found, print out the string “Default gateway IP/Mac” and the corresponding IP address and MAC Address.

Using a conditional statement, also search for ‘’. If this IP address is found, then print out “Arista3 IP/Mac is” and the corresponding ip_addr and mac_addr.

Keep track of whether you have found both the Default Gateway and the Arista3 switch. Once you have found both of these devices, ‘break’ out of the for loop.

3.  Read the ‘show_lldp_neighbors_detail.txt’ file. Loop over the lines of this file. Keep reading the lines until you have encountered the remote “System Name” and remote “Port id”. Save these two items into variables and print them to the screen. You should extract only the system name and port id from the lines (i.e. your variables should only have ‘twb-sf-hpsw1′ and ’15’). Break out of your loop once you have retrieved these two items.

4. You have the following data structure:

arp_table = [('', '0062.ec29.70fe'),
 ('', 'c89c.1dea.0eb6'),
 ('', '1c6a.7aaf.576c'),
 ('', '5254.aba8.9aea'),
 ('', '5254.abbe.5b7b'),
 ('', '5254.ab71.e119'),
 ('', '5254.abc7.26aa'),
 ('', '5254.ab3a.8d26'),
 ('', '5254.abfb.af12'),
 ('', '0001.00ff.0001'),
 ('', '0002.00ff.0001'),
 ('', '6464.9be8.08c8'),
 ('', '001c.c4bf.826a'),
 ('', '001b.7873.5634')] 

Loop over this data structure and extract all of the MAC addresses. Process all of the MAC addresses to get them into a standard format. Print all of the new standardized MAC address to the screen. The standardized format should be as follows:


The hex digits should be capitalized. Additionally, there should be a colon between each octet in the MAC address.

5. [Optional/bonus] 

*** Note, to actually test this in your environment, change the test IP addresses to something in your environment that you can ping successfully. ***

Construct a list of 254 IP addresses. The base IP address should be equal to ‘’ or ‘10.10.100.’.

You should use the ‘range’ builtin to accomplish this.

Your list should have all of the IP addresses from to

Use Python’s ‘enumerate’ to print out all of the IP addresses and their corresponding list index. The output should look similar to the following: 

0 --->
1 --->
2 --->
3 --->
4 --->

Use a list slice to create a new list that goes from to

Using a loop and os.system(“ping -c 3”) try pinging all of the IP addresses in this short list. For Windows the command will probably be os.system(“ping -n 3”).

Put a variable at the top to define whether you are using Windows or Linux/MacOs. This should be similar to the following:


base_cmd_linux = 'ping -c 2'
base_cmd_windows = 'ping -n 2'
# Ternary operator
base_cmd = base_cmd_windows if WINDOWS else base_cmd_linux