NUMERICAL COMPUTING IS FUN

A guide to principles of computer science and numerical computing for all ages

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.


OVERVIEW OF PART 3

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.

PART 3 : A Life of an Algorithm

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.

3.1. Creating a Simple Algoritm

In [3]:
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.

In [4]:
first_algo(7, 2)

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.

In [8]:
def first_algo(left, right):
    
    print(left % right)
In [9]:
first_algo(8, 4)
0

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 print which just prints something on the screen, return output can be used as an input for another function. Later you'll learn more about this.

In [10]:
def first_algo(left, right):
    
    return left % right
In [11]:
first_algo(8, 4)
Out[11]:
0

As you see, this behaves exactly like we want it even though we don't use print anymore. Keep this in mind, it's one of the most commonly used features in Python programming. Let's run through a few examples of how we could use our function / and the algorithm inside it.

In [12]:
first_algo(5, 2)
Out[12]:
1
In [13]:
first_algo(15, 3)
Out[13]:
0

We could also input much larger numbers.

In [14]:
first_algo(1523434234234, 234323)
Out[14]:
120990
In [15]:
first_algo(32942098, 234323)
Out[15]:
136878

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.

In [19]:
def second_algo(left, right):
    
    return left % right is not 0
In [29]:
second_algo(8, 4)
Out[29]:
False
In [30]:
second_algo(30, 2)
Out[30]:
False
In [31]:
second_algo(7, 5)
Out[31]:
True

3.2. Conditional Statements

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.

In [32]:
if 1 is 1: 
    
    print("hello world")
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.

In [33]:
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 if called 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:

In [34]:
if 1 is 1: 
    
    print("hello world")
    
else: 
    
    print("bye world")
hello world
In [35]:
if 1 is 2: 
    
    print("hello world")
    
else: 
    
    print("bye world")
bye world

At this point, let's put some of the concepts we've learn together in to something just slightly more involving.

In [36]:
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.

In [39]:
third_algo(8, 2)
Out[39]:
True
In [40]:
third_algo(8, 3)
Out[40]:
False

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.

In [44]:
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'
In [52]:
third_algo(8, 2)
Out[52]:
'Play football'
In [49]:
third_algo(9, 4)
Out[49]:
'Go for a walk'
In [48]:
third_algo(25, 11)
Out[48]:
'Play playstation'

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.


Part 3 Summary

  • Algoritms are like insides of factories
  • Algoritms process inputs to produce outputs
  • Conditional statements are a tool for putting boolean logic in to action
  • Conditional statements are part of "flow control"
  • Flow controls give us the ability to create rules for computer programs
  • The three conditional statements in Python are if, else and elif
  • Even just if alone 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.