5 Concepts Every Brand New Programmer Needs to Know

Roberto
13 min readDec 18, 2023

--

Photo by David Brooke Martin on Unsplash

So, you just got started with programming? Every programming language is different, but there are many concepts that are universally shared among them. Learning about these basic concepts — variables, operators, conditionals, functions, and common data structures — will greatly improve your coding skills!

Variables

What is a variable? In simplest terms, this is some value that has a name.

# example Python code
message = "Hello world"

In the above example, we declare a variable called message in Python which stores the value "Hello world" . Now, it is possible use the stored value by referencing the variable message . A good analogy I remember being taught was the concept of variables as cups. You can imagine the variable message being a cup and you’ve poured the liquid Hello world into it. This is helpful because now we don’t have to go around saying writing Hello world everywhere; we can just reference our variable message , and our code will know what we’re referring to.

Variables are called “variable” because you can change their value — just like how you can change what liquid a cup is holding. In the example below, we can take our variable message and change it from saying Hello world to Goodbye world .

# Python code
message = "Hello world"

print(message) # Outputs --> Hello world

message = "Goodbye world"
print(message) # Outputs --> Goodbye world

In almost all programming languages, the value you assign to a variable has a designated “type”. For example, the value "Hello world" is generally of type string or str . The most basic types are often referred to as “primitives” as well since they make up other types. Here are some common types:

  • Boolean or bool — holds the value True or False
  • Various Numbers (integers, floats, doubles, etc.) — hold numeric values
  • Char — holds a single text character value
  • String or str — holds text

Why do we have different types? It boils down to making it easier for the computer to interpret what a value is supposed to be. Remember that at the lowest level, all a computer really understands is binary code, which are all 1’s and 0’s, and in order to make it easier to translate human-readable code to 1’s and 0’s, types exist. Once you’re more comfortable with general types in your programming language, you can learn how to optimize or choose the right variable types (for example, choosing between a signed vs. unsigned integers of 32 vs. 64 bits), but these are problems for a later date!

Some languages are stricter than others in terms of how you use variables and types. In Python and JavaScript, you can declare a variable of a string type and then change it to be an integer type. These type of languages are sometimes referred to as “untyped,” “dynamically typed,” or “weakly typed.”

# Python example
message = "this is a str" # declaring a str
message = 5 # changing str to be an int

This is in contrast with “typed,” “statically typed,” or “strictly typed” languages, such as Go and Java. In these languages, once you declare a variable of a certain type, you can only modify it as a value of the same type.

// Go example 
message := "this is a string"
message = 5 // Will NOT compile or run, Error

Going back to the cup analogy, in a dynamically typed language, you can use the same cup to hold coffee, water, wine, beer, etc,. — it doesn’t matter. In a statically typed language, you can only use a coffee cup to hold coffee, a wine glass to hold wine, beer mug to hold beer, and so on.

Now, there are some variables that we don’t want to ever update; we want to keep the value the same the entire time, or “constant.” These “consts” cannot change value once they’re declared — they are cups that once you pour something in, you can’t pour anything new in or out of the cup. There are different ways of declaring constants in different languages, as shown below.

MESSAGE = "I cannot change" # Python example

const message = "I cannot change"; // JavaScript example

const message = "I cannot change" // Go example

let message = "I cannot change" // Swift example

The last fundamental for variables — scopes — we will talk about once we get to functions.

Operators

What is an operator? An operator is a symbol or character that represents some process or action on some value.

The first operator type we’ve actually already used is called assignment. This is most commonly the = sign. The variable on the left hand side will be “assigned” the value of the right hand side.

# Python code
x = 5

The next type of operator is called an arithmetic operator. These perform your common math operations, such as addition, subtraction, multiplication, exponents, division, and modulo/remainder. This operator type will return a number value.

# Python code
1 + 2 # addition symbol "+" - value is 3
3 - 1 # subtraction symbol "-" - value is 2
3 * 2 # multiplication symbol "+" - value is 6
3 ** 2 # exponent symbol "**" - value is 9
10 / 2 # division symbol "+" - value is 5
13 % 4 # modulo symbol "%" - value is 1

The only new concept you may not have seen before in the above is the modulo symbol. This essentially divides the first number by the second number and returns the integer remainder value (i.e 13 % 1 is 0 since 13 divided by 1 has no remainder, 13 % 2 is 1 since 13 divided by 2 has a remainder of 1, and so on).

In some languages, such as Python, there exists a combination of assignment and arithmetic operators, where you are assigning and doing arithmetic at the same time.

# Python code
x = 5 # assigning to x the value 5
x *= 2 # assigning to x the value of x * 2
# x currently equals 10, since 5 * 2 = 10

x += 1 # assigning to x the value of x + 1
# x currently equals 11, since 10 + 1 = 11

The next type of operator is called a comparison operator. These operators “compare” two values.

# Python code
x = 5
y = 10

x == y # false
x != y # true --> the "!" sign is usually negates statements
x < y # true
x <= y # true
x > y # false
x * 2 >= y # true

