As much as this series is to educate aspiring computer programmers and data scientists of all ages and all backgrounds, it is also a reminder to myself. After playing with computers and numbers for nearly 4 decades, I've also made this to keep in mind how to have fun with computers and maths.
In this third part, we will focus on putting in to practice what we learned in Part 1 and Part 2 of this series. We will focus on understanding what algorithms are, and creating one of our own (for looking for prime numbers). As a reminder from Part 1, prime numbers are the numbers that all other numbers are made of. This also means that any number that is only divisible by 1 or itself, is a prime number. Consequently any number that is divisible by a number other than 1 or itself, is not a prime number.
In a factory there is something that comes in (for example recycled newspapers), there is a process of some sort in between (for example turning newspaper in to pulp and then in to paper), and something that comes out (for example toilet paper). Algoritms are the part in the middle, where some process takes place in order to transform what comes in to what goes out.
Using what we have already learn, let's create a very simple algoritm. One that takes in two numbers, finds out if the first number (left number) we input is divisible by the second number (right number) we input. Algorithms are sometimes called 'algos' and we will be using that shorthand from now on.
def first_algo(left, right): left % right
Now that we have created our function, which contains an algoritm that finds out if the left number is divisible by the right, you might remember that we have to call it to get the output.
How come we did not get a result even we seemingly did everything right? Actually this is an expected behavior of the function, because we are not saying that we want to print something out. The computer has no way to know that we want to print something out. Instead, it silently performs the modulus operation. This is easy to fix by modifying our function slightly.
def first_algo(left, right): print(left % right)
Nice, now it works. Before we move on, let's look at a better way to achieve the same thing. Not always we want to print something, so it's better to use
return at the end of the function. Return just means that there is some kind of thing we want to spit out of the function once its done its job. Unlike
return output can be used as an input for another function. Later you'll learn more about this.
def first_algo(left, right): return left % right
As you see, this behaves exactly like we want it even though we don't use
We could also input much larger numbers.
As you can see, regardless of what numbers we use as input, we always get exactly what is expected; the remant of the modulus. In other words, we always see what is remaining after we divide the left number with the right number. Let's apply some Boolean logic to the our algoritmh.
def second_algo(left, right): return left % right is not 0
One of the most important, and commonly used tools of computer programmers and data scientist are conditional statements. These relate with yet another archaic computer term "flow control". The easiest way to understand conditional statements is to consider a statement such as one like this:
**"I will go to play football if it's not going to rain"**
As you can see, this is two boolean statement in one sentence. There are two options, it will rain or not. If "will rain" is true, then "go play football" is false. This is exactly how conditional statements work in the programming context. As everything else you have learn so far, conditional statements are incredibly intuitive to use in Python. Let's see a simple example.
if 1 is 1: print("hello world")
In this example, we are just saying that if 1 is 1, which we know is true, then print "hello world". We know exactly what to expect, and actually that is a key principle with conditional statements, we should know exactly what to expect when we are writing them! Same works reversely.
if 1 is 2: print("hello world")
We know 1 is not 2, so we also know that there will be no print out.
Let's introduce a common continuation to
else. The best way to understand this, using our previous example, is a statement such as this:
**"I will go to play football if it's not going to rain, otherwise I will play playstation"**
Here we are adding one more component to our conditional statement, that if it's raining and as result we don't go to play football, we'll play playstation instead. Let's see how this looks like in Python:
if 1 is 1: print("hello world") else: print("bye world")
if 1 is 2: print("hello world") else: print("bye world")
At this point, let's put some of the concepts we've learn together in to something just slightly more involving.
def third_algo(left, right): if left % right is 0: return True else: return False
As you can see, we are now returning
True when the remnant is 0 and
False when it's not.
Before moving on the nex section, where we will cover generating numbers, let's consider a conditional statement with one more clause.
**"I will go to play football if it's not going to rain at all, and if it rains lightly I will go for a walk still, otherwise I will play playstations"**
Now we have a case where how heavy the rain is effects the oucome. If it's not raining at all, we go play football, if it's raining a little we go for a walk, but otherwise we'll play playstation. For this we're going to again modify our function.
Now we're going to add
elif clause, which is just another way to say if between if and else. We will also introduce the idea of comments, where inside our function we use human language to explain what parts of code do. Anything that starts with
# is consider a comment in Python. It means that part of the code will not be excecuted together with others. In other words, comments do not effect the workings or output of the function in anyway.
def third_algo(left, right): # it will not rain if left % right is 0: return 'Play football' # it will rain a little elif left % right is 1: return 'Go for a walk' # it will rain heavily else: return 'Play playstation'
'Go for a walk'
In this example we decide if we will play football or not. If the output is 0, it means there is no rain and we go play, and output is True. If it rains a little, we go to walk instead and output is False, and if it's more than 0, we play playstation and output is also False. That's it, you now understand conditional statements which is not just a key concept in Python language, but is the primary means we use in order to instruct computers and tell them what we want them to do.
In the next episode we will continue building on what we've learned here and you're going to build simple but far more powerful algoritms with your new skills.
ifalone can be used to create a conditional statement
We've made great progress! Time to wrap up for now, and then in the next part we get in to the real action, looking for prime numbers! With the skills you're learn so far, you're doing a lot of the things the day-to-day of advanced programmers and data scientists is made of.