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.
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.
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.
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.
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).
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.
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.
The creation of an algorithm is best done by following several steps:
Understanding the problem:
Coming up with the solution:
Picking a language:
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**
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.
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:
^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: maximum_number(list_of_numbers)
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:
# 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 # 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.
# Call out the function using the user's list to give us the final answer maximum_number([2,4,6,7,5,3]) maximum_number([1,2,3,4,5,6,7,8,9,10])
The largest number is: 7 The largest number is: 10
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.
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:
# 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: print(shark)
hammerhead great white dogfish frilled bullhead requiem
For more on commenting in Python, see the following: https://www.digitalocean.com/community/tutorials/how-to-write-comments-in-python-3
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:
def maximum_number(list_of_numbers): #--->Indentation under the function largest_number = list_of_numbers 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)
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.
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.
# 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.'
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:
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.
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.
# ---------------Not so good--------------- def nu(z): lar=z for n in z: if n>lar: lar=n 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 # 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)