I’ll let you in on a secret — coding is actually easy

Part 3 of the Coding for Marketing People series. By the end of this post, you will learn how simple it really is.

The way people talk about coding makes it sounds like brain surgery. The truth is that if you can use a calculator, you can code.

I think the ‘difficult’ part is knowing how to approach coding and how to navigate the wider ecosystem of the stuff that goes around coding —like how to run scripts on your computer, how to host them online somewhere, which software to use or how to debug code that isn’t working. It’s true that this is all part of the coding experience, but it’s not always necessary.

At it’s simplest, coding is just about writing a series of simple instructions, one at a time.

The trick to coding is knowing enough to make the computer do what you want. Ideally your code will be tidy, elegant and efficient but what’s most important is that it works.

Want proof? OK, here’s a snippet of Python code:

A = 1
B = 2
C = A + B
print C

That is totally legitimate, 100% working Python code. You can save those four lines into a file and host it on a server, and it would run. Better still, you can probably tell what it’s going to do as well — even if you’ve never written any code before in your life.

Let’s see it in action

You’ll need to have Python installed on your computer first. If you haven’t already, follow our easy peasy installation guide for Python.

Now open up the Terminal application on OSX, or Command Prompt if you’re on Windows. Type the following word and hit enter:

python

We’re off! What you’re seeing is the Python Interpreter in interactive mode, which means you can feed it commands ‘interactively’ (one at a time) and it will execute them instantly. Each new line starts with three right angle brackets (‘>>>’) and each command is executed when you hit Enter. Unless the statement requires an explicit response, it will just quietly execute the instruction in the background and wait for the next one. If you did something wrong, you’ll know because you’ll get an error message, so no news is good news here.

Next, type in those four commands one line at a time, pressing enter after each one:

>>> A = 1
>>> B = 2
>>> C = A + B
>>> print C

Here’s roughly what you should see:

You just type ‘python’ to start the interactive interpreter. Here’s roughly what you’ll see if you’re following along on a Mac.

Congratulations! You’ve just done your first bit of coding. The result of those four lines was the number 3 printed on screen.

It may seem obvious but just to be sure, here’s how that worked:

  • First you assigned the value of ‘1' to the variable ‘A’. The variable on the left of the equals sign is the target and the main ‘subject’ of the statement. On the right is the value we want the target to adopt.
  • Next you did similar again and assigned the value of ‘2' to the variable ‘B’.
  • Now that we have A and B, you created a third variable (‘C’). Instead of assigning a direct value to it, you performed an operation on two others by commanding Python to add A and B together. Python will always execute whatever is on the right of the equal sign first, and then assign the result to the target on the left side. Python (and all languages) have a lot of built-in operations you can perform; many of them will be familiar whilst others will be strange at first. Here, adding two things together is as simple as putting a plus symbol between them, just like in common maths.
  • Finally, we used a built-in function called “print” to (drumroll) print the value of C out to the screen.

That’s it, a super simple but totally authentic example of Python code.

Python is quite forgiving; if a variable doesn’t yet exist, it’s created on the fly. In other languages you’d have to explicitly create each variable first but for now, Python’s forgiving nature will help us to write simple code.


Using these principles in a real-world scenario

Here’s a more complicated example, but hopefully shows that real-world situations follow the same approach:

>>> import urllib2, json
>>> response = urllib2.urlopen('https://graph.facebook.com/philsheard')
>>> json_response = json.loads(response.read())
>>> print "My name is {0}".format(json_response['first_name'])
My name is Phil

In the second four-line example above, we start by importing a couple of the included libraries that Python provides to do common tasks: urllib2 (for working with internet resources) and json (a library for dealing with the json file format, common across web services). Python is described as ‘batteries included’, which means it comes with lots of these handy libraries for doing many things. It’s one of the things that makes it great for beginners.

In line two, we use a function in the urllib2 library (‘urllib2.urlopen’) to download some data from the internet, in this example Facebook, by feeding it with a URL target.

Third we ‘parse’ the response (which happens to be text in the json format) into a more usable format and assign the result to a new variable called `json_response`.

Finally, we tell Python to print out a string of text and insert a variable into the sentence, at the location marked by the ‘{o}’.

There you go. Another simple but much more realistic example of coding in action. We’ve grabbed some data from the internet, modified it and printed it out in the same number of lines it took to do the basic maths in example one. We used some more advanced techniques but I hope it shows that it’s just a series of small steps towards a specific objective. Once you learn the basics, you’ll find yourself doing things you couldn’t have previously imagined.

If you found this useful, subscribe to the collection here on Medium and you’ll get an alert for every new post in the series.


Oh yeah, and you know the secret — that coding is easier than people think? I want to spread the word but if it makes you look clever in front of your boss / friends / potential sexytime partner at a bar, then I won’t tell if you don’t.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.