What is Programming?

Programming is simply about asking a computer to solve problems that are either too difficult or time consuming. To program successfully, we first need to identify our problem, break it down into simple steps, and finally communicate those steps to the computer in a language that it understands. By doing so, the computer can solve our problems more efficiently and effectively! We will use an example to illustrate this basic idea of programming.

Our first Programming example

Let's imagine that you want to find the biggest number from a list of numbers. If the list only has few numbers, it is a simple task for you to find the largest number. However, if the list gets very big (for example with more than 100, or even 1000 numbers), it will take you much longer to find the biggest number. Thus, the computer is actually a great tool as it can repeatedly execute a simple task with accuracy and efficiency! In order to get the computer to do that, we need to overcome two challenges.

Challenge 1: Breaking the problem into simple steps

The first challenge is that the computer can only execute very simple tasks. It cannot find the biggest number in the list when we command it to "find the biggest number in the list". We will need to create a recipe that will list each tasks it has to execute to solve the problem logically and precisely. This "recipe" is commonly called an algorithm. The main difficulty in writing an algorithm is to break down the complex problem into simple problems that our computer can easily solve. This would be covered in the section Divide & Conquer.

Challenge 2: Coding

The second challenge is that computers are no humans, so they do not understand our language (in this case English). If you want your computer to solve your problem, you need to tell it what to do in its language (such as Python, R, Java, HTML etc). This is where coding comes in. Coding is the action of translating an algorithm from our human language to the computer language.

What is a Program?

Now that we know what is Programming, we can understand what a Program is. A Program is a piece of code (a set of instructions for the computer to execute) that makes the computer solve our problem. Consequently, one can simply associate the term "program" with a programmer's work output.

In our example, our Program will be a piece of code that enables the computer to find the biggest number from any given list of numbers. Given an input (which in our example would be a list of numbers), the programm will solve the problem and give the solution as an output (which in our example would be the biggest number of the list).


Combining Programs

In complex problems, we usually need multiple programs to solve different parts of the problem we need to solve. It is very common to have a program inserted in another program, which helps to solve a bigger problem. Program


An algorithm is a concrete set of rules used to solve a problem. From simple math like adding numbers to more complicated tasks like displaying content based on user input, an algorithm can be used to do just about anything. Algorithms are omnipresent in the programming world—they are a fundamental part of all programs; thus, every programmer needs to have a strong understanding of algorithms.

Creation of an Algorithm

The creation of an algorithm is best done by following several steps:

  1. Understanding the problem:

    • Before any coding is done, the programmer should understand the problem he or she is tackling. This is best done by describing it in an easy to understand, human language.
    • In addition, the programmer should know the inputs and outputs and see the connection between them.
  2. Alternatives:

    • There is often more than one way to solve a programming problem.
    • The programmer should describe the various alternatives and then choose the best.
  3. Coming up with the solution:

    • Having chosen the best alternative, the programmer should then come up with a solution.
    • Helpful tools include diagrams and pseudocode.
    • Pseudocode is code “translated” to a human language. This can be very helpful as it bridges the gap between a human language and a programming language.
  4. Picking a language:

    • Once the programmer has come up with the solution, he or she must pick a language to write the program in.
    • For our purposes, this language will be Python.
  5. Debugging:

    • Having coded the program in a specific programming language, the programmer needs to debug.
    • This means he or she must ensure that everything functions as it is supposed to.
    • Most specifically, the algorithm must do what the programmer wants it to do.
  6. Documenting:

    • Lastly, the programmer should document the program well.
    • This means, for example, that he or she includes many useful comments and uses appropriate variable names.
    • The purpose of this is to make the program readable by any human. Consequently, any programmer can take a look at the program and is able to understand every line of code does.

Example of an Algorithm

Let's go back to our first example and create our own algorithm.


As mentioned above, pseudocode serves to offer a more human friendly version of code. Hence, it is a useful tool to convert a thinking process into a sketch of the structure of the intended computer program so that the time taken to do actual coding can be reduced. This is best done by writing out the code in a way that resembles English as much as possible. An example of this can be seen below.

Find the highest number in a set of numbers.

1. Create **list** = list of numbers 
2. Set a variable, **max**, equal to the first number of **list**
3. for each **number** in the **list**:
4. **if number** is higher than **max**:
5. **max** takes the value of **number**
6. print the **max**

Divide & Conquer Principle

What is this Principle about?

The Divide & Conquer Principle is a versatile method applied universally to solve complex issues (Application fields include Politics and Military). The reason for this is because it is a great method to break down extremely complex issues to bite sized ones. Consequently, one can easily solve the big problem by finding solutions to the bite sized problems and then combining these answers to create a final solution.

The visual below summarises the above explanation of the Principle.

Divide & Conquer

Application of the Principle to Coding

In our example, our "complex problem" is finding the largest number from a given list of numbers. Some thinking is needed to break down the complex problem to simple problems, and smaller problems into even smaller problems... till the problems can be easily solved. Subsequently, we would need to order the set of problems in a logical fashion for the computer to understand. This process is documented in the mindmap below:

Divide & Conquer

^The reason that a function is needed is because we want to use the solution over and over again without having to implement the same coding algorithm as and when we want to utilise it. This cuts down a lot of time.

After this stage, we would need to write a pseudocode. There can be many versions to the pseudocode. Thus, you might find that the earlier psuedocode will differ slightly from the psuedocode shown below:

# Create a function that gives the largest number from the user's list of numbers

Function maximum_number(list_of_numbers): 

    1. set a variable, largest_number, be the 1st number of the list         
    2. for every number in the given list of numbers:                        
    3. if number is higher than largest_number:                            
    4. largest_number takes the value of number                          
    5. print largest_number                                                 

