A word before I begin
You can do no better in learning Python programming for data handling/analysis than to study for yourself one of Charles Servance’s MOOC’s, or to download and work through his freely-available, ‘remixed’ open-source book (see References below for links to both). I am not even standing of the shoulders of giants here… I am merely scurrying along way behind them all, picking up what few crumbs of knowledge and wisdom I can along the way. Blogging my own educational journey – as a 40-somthing woman heading into the world of tech – is a way to help me consolidate my own learnings. If it can help you too – above all by showing empirically that “if she can, then I can” – it will have served a double purpose.
Jargon
I’m working on a glossary here, but at the moment it is very unstructured and massively incomplete. After a couple of months of this, I might be in a better position to turn it into a more structured glossary. In the meantime, Google’s yer man!
Preamble: why code?
I wanted to learn to code partly for improved career prospects and mainly because I realised it’s something I should have learnt to do a long time ago. Various roles in finance/accounting over the years epitomised the idea of the ‘mundane task’ computers can do in a fraction of the time we can. I’ve been following AI and machine learning a bit these last few years too – specifically how it’s going to impact accountancy jobs at some unspecified point in the future.
Bring it on, I say. Humans must surely deserve better than to have to carry on being accountants? (Unless karma is real, and we all committed terrible heinous crimes in past incarnations for which an accountancy career is our cosmic punishment in this life!)
The users are on the ‘outside’ – they get what they’re given. They’re dumb; they have to follow orders. (They may even get made obsolete.)
I’m not happy to settle for second best. And I definitely don’t like following orders!
I can really see how more programming skills can help me in a business/finance environment. I can also see how learning more about the systems and programming side of data/analysis will help me to help my employer (and lots of other companies too) to run more efficiently, use data more effectively, make better decisions and ultimately make more money. So you see, it has a practical, sensible purpose.
But also, a programmer is an ‘insider’ – she gets to speak to the computer more directly. Or, better, she gets to tell the computer what to do. And gets the cpmputer working on stuff for her. I like that. I like the power and the autonomy of that. So it is also a philosophical stance too.
And I can easily imagine me staying up ’til the small hours working on some silly programming ‘game’ which adds no financial value to anyone – but would please my inner child immensely.
And my inner child is ‘she who must be obeyed’.
Let’s kick off…
Code has a certain syntax, the structure and language the program understands, with certain words reserved for specific functions. Each line of code is a sentence. And a program is the multiple sentences strung together to provide a sequence of commands or functions (simple or complex) which the computer follows to the letter (or not, if we screw it up).
Here’s some reserved words:
and – as – assert – break – class – continue – def – del – elif – else – except – exec – finally – for – from – global – if – is – import – in – lambda – not – or – pass – print – raise – return – try – while – with – yield
I even understand what some of those words mean already. Great stuff.
Here’s the world’s simplest (and most ubiquitous) bit of code:
We’ve written some English here, be we’re also writing a “syntactically correct Python sentence”. (Note how Python really doesn’t like ‘Sentence case’, but it’s OK ‘cos our English teacher isn’t reading this so we can carry on.)
Some other kinds of simple sentences are:
Assignment statement (assigning the value 2 to the variable x): x = 2
Assignment with expression (like above but the value being assigned to the variable x is some mathematical expression): x = x + 2
Putting together a sequence of sentences creates a Python script, or a Python program (the equivalent of our paragraph in English). It’s possible to run a short script (only a few commands long) directly in the interactive Python window. But longer programs or scripts should be written in a text editor like Notepad++, and saved with the file extension .py so the computer knows it’s Python code, not some other language. You can then run the program from the command window: using cd to go to the directory containing the program file, type the filename (including the extension .py).
The program has a required structure too, known as the program steps or program flow. Most steps will be sequential – actioned one after the other. Some steps are conditional – they’ll only be run if certain conditions are met otherwise they’ll be skipped over. Other steps are iterative – they’ll be repeated over and over (usually until some condition forces a stop).
The path is the steps/order the programmer wants things to be done in, and the program is written such that Python can follow along in the required order. Think of it like using punctuation in sentences, to tell the reader where to pause, and where to continue. Or better yet, in maths how brackets (parentheses) are used to define the order in which calculations should be performed e.g. (2 x 4) + 3 is not the same as 2 x (4 + 3).
A simple program with sequential steps looks like this:
This will output: 1; 2; 3
Python will just run this, line after line til it gets to the end.
A simple script with conditional steps looks like this:
This will output: smaller than 5; the end
The reserved word if tells Python “If yes (TRUE) then do the indented bit, but if not (FALSE) then skip the indented bit and carry on”.
An iterative script (with repeated steps) looks like:
This will output: 3; 2; 1; Go!
The reserved word while tells Python “If yes (TRUE) then keep on doing the indented bit over and over until not (FALSE), then skip the indented bit and carry on”. This is known as a loop, and it has an iteration variable (here called n) that changes each time it goes through the loop. (Note that for is also a reserved word indicating an iterative function.)
Here’s the best quote from the book so far (perfect for a control freak like me):
Python is a way for the creators of programs to express
how the conversation is supposed to proceed.
References
Book: Programming for Informatics – Exploring Information by Charles Severance
Course: Programming for Everybody (Getting Started with Python) by Univ. of Michigan. Part of the Python for Everybody specialisation.