A Brief Introduction To Programming

corgi writing a program

If I’m being candid, there is already a whole bunch of content out there on the subject of programming. (If you want to get started on a full-blown course learning how to code in Python, for example, I’d start with FreeCodeCamp)

However, if somebody was trying to explain to me how the engine of a car worked, it would be a very confusing lesson if I hadn’t the slightest clue on how to drive. Someone who can drive may not necessarily understand what happens when you “hit the gas” but is at least a little better equipped to understand how things work. So let’s go for a drive!

a hackerman who is visibly frustrated as his unit tests fail for no apparent reason
Me busy at work. True story

Programming, at its core, is all about data manipulation. You give your code an input, it performs the set of instructions you’ve asked it to on that input and it gives you an output. The input can come in a lot of different forms; numbers, text, images, and so on. They can also come from a variety of sources; user-provided input, computer-generated input, over a network, from a database, (you get the point…). Once you have this input, you can write “code” which is basically a set of instructions telling your computer how to manipulate this data to get a useful output out of it.

Let’s look at our first ever python script!

def add_numbers(num1, num2):
	return num1 + num2

first_number = int(input("First number: "))
second_number = int(input("Second number: "))
result = add_numbers(first_number, second_number)
print(result)

Let’s run this piece of code now. I’ve saved this file as test.py

~ python3 test.py 
First number: 5
Second number: 10
15

Underwhelming? Probably. But let’s take a better look at what this snippet is really doing. (Okay now, I might butcher some of the lingoes for the sake of clarity. So I apologize to my fellow nerds ahead of time).

Remember how I said that all a piece of “code” does is take an input, do something with it, and return an output? You’d be able to see that pattern all over this script that we just wrote. In programming, something is called “a function” if you can invoke it by its name (bear with me here). If you see Line #1, I’ve defined a new kind of function called add_numbers which takes two inputs num1 and num2. And what does this function do? That part is defined in Line #2. The function is expected to give back num1 + num2. That’s what the return keyword is for. It asks the function to compute num1 + num2 and give back the result to whoever invoked the function. The indentation in Line #2 (the blank space before the return keyword) indicates that Line #2 is part of the function body and not a part of our overall snippet. (You’ll see that Line #4 for example does not have any blank space before it. This is how python knows what part of the code belongs in the function body and where the function body ends). Only at Line #6 is when I actually go ahead and invoke the function by its name add_numbers along with the inputs this function takes which are comma-separated inside the parentheses add_numbers(first_number, second_number).

Just like how we defined our own functions, python comes with a set of pre-packaged functions that we can use. The act of taking input from the user is a pretty commonly used functionality in most programs and therefore, you’d see the input function on Line #4. I did not define a input function. I knew Python had a built-in function (Link to docs) for this. And as the doc mentions, you can pass the prompt to be displayed as part of the input collection process which I specified as "First number: ". Similarly, the int function on Line #4. I knew that the input function outputted a string but I wanted an integer since I was looking to add those numbers. Therefore, I passed the output of the input function as the input to the int function so that the int function could give me the integer equivalent. The same line could have been broken down as follows:

string_number = input("First number: ")
first_number = int(string_number)

Okay, this is the last part of the code I’ll talk about. Do you see the left-hand side of any of these lines? string_number = input("First number: "). What we’re instructing python to do is, compute the input function with the input "First number: " and store its output in a variable named string_number. A variable can be thought of as a box. The output of the input function is stored in a box named string_number which can now be passed along to other functions such as int as input.

Okay phew, that was quite a bit. But on the bright side, we’re now officially done looking at code snippets! For now…

Now, instead of looking at “code” as a black box, we can start thinking of it as a collection of “functions” strung together.

a tree format of the same code. functions are connected to each other via lines representing the flow of data
The same program with the flow of data visualised as a “tree”.

Alright. So far, we’ve seen that any code can basically be represented as a “chain of functions”; much like an assembly line in a factory. Data flows in, are acted on by functions and the result of this becomes the input of other functions down the line. But to be completely honest with you… this is a human representation of code. This isn’t what my computer saw when I ran python test.py. Or atleast, this isn’t the complete picture. I’ll save that part for another blog post.

Share