The entire statement using a comparison operator (i.e x == y ) is often called a condition. The entire condition will return true or false depending on if the comparison being made is true or false. You can combine these conditions together using logical operators. These are usually thought of as the conjunctions AND and OR, and also the special word NOT.

If we combine two conditions with an AND (usually the && symbol or and ), then they both must be true in order for the combined conditions to return true. If either is false, then the entire condition is false.

If we combine two conditions with an OR (usually the || symbol or or ), then they if either condition is true, the entire condition is true. If both are false, then the entire condition is false.

The NOT operator (usually the ! symbol) is less of a conjunction and more just a modification to a condition. For example, the condition 5 > 3 returns true, but the condition !(5 > 3) returns false because the negation (or opposite) of 5 > 3 is 5 <= 3 , which is false.

The last type of operator that you should at least know exists are called bitwise operators. These operate on values at the bit level. Once you understand more about binary and how values are represented to the computer, you should read this article that does a good job of describing these operators.

Conditionals

We’ve already talked about conditionals, and conditional statements are simply an extension of that. These conditional statements are the backbone of making the computer do a certain thing in certain cases.

The most common conditional statement is and if/else statement. These work very similarly to how they do in English: If it is cold outside, wear a coat. Else, wear a shirt. In this statement, if the condition cold outside is true, we’ll wear a coat, and if the condition is false, we’ll wear a shirt. If this were code, it’d look something like:

# Python code 
if weather == "cold":
print("wear a jacket")
else:
print("wear a shirt")

If/else statements can have multiple parts to it that will check in the order you put them. These parts can be used using else if logic.

# Python code 

if weather == "cold" :
print("wear a jacket")
elif weather == "hot":
print("wear a bathing suit")
else:
print("wear a shirt")

In the above example, first we check if it’s cold. If it is cold, then we wear a jacket. If it is not cold, then we check if it’s hot. If it is hot, then we wear a bathing suit. If all else is not true, then we wear a shirt.

An extremely similar concept to if-else statements is switch statements. I’ve heard these called slightly less intuitively so let’s take a look at the following example that uses an equivalent if/else and switch statement.

// Go code
// If/else statement
if x == 1 {
fmt.Println("x equals 1")
} else if x == 2 {
fmt.Println("x equals 2")
} else {
fmt.Println("x is not 1 or 2")
}

// switch statement
switch x {
case 1:
fmt.Println("x equals 1")
case 2:
fmt.Println("x equals 2")
default:
fmt.Println("x is not 1 or 2")
}

Both of the above ways of evaluating the value of x result in the same behavior. This would generally be considered a good time to use a switch statement over an if/else statement because the conditional statements are looking at a single variable. In most cases, if you can use a switch statement, there is a slight performance boost due to the way the code compiles, which is when your code gets into machine instructions. However, there is no right or wrong way to do something like this, but it’s important to know it exists!

The last type of conditional you should be familiar with is for-loops and, by extension, while-loops (pro-tip, these are also called iterative loops). This last conditional is arguably what allows us to truly benefit from writing code. In for loops, your code will execute “for” a certain number of times. In while loops, your code will execute “while” a certain condition is true. Let’s take a look at the 3 examples: one using no loops, one using a for-loop, and one using a while-loop.

// JavaScript code 

// No loops
let x = 0;
x += 10;
x += 10;
x += 10;
x == 30; // True

// For loop
let x = 0;
for (let i=0; i < 3; i++) {
x += 10
}
x == 30; // True

// While loop
let x = 0;
while x < 30 {
x += 10
}
x == 30; // True

In the no loops example, we manually write out x+=10 3 times, giving us a result of 30. This might seem fine when you only have to write it out 3 times, but what happens if you need to do it 5, 10, 100, or 10000000 times? That would be crazy.

In the for loop, we also do the operation x+=10 3 times. Let’s break down the for loop. The first statement let i=0 is declaring a variable i equal to 0. The second statement i<3 is declaring the condition we will run the for loop under; the for loop will execute while i is less than 3. The last statement i++ is saying that at the end of each loop, we will add one to i . So to recap what is happening from beginning to end on the for loop:

Loop 1: i = 0 (less than 3) -> add 10 to x -> add 1 to i
Loop 2: i = 1 (less than 3) -> add 10 to x -> add 1 to i
Loop 3: i = 2 (less than 3) -> add 10 to x -> add 1 to i
STOP because i = 3 which is not less than 3.

In the while loop, we also do the operation x+=10 3 times. We declare our condition x < 30 , and while that is true, we will continue to loop.

Loop 1: x = 0, which means x < 30 -> add 10 to x
Loop 2: x = 10, which means x < 30 -> add 10 to x
Loop 3: x = 20, which means x < 30 -> add 10 to x
STOP because x = 30, which means x is not less than 30.

When do you want to use for loops versus while loops? Like the switch statements versus if/else statements, there is no right or wrong answer. Generally, you want to use for loops when you know the specific amount of times you want to run the loop, and on the flip side, while loops can be more intuitive when you don’t necessarily know how many loops you need to meet a certain condition.

Functions

What is a function? A function is essentially some sort of defined, packaged piece of code. It essentially allows us to repeat our code multiple times without having to rewrite it when we want it to execute.