Call out the function using the user's list to give us the final answer:


From the earlier stage, translate your pseudocodes into a coding language that the computer understands (which in our case is Python). Translation of the simplest tasks should be done first:

In [1]:
# Create a function that gives the largest number from the user's list of numbers

def maximum_number(list_of_numbers): # Create a function called maximum_number
    largest_number = list_of_numbers[0] # Create a variable that the list of numbers can be compared to
    for number in list_of_numbers: # Iterate each number in the list
        if number > largest_number: # The computer needs to compare adjacent numbers in the list
            largest_number = number # Replace the previous element with the larger element     
    print("The largest number is: ",largest_number) # Computer prints the result of the largest element.

Now that we have the function, we can call it out anytime for any list of numbers.

In [2]:
# Call out the function using the user's list to give us the final answer

The largest number is:  7
The largest number is:  10

Writing a good program

Three concepts to evaluate programs

  1. Correctness: The solution produces output that we wanted out of it - accuracy.
  2. Design: The implementation of the solution is done in a smart way where there are no redundancies, no copy and paste and steps are explicitly clear.
  3. Style: The code is made easily readable for a 3rd person that is new to the code.

In every programming languages, there is a style guide for the language that proposes a standardized presentation of code so as to make it readable to any user. One style guide for Python would be the PEP 8 (https://www.python.org/dev/peps/pep-0008/). In the following section, we described some of its design and style principles that are recommended.

Four easily implementable practices for writing readable code:

Commenting & Documentation

Code is more often read than written. When we write code, we write it for two primary audiences: our users and our developers (including yourself). Both audiences are equally important. If you’re having a problem reading your own code, imagine what your users or other developers are experiencing when they’re trying to use or contribute to your code.

Comments are created in Python using the pound sign (#) and should be brief statements no longer than a few sentences. Comments though simple can be really useful when you have a long piece of code.

There are generally two ways to comment on a code. The first way is to have block comments that follows your code either before/after the code. The other would be to have a inline comment. Lets have a new example to demostrate how commenting improves readability:

In [3]:
# Define sharks variable as a list of strings
sharks = ['hammerhead', 'great white', 'dogfish', 'frilled', 'bullhead', 'requiem']

# For loop that iterates over sharks list and prints each string item
for shark in sharks:
great white

For more on commenting in Python, see the following: https://www.digitalocean.com/community/tutorials/how-to-write-comments-in-python-3

Consistent Indentation

Indentation refers to the use of tabs and spaces at the start of your code to convey a program structure. In programming languages such as Python, indentation is used to determine the structure instead of using braces or keywords.

Indentation improves the readability of your code. It is used to clarify the link between flow constructs such as conditions or loops, and code contained within and outside of them.You can see at a glance where the end of a code block is, rather than having to read each line until you find it.

Let us demonstrate this example with the same code as before. Note that there are three levels of indentation: (1) under function (2) under the For loop (3) under the If statement. Note that if indentations are not done properly, Python would not be able to recognize the statements and would return an error. Let us take a look at our example:

In [4]:
def maximum_number(list_of_numbers):
#--->Indentation under the function
    largest_number = list_of_numbers[0]
    for number in list_of_numbers:
#------->Indentation under the For loop       
        if number > largest_number:
#----------->Indentation under the If statement
            largest_number = number
#---> Note that this print is just in the function, but outside of the for loop
    print("The largest number is: ", largest_number)

Consistent Naming Schemes

There are a lot of different possible naming styles. Naming styles can be used to distinguish between global/local variables, arguments, functions and classes. More importantly, naming conventions should be consistent throughout your program. This allows the reader to relate to the declared variables. Use function/variable names that are useful for the reader to understand the code rather than using ambigious names.

Limit Line Length

Our eyes are more comfortable when reading tall and narrow columns of text. That is also the reason why newspaper articles have their content in narrow columns. When writing codes, try to avoid writing overly long horizontal lines of code unless there is a special situation where longer lines would improve readability. You could check out the PEP8 style guide for some guidelines on this as well - it proposes some maximum line length under its guidelines.

In [5]:
# Use parentheses or slashes (\) to break strings across multiple lines
my_string = ('This is a very long string, so long that it will not fit into just one line' 
             'so it must be split across multiple lines.')
# or
my_string = 'This is a very long string, so long that it will not fit into just one line ' \
            'so it must be split across multiple lines.'

Example demonstrated on the code presented above

In this example, we have two codes with the same function, written in different ways: one not so good while the other written in a better way. Lets take a look at the two codes written below and notice the differences:

  1. Absence of comments in the first code makes it harder for the the user to understand the block of code. As compared to the second example, we could determine quickly that it is a function that gives the maximum number as its output.
  2. Absence of indentation makes it difficult to differentiate between the flow constructs (Defining a function, For loop, If statement). This can potentially be messy when you have long lines of code and multiple functions/conditionals.

  3. Variable names can be improved instead of using ambiguous names such as "nu", "z" which leaves the user guessing. In the second example, we used names that spell out its own purpose to improve readability.

In [6]:
# ---------------Not so good---------------
def nu(z):
    for n in z:
        if n>lar:
    print("The largest number is: ", lar)
# ---------------Good readable code---------------

# Create a function called maximum_number
def maximum_number(list_of_numbers):
# Create a variable that the list of numbers can be compared to  
    largest_number = list_of_numbers[0]
# Iterate through each number in the list    
    for number in list_of_numbers: 
# Compare adjacent numbers in the list and replace    
        if number > largest_number: 
            largest_number = number 
    print("The largest number is: ", largest_number)