Introduction to Python

Introduction

Over the course of my IT career, I have used a few different platforms to create tools that help me in my day to day work. Initially, VBScript used to be my scripting tool of choice when I was a Sysadmin. When PowerShell was released, I quickly embraced it and really liked it’s human readable verb-noun structure. It saved my bacon on many an Exchange deployment/migration. I’ve also dabbled in various flavours of Visual Basic over the years to create some small applications but I am no developer, by any stretch of the imagination.

Over the last 18 months or so, I’ve been looking to learn a tool that was more cross platform, easy to pick up and could be used across the board for not only my networking tasks, but anywhere I could save time and do things more consistently. I probably spent far too long dwelling on the options but about this time last year settled on Python.

This discussion is about Python but that assumes that you have chosen Python yourself as a language you wish to learn. If that isn’t yet the case, the discussion below on which version of Python to use could also be applied to which language you should settle on and the sections further below apply to any programming language you finally set out to learn.

Version

Don’t get hung up on whether to use Python 2 or 3 (or Java, Go, Ruby, C, etc. for that matter). Whilst 2.x fans will argue it has a larger support base including more modules and libraries, more deployed code etc. it is well known that 2.7 was the last version that will be released on that track and the creator of Python, Guido Van Rossum seems to be committed to the 3.x train. I have been learning 3.x because I found that everything I set out to do in my first months learning Python had suitable modules available.

The key is not to spend too much time on this decision though. Take a look at them both, read up on some of the arguments in favour of each and then pick one and stick to it. Most modern Linux distros seem to have both versions installed so if you are really unable to flip a coin, learn both…but learn at least one of them!

As a quick note on why I chose Python over other options I would have to say:

  1. It’s relatively easy to learn
  2. There are loads of on-line resources
  3. It is well supported across many different areas and vendors
  4. I can use it to create the most basic of scripts up to the most elegant of object oriented applications and everything in between
  5. It’s fun!

Tool kit

When I say tool kit, I’m talking about setting up your environment to make working with Python and the code you create more enjoyable. There is nothing worse than trying to learn something new and having to jump through a load of hoops before you get to the good stuff. Take GNS3 back in the early days. I can remember spending up to an hour on some occasions just to get my lab up without crashing. Not fun. The bullet points below briefly cover off the main points you should be getting in place:

  • Platform. I come from a Microsoft background and initially started playing with Python on Windows. I quickly realised that Linux was the way to go. Not only is it better supported and comes already installed on many popular distros, it has the added benefit of upping my Nix skills at the same time. Hoorah!
  • Editor. For me, there are two options here. Either Notepad++ or a Python specific Integrated Development Environment (IDE) and I use both of them depending on my needs. Notepad++ has syntax decoders built in for most languages in use and if you save your file with a .py extension, it will get recognised as a Python file, with nice colour coding to highlight your syntax. An IDE adds better file management and will also have intelligent help systems that can give you pointers on the usage of certain features and syntax. I use Jetbrains Pycharm as an IDE, but most of my scripting is done in Notepad++ at the moment. Make sure you set both of these up in a way that suits your workflow. As an example, I have got my Notepad++ configured to insert four spaces when I press the TAB key as it’s easier to do but Python convention dictates that indents are spaces and not tabs
  • Code repository. You will quickly find that keeping track of your different scripts and the various versions of each of those becomes a time consuming task so you will want to utilise something like Git, which is what GitHub uses. You can get an account for free but just be aware that if you want to keep any of your code private (e.g. it contains database connection strings, or IP address information), then you will need to pay for a private account, which isn’t extortionate. Another option if you are feeling daring is to install your own version of Git

Training materials

The best resources are to be found on-line and there are countless websites that have great content relating to Python on them, with the Python homepage being a great starting place. I haven’t found any really good video based training to date that would pass as a course as such but there are lots of books available on the subject. My favourites so far have been:

  • Python Cookbook (latest edition for Python 3)
  • Hacking Secret Ciphers with Python
  • Python 3 Object Oriented Programming

Read the reviews, use Amazon’s ‘Look Inside’ feature to see if the style suits and start building a reference library to consolidate that knowledge.

Get yourself a project and play, play, play

All those tools and all that knowledge will still make your journey a tedious one if you don’t apply it to the real world. Find a project that could use your Python skills and put them to the test. I initially struggled trying to learn Python from a network engineer perspective as it involved ugly screen scraping techniques on kit without APIs. What I found really useful was when we started using a dashboard application in house that is built on Ruby and converted all the Ruby scripts to Python. I now feel much more confident about tackling  more difficult tasks. The key is not just to type up the scripts you find on-line or in a book but to create something that you needed anyway. This will be so much more rewarding for you in the short term, which will motivate you further in the long term. If you are struggling to find a project, buy a Raspberry Pi and a related project book to get your teeth in to.

Summary

Regardless of what field you work in, if you use a computer on a regular basis and have a number of tasks you repeat, I strongly suggest you look at Python as a great tool to help you get those tasks done quickly and more consistently. You’ll soon start seeing that you can use these skills all over to help make life easier.

[sourcecode language=”python”]
print("Till the next time")
[/sourcecode]

Please let me know your thoughts!