Let’s say you want to do the following operation each time you get a value x to get a y : y = x^2 + 20x — 15 . You could do the following:

// JavaScript code 
const X1 = 1;
const X2 = 2;
const X3 = 3;

const Y1 = X1**2 + 20*X1 - 15;
const Y2 = X2**2 + 20*X2 - 15;
const Y3 = X3**2 + 20*X3 - 15;

Again, imagine you had to do this 100 times. Each time you write it, you could make a mistake, and it also wastes a bunch of time. You could replace this with a function.

// JavaScript code 
function ourFunction(x) {
return x**2 + 20*x - 15;
}

const X1 = 1;
const X2 = 2;
const X3 = 3;

const Y1 = ourFunction(X1);
const Y2 = ourFunction(X2);
const Y3 = ourFunction(X3);

Now, we’ve declared a function ourFunction that takes in a parameter x , does our x^2 + 20x — 15 math, and then returns that value. It makes it very easy to do it to any value we want without having to rewrite it. That’s the basics of functions.

In the above case, since we’re returning a value, we can call it a value-returning function. If we don’t return a value, then we call those void functions, or non-value returning functions. For example:

// JavaScript code
function printValue(x) {
const y = x**2 + 20*x - 15;
console.log(y);
}

const y = printValue(1);
y == undefined; // true!

Now, if we call printValue , we do the same math, but now we just print out the value and don’t return anything. We would see in the console 5 printed out, and y == undefined is true since printValue(1) doesn’t return a value.

In the above cases, we gave our function some parameter x . This is a parameter that the function uses to do some behavior. You can list as many as you want for your function, or none at all!

Next, we can have something called an anonymous function. This is a more advanced topic, but essentially these are functions that don’t have names. The proper usecase for these is highly debated (check out this stackoverflow article haha), but generally, these can be useful to do some operation once.

The last topic for functions is scopes! This is also slightly a more advanced topic, and the rules slightly vary from language to language, but this might cause a headache when you’re first starting. Generally speaking, variables are scoped to wherever they’re declared. If they are declared outside of any functions, they are called global variables, and can be accessed from wherever in the code. If a variable is declared within a function, they are generally called local variables and CANNOT be accessed outside of the scope they were declared in (for most programming languages).

# Python code
x = 10 # global variable


def printFunc: # this is how you declare a function in python
y = 10 # local variable
print(y+x)

printFunc() # prints out 20

print(y+x) # ERROR ERROR ERROR

In the above code, you’ll get an error on the print(y+x) line because at that point, y is undefined as it was only defined inside the printFunc function. Double check the scope of your variables in case you get this issue!

Common Structures

Everything we’ve done so far has been using only primitive types, but hat are some of the most common data structures you can use to enhance your programming? Arrays and maps! There are many others (see this article if you’re feeling adventurous) , but if you’re just getting started, these two will help start to make things easier.

You can think of arrays as a list of values held together in a fixed-length structure. In most programming languages, the values in an array are of the same type (i.e all integers, all floats, all strings), with JavaScript being a notable exception. Arrays are synonymous with open and closed square brackets [] .

In the above functions example, I declared the x values individually.

// Previous code without array. 
const X1 = 1;
const X2 = 2;
const X3 = 3;

// Using array
const xValues = [1, 2, 3];

Now, we have our x values grouped together. How am I supposed to now access the value 1 ? Arrays are indexable which means they are indexed with their position in the array. Most programming languages are zero-indexed, which means the first element in the array is at position 0. So in the above case xValues[0] == 1 , xValues[1] == 2 , and xValues[2] == 3 . Different programming languages have different functionalities on how they treat/call arrays, but most of them share similarities in how to get the length of an array, indexing, adding/removing elements to an array, finding elements within an array, and iterating through an array.

Maps (also called dictionaries) are a different type of data structure found in most programming languages. Dictionaries are a good way of thinking about them. In maps, you can have some key that maps to some value. Like a dictionary — where the key would be a word, and the value would be the definition. Maps are very useful when there is more semantic meaning rather than indexing via arrays.

# Python code
ourDict = {
"author": "Roberto",
"website": "Medium",
}

print(ourDict["author"]) # Outputs "Roberto"
print(ourDict["website"]) # Outputs "Medium"
print(ourDict["random"]) # ERROR!

Now, you don’t have to group everything into an array or a map, but they can be useful when you need them!

Summary

These are 5 concepts I think are very important and will allow you to do many cool things with programming. There are many more important concepts to know to become a good programmer — both in terms the theory/concepts of how things actually work (i.e what happens when I go to google.com ? How does my browser know what to do?) and in terms of how to make more useful code (i.e algorithms, more data structures, etc.), but you need to start somewhere!

If you’re looking to learn more information, or hire me for tutoring/contracting services, please consider contacting me at roberto@tidallabs.io, following me on Medium, buying me a coffee, following me on twitter, or connecting with me on LinkedIn!

--

--

Roberto

Stanford alum, Software Engineer with a passion for CyberSec, Biotech, and Sustainability. Work with me at https://www.tidallabs.io/.