← Home

A Complete Beginner's Guide to Programming

This post may seem out of place on a site for programmers, but I wanted to write something from zero. What even is programming? And, what are the building blocks of programming?

This post is going to start out by discussing what computers are at a conceptual level, and then discuss the programming fundamentals. We'll use Python in this post, but a lot of the building blocks work across programming languages, so this will still help even if you are interested in learning a different language at first.

What is programming?

You probably interact with computers on a daily basis, but let's define concretely what we mean when we talk about computers in relation to programming. A computer is a machine that processes and stores information.

Programming is telling the computer how to ingest, process, and then store that data. When someone writes a program, that person is giving the computer a set of commands that it must follow.

When we write programs, we are writing instructions that the computer will follow. Computers are very literal -- they will take our instructions and follow them exactly, but we must lay them out in a very detailed way so that they understand us.

Programming, at its core, is taking a big problem and breaking it into smaller and smaller problems until they are small enough that we can tell the computer to solve that problem.

Where can you see programs in use?

Everywhere! From your operating system on your computer through complex websites, all are written using code! Older (and newer!) cell phones, fancy coffee machines, self-driving cars, Facebook, Amazon, an ATM, the Lyft App, metro card reloaders, supermarket scanners, and most TVs use code to run in addition to your desktop or laptop computer.

What are programming languages?

Computers can't understand natural language by default, though they are getting closer and closer to being able to!

At their root, computers run on a series of microscopic on and off switches, and when we write code we are toggling them on and off -- just like a light switch! Computers use a numerical system called binary to use these on and off switches. Binary is a numerical system comprised of 1s and 0s in countrast to our decimal system which uses 0-9.

Thankfully, a lot of really smart people before our time thought of a way that we can talk to our computers without zeros and ones. Instead, we use programming languages which can be interpreted by our computer, similar to how language interpreters can translate Spanish to English or even English to sign language. These look a lot more like English than binary, but they still have a lot more symbols and fewer ways of doing things than natural language.

There are tons of programming languages out there, similar to how there are lots of languages spoken around the world. Some, like Assembly or C are very low level and don't really resemble how we speak. Others, like Python and Ruby closely resemble human language. These languages are used for different tasks, for example HTML, CSS, and JavaScript are used to write websites whereas C is used to write your operating system. These have evolved a lot over time -- old programmers used to have to use punch cards and feed them to the computer instead of typing out code on their computers! There are trade-offs between performance and ease of use, but when you learn how to code I would highly recommend one that is closer to normal language!

The Key Fundamentals for Programming

There are some fundamental concepts that move with us from programming language to programming language. We'll use Python, but pretty much every language know of has these fundamentals, though they may be written differently (outside of HTML and CSS which are pretty different).

Brief aside, you can run Python in your web browser using Repl.it. You can create a Python project by pressing the new repl button, and selecting Python. Then, type in the area under main.py. You then can run your code with the green run button.

You can also install Python on your computer by downloading it, and then using a text editor -- my favorite is VS Code. You would then run the code via the command line. You would create a file with a .py extension, then write your code in your editor, finally, you would run the file by running python your_file_name.py.

Hello World

When you learn a new programming language, it's a tradition to write a hello world program. So let's write one in Python!

There's a function in Python that allows us to write text to wherever we're running our code -- so if you're using the command line, it will print out there, if you're using Repl.it, it will print out on the right-hand side of your screen.

We'll talk about what functions are in a bit!

print("Hello, World!")

All of the code samples are also available in repl.its, but are linked instead of embedded

Variables

Variables are a very important piece of programming. Variables store a piece of information that you can use over and over again. If you remember variables from algebra class, they're the same thing conceptually!

In Python, in order to set a variable, we'll write a variable name (in this case name and age), then use an equals sign to set that variable to a value -- in the example "Ali" and 24.

If we use letters, we need to put them in quotes. If we want a number instead, then we'll keep them out of quotes.

name = "Ali"
age = 24

print(name)
print(age)

Now, whenever we can use name or age wherever in our code!

Repl.it

Remember from above, programming languages are for humans, not just the computer. If we were just talking to the computer, then we'd just use 0's and 1's and write binary! So, remember to use variable names that will make sense to future you -- or another programmer who reads your code in the future.

Rule of thumb for variables: If you're going to use a value over and over, create a variable for it!

Data Types

There are different types of data that we can use when programming. Some common ones are integers, floats, booleans, and strings.

Integers

Integers are another math class term - they're basically numbers without decimals. So 1, 0, -100, 200, etc.

We can use integers in Python by just typing numbers!

5
10
-20
30

We can store them in variables, like we saw above, so that we can use them over and over again.

my_favorite_number = 22

We can also do math with them! The four basic symbols are * for multiplication, + for addition, - for subtraction, and / for division.

print(22 + 10)

pieces_of_candy = 20 - 5
print(pieces_of_candy)

Repl.it

Floats

Floats are numbers with decimals -- so 4.0, 4.5, -19.6 etc. They work like numbers otherwise!

Booleans

Booleans are True and False in Python -- these are mostly used to say "yes" or "no" -- remember how computers are a bunch of on and off switches? Booleans are similar!

We can set variables to booleans, but it's usually more useful to compare values and see the results as booleans. One common use is checking equality. Is some variable equal to some value? We use double equals signs to check equality since we use one equals sign to set a variable.

age = 22
print(age == 22) # True

We can also check greater than or less than -- or all those mathematical checks. We use > for greater, < for less, >= for greater or equal, and <= for less or equal!

