Introduction to Python

Python Jun 7, 2014

I decided to get familiar with Python so thought I would start sharing my notes. I have done some scripting in the past to help me get things done quicker, eliminate doing the same task multiple times and to manipulate data, most of this was done using Perl (and a few years back when I was focused on NMS/OSS tasks). In the current project i am working on I have been testing applications API and thought instead of just using curl to pull back the JSON/XML output I would go a step further and use Python and then manipulate the data into a desired format. The other desire to get a better understanding is the way the industry (automation, orchestration and SDN) is going and the adoption of Python to achieve many tasks.

To start of with I got familiar with the basics.

Unlike other languages Python requires a structure to be followed, to help me with this (and also save me time) I have been using Sublime Text 2 as my text editor (it has some nice plugins for Cisco Syntax as well).

These are just example of the syntax, if you are new to scripting or programming I would recommend that you either get a book (can be a bit dry), take an on-line course (I have used and but a search will show many exit) or read one of the many tutorials on the Internet.

    # Define a variable
	# Define a variable
    limit = 10
    # Print to screen
    print "You can see this on screen"
    # Combining variables (basic arithmetic)
    total_left = money - (beer + nuts + shots + kebab)
    # Combining variable in a print statement
    name = "Rob"
    age = "33"
    print "He is called "+name+", and is "+age+" old"
    # if statement syntax
    if sport == "rugby":
        print "Lets go watch it!"
        print "Stay at home!"
    # for loop syntax example
    for i in range(1,11):
        print i
    for i in range(1,11):
        for j in range(1,11):
            print "i=",i,". j=",j
    # while loop syntax example(s)
    limit = 10
    while limit == 10:
        print limit
        limit = 1
    number = 1
    while number < 10:
        print number
        number = number + 1
    # Capture data from a user
    username = raw_input("Who are you?")
    print "You are "+username
    # Type Conversion (string & integer)
    age = raw_input("What's your age?")
    age = int(age)
    #convert integer to string
    # number = str(number)
    # Can also use float() rather than int()
    # Mathematical Options
    # + adding
    # - subtracting
    # * multiply
    # / division
    # % modulo
    # Else if syntax example
    score = raw_input("cc?")
    if score == "5":
        print "What no conversion?"
    elif score == "7":
        print "You got the conversion as well"
    elif score == "3":
        print "Penalty time!"
    elif score == "4" or score == "6" or score = "2":
        print "You are playing Rugby league not union."
        print "What you didn't score any points?"
    # functions
    def myFunction(p):
        return "test of " +p
    a = 10
    name = "Rob"
    print myFunction(name)
    def myAdd(a, b):
        return a + b
    print myAdd(3,4)
    # data structures
    months = "jan, feb, mar, apr, may, jun"
    #using a list has index functions
    months = ["jan", "feb", "mar", "apr", "may", "jun"]
    for i in range(0,6):
        print months[0]     # Will output Jan
        print months[i]   
    #data structures
    list = [1, 2]
    entry = raw_input("Give me a number: ")
    entry = int(entry)
    for i in range(0,3):
        print list[i]
    # Nested list
    months = [ ["jan", 31], ["feb", 28], ["mar", 31], ["apr", 30], ["may", 31] ]
    number = int(raw_input("Enter the months number"))
    print months[number-1][0] #gives the month name
    print months[number-1][1] #gives number of days in month
    #or another cleaner option
    print "Name: " + months[numbers-1][0] + ", number: " + str(months[number-1][1])
    # Further lists functions
    a.append(4) # Adds 4 to the end of the list
    b = a.pop()
    print str(b) + " - is the last entry"
    # So far covered;
    # lists
    # append
    # indexing
    # nested
    # string and numbers lists
    # pop, reverse, sort

As I increase my Python knowledge I will update and add new notes as well as put some example up, so far I like Python much more than other languages I have played with and seems very powerful.


Rob Edwards

Northern (UK) chap focusing on platforms, automation, cloud and cloud native applications. Recovering network engineer, although it turns out networking is as important, if not more, now than before!