Scratching the Surface

In the last blog posts, we covered just starting scratching the surface of Python Data Types and Python Statements.  We also explained what each data type was, general use cases, and how to run some standard methods against them.  Next, we went on to the three Python statement types. A brief description of what they can do, with a real-world non-programming example of choosing one vs. the other.

These posts are by no means all that you can do with Python.  For instance, I did not show that you can nest for loops inside of other for loops:

Nested Loop Example

lst1 = [1,2,3,4,5,6]
lst2 = ['a','b','c']
for num in lst1:
    for let in lst2:
        print(num,let)

What the above does is for every number in lst1, it will print a letter from lst2 next to it;

1 a
1 b
1 c
2 a
2 b
2 c
3 a
3 b
3 c
4 a
4 b
4 c
5 a
5 b
5 c
6 a
6 b
6 c

If we break down what the script is doing:

  • for num in lst1
    • takes the first element in lst1 and processes what is below it
  • for let in lst2
    • takes the first element in lst2, and processes code below it
  • print(num,let)
    • the above statement will be the first element from lst1 and the first element from lst2
  • Since we are still in the bottom for loop, and there are still items for that for loop to iterate over, the first for loop for num in lst1 does not run again until the intended for loop has completed its iteration.
    • Once the indented for loop completes its iteration, it returns to the top for loop and starts the process again.

Reflection

It took me almost six years to learn and reach where I am today. Mostly, that six years was a combination of things. For starters, I did not believe that programming would help with anything. When I wrote my first script, I realized what this could do for my productivity.  We are all comfortable with how we do work.  I had a system where I would have config ready in a text editor and find and replace keywords that I put in.  If I was adding a BGP peer, instead of remembering the last neighbor IP I configured or what ASNs I used last, my configs looked like this;

configure router bgp group <"NAME"> neighbor <IP> vpn-apply-import
configure router bgp group <"NAME"> neighbor <IP> vpn-apply-export
configure router bgp group <"NAME"> neighbor <IP> import <IMPORT_POLICY_NAME>
configure router bgp group <"NAME"> neighbor <IP> export <EXPORT_POLICY_NAME> 

It was quickly repeatable, and I had config templates for everything I did.  I did not realize I could template this in Python, have it prompt for information once, and fill out all required information.

Once I wrote a script to solve this issue, it became apparent how powerful it was.  Now, I will write a script to do something any chance I get, even if I only use it once. The way I learn is by repetition.  If I only use the script once, that is knowledge I can store and keep for the next!

Next, we will delve into the beautiful world of functions!

To read more on Python: https://www.python.org