print(5 > 10) # False
print(10 >= 10) # True

We can also store booleans to variables!

is_greater = 5 > 10
print(is_greater) # False

Things after hashtags in Python are comments -- they're ignored by the computer and are there as notes for yourself or other developers!

Strings

Strings are text in Python. We wrap them in quotation marks!

my_string = "This is a string!"
print(my_string)

Lists

Sometimes, we want to store more than one value in a variable -- say a collection of numbers, the names of the people in a room, the most recent tweets on twitter, or the prices of the items in our shop.

We can store these values in lists in Python!

items_in_store = [5, 10, 15, 8]
dev_employees = ["Ali", "Jess", "Ben", "Peter", "Andy", "Mac", "Liana", "Michael", "Anna", "Mario"]

Rule of thumb for lists: If you have a group of similar things, put them in a list

Conditionals

Another key part of programming is conditionals. These allow us to run blocks of code sometimes and other blocks other times.

So, if a condition is True, run a block of code. Maybe, if something else is true, run a different block of code. Finally, if all the other ones aren't True, run this other block of code.

In Python, blocks of code are indented, so "if this thing is True, run the indented code after it"

name = "Ali"

if name == "Ali":
    print("Hi, Ali!")

Repl.it

Now let's add a condition that will run if the first condition is False. We'll use the else keyword for this!

password = "hello!"
correct_password = "hi"

if password == correct_password:
    print("Welcome to the website!")
else:
    print("Permission denied")

Repl.it

We can also check multiple conditions using elif:

age = 50

if age > 100:
    print("you are old")
elif age < 20 and age >= 13:
    print("You are a teenager")
elif age < 13:
    print("You are a child")
else:
    print("You are an adult")

Put in different values for age and see what changes!

You can use and to check multiple conditions at once! Or or to check if one things is True or a different thing is True.

Repl.it

Rule of thumb for conditionals: If you want certain code to run sometimes, and other code to run other times: use a conditional!

Loops

Loops allow us to run the same block of code over and over again for different values. The most common situation is to loop over a list.

There are two main types of lists -- the first is a for loop. They follow the formula For item in list: do something. The item can be anything -- it's a variable name that changes with each loop.

For example:

dev_employees = ["Ali", "Jess", "Ben", "Peter", "Andy", "Mac", "Liana", "Michael", "Anna", "Mario"]

for employee in dev_employees:
    print("Hi" + employee + "!") # We can join multiple strings together with `+` signs!

At first, employee is Ali, then it moves to Jess, then Ben, etc.

While loops also exist, but they're a bit more rare and have some tricks to them, so we'll skip over them for now!

Repl.it

Rule of thumb for loops: If you want the same code to run over and over again, use a loop!

Functions

Quick jargon breakdown before I start explaining functions:

Arguments - passed into a function each time you call (aka invoke) it.

Parameters - the variables in the function definition.

In def myFunction(x, y), x and y are the parameters. When we run that function by saying myFunction(1, 3), 1 and 3 are the arguments.

When I teach functions, I try to teach them in two ways in order to make sense to two different types of thinkers. The first is a reusable chunk of code that you can plug values into to make your code more versatile and allow for less repeated code. In this case, arguments are the "dynamic" pieces of information that are plugged into the chunk of code. So, when you call the function, that value may change. Pretty much a variable that is different each time you run the function.

I also like explaining functions as a series of inputs and outputs -- kind of like a little machine. You put something into the machine and something comes out based on that. The arguments are the things you are feeding into the machine, and the return value is what is outputted. This matches the algebraic definition of functions more closely -- if you remember f(x) = 2x + 1 from school math, those are functions just written on paper instead of written programmatically.

In Python, the order of the arguments being passed into the function corresponds to the order of the parameters in the function declaration. So, if my function declaration looks like def add(x, y) and I then call the function with add(1, 2), in the function 1 will be x and 2 will be y. If I instead ran add(100, 50), x will be 100 and y will be 50. Since x is my first parameter, the first argument I pass into the function will be x, and since y is second, the second value I pass will be y. Sometimes it's helpful to diagram this out.

Anything after the return keyword is the output for the function.

def subtract(x, y):
  return x - y

print(subtract(5, 2)) # 3, 5 is x, 2 is y
print(subtract(200, 50)) # 150, 200 is x, 50 is y
print(subtract(20, 70)) # -50, 20 is x, 70 is y

five = subtract(10, 5)
print(five)
xysubtract(x, y)
523
20050150
2070-50

Repl.it

Another example:

def say_hi(person):
    print("Hi " + person)
    return person

ali = say_hi("Ali")
print(ali)

Repl.it

This function outputs person (which is the same as the input), but it also performs another action -- printing out the person's name with hi. That first action doesn't affect the output -- or what's returned from the function. If you print out ali it's "Ali"!

Rule of thumb for functions: If you want to reuse a chunk of code, potentially with different data, use a function!

Next Steps for Learning how to Code

These are some really important fundamental concepts for programming, but there are so many more! Two important ones are debugging and problem solving.

When you're writing code, the computer is really smart in that it does exactly what it tells you too. But, if you have a typo or any incorrect code, your code is going to throw an error! Learn to work through those errors and love them for telling you what's going on rather than fearing them! They're so helpful!

Problem solving comes in when we try to put the pieces of the puzzle together to make different programs. I'm actually working on a series about that now!

Also, if you want more free resources for learning programming, here's some great ones!

Be the first to know about my posts!

Share this post with a friend!

LinkedIn
Reddit