Monday 26 January 2015

How to code: a syllabus



If I was asked today, what a researcher (who isn't from a computer science background) should do to learn how to code, I would suggest they just start. It doesn't really matter where you start or in what language. There are some languages that offer more return for the same effort, like Python, because they are well structured and logical languages. Python can be used for a huge range of tasks because it is a general programming language that has libraries (add on functionality) that add domain specific functionality. Rather than a programming language designed for a specialist task like R or MATLAB that are often difficult to use in other domains like text handling or negotiating network connections.

import antigravity

def main():
     antigravity.fly()

if __name__ == '__main__':
     main()

 
 

But it doesn't really matter what sort of programming you start with, they will all start you on the path to thinking like a coder. That is - logically, structurally, algorithmically and in a way that breaks a problem down into component pieces that can be solved once and used as a component in many situations after that.
Most languages have the basic parts to assign a data structure to a variable and create a loop to repeat an action a particular number of times and maybe change one component each time through the loop.

words = 'hello world ! we require a shrubbery'.split()
 
for word in words:
    print word, len(word)
 
 
hello 5
world 5
! 1
we 2
require 7
a 1
shrubbery 9

or

# First create a list of words to use

# The easiest way to do this and avoid putting in all the quotation marks and commas is


words = 'hello world ! we require a shrubbery'.split()
 
# The split() function will split the string at the spaces and return a list. 
 
# Pop will remove a word from the end of the list words, 
# check the length with len() to stop when we have removed all the words
 
while len(words) > 0:
    word = words.pop()
 
# Use string formatting to construct the response and indexing to read 
# the word backwards 
 
    print word, "reversed is :- %s" % word[::-1]
 
Output: 
 
shrubbery reversed is :- yrebburhs
a reversed is :- a
require reversed is :- eriuqer
we reversed is :- ew
! reversed is :- !
world reversed is :- dlrow
hello reversed is :- olleh

The will have a way to perform an action based on a decision, like True or False:

# get a response from the user and assign it to the variable answer

answer = raw_input("Would you like a quote from Monty Python, yes or no? ")
 
# make answer all lowercase because 'Yes' and 'yes' are different to Python
 
if answer.lower() == "yes":
 
# this only gets printed if answer == 'yes' 
     
    print "We require a shrubbery"
    
Output:
Would you like a quote from Monty Python? yes or no? no




For researchers the Software Carpentry workshops are the best way I've come across to learn about programming. They are presented by domain experts and usually these presenters are not primarily programmers, so they have insight into the difficulties of learning programming from another field.

In the boot camp you will cover shell programming, version control with Github, the concept of code testing for reliable and maintainable code and a programming language (either Python, R or MATLAB).

Once you have this overview of the parts necessary for your journey into code, there are a number of great (and free!) resources I would strongly recommend.

"Programming for Everyone" on Coursera is run by the fantastic Dr. Chuck.

After those two steps, I'd suggest coming along to the NeuralCode group or something like it in your area.