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.


In this demo, we'll learn about a few different things.

First and foremost, we're going to learn about an inceredibly fun and easy to learn programming language called python. It is the first choice of data scientists, hackers and even international spies! Most importantly, as I had mentioned, it's a lot of fun to do things with python.

The second thing we're going to learn about, is Jupyter Notebooks, which is this platform we're using right now. In the past most programming was done from the command line...

This can be quite intimidating, especially for someone without experience in working with such an interface! The other common tool for programmers of the past was a simple text editor, which can be even more intimidating for beginners...

What Jupyter notebooks do, is put together the command line and text editor in a way that is easy to manage for anyone, even people without any experience with programming.

In addition to Python language, and Jupyter notebook, we're going to learn about some very interesting mathematical concepts, namely prime numbers.

Prime numbers are the numbers that all other numbers are made of. This 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. Finding prime numbers, particularly very large ones, is one of the great mysteries of mathematics. This problem, finding prime numbers, also presents one of the best ways to learn about the magic of numerical computing in an interesting and fun way! Now let's go do it!

PART 1 : Introduction to Python

Python language is very powerful tool for creating any kind of computer program, but today we're going to focus solely on how to perform some very basic techniques and ideas found in all computer programming languages.

1.1 Hello World

Throughout the ages, computer programmers have preferred to start learning a new language by creating a short program that says "hello world". So we're also going to start with this classic example before moving on to math stuffs!

In [1]:
print("hello world")
hello world
In [2]:

Congratulations, now you know how to write a simple program in Python language! Yet much remains to learn, so let's move on :)

1.2. Mathemical Operations

Let's start with some basic numerical operations.

In [3]:
1 + 1
In [4]:
2 * 2
In [5]:
4 / 2
In [6]:
3 - 2

It's that easy!

As you can see, it could not be easier to perform common mathematical operations. In fact, it works exactly the same as how we would do it using pen and paper! Python language is like that, it's very intuitive and easy to learn :)

1.3. Data types

This next concept is going to seem a little bit strange first, but is very easy to understand. You just have to aknowledge that computers deal with information differently from the way we human do. To understand this, we will now briefly go over the idea of 'type'. Most importantly, two different numerical data types; float (decimal numbers) and int (whole numbers).

examples of ints

1, 2, 5, 6, 123, 345

examples of floats

0.1, 0.00004, 0.234

Let's now try a few examples to highlight this concept.

In [7]:
2 + 3

That was an integer (int).

In [8]:
2 / 3

And that was a floating point value (float). We'll learn more about different types later, now it's enough to understand that computer programs typically involve such a thing as type.

1.4. Functions

The third concept we're going to learn is called a 'function'. Just like its name suggest, it is a utility that performs a function. For example, there is a function that allows us to identify the type of data (the same type we just went through in the previous examples). Actually we already used a function...the print() function for doing our "hello world" example in the beginning.

In [9]:
In [10]:
In [11]:

From the 'hello world' example you might remember that we could deal with text by enclosing it inside quotes:

In [12]:
print("hello world")
hello world

Single quotes work as well:

In [13]:
print('hello world')
hello world

When something is enclosed in quotes, either double (") or single (') python consider it as a third data type called string (str for short).

In [18]:

Unlike with number (float and int) types, mathematical operations can't be performed with strings. Instead, two strings will be joined together.

In [19]:
'3' + '3'

Calling functions is as easy as that, and there are a lot of them. This means that you can do a lot of different exciting and useful things very simply just by calling a function you want to use in a way we had just done. You can even make your own functions!

In [28]:
def my_first_function():
    print("hello world") 

Before trying it out, let's briefly overview what's happening here. First we declare our own function with def which just means we're going to define a function next. Then we have the name of our function my_first_function following it, and parenthesis with semicolon following it. That's it! Now let's see how to use our function.

In [29]:
hello world

As you might have guessed it, we use it just like we use print() and type() functions in the above examples. Also you might have found one difference, we are not making any input inside the parenthesis. This leads us to another basic building block of understanding computer programming; there are functions, and inputs to those functions.

The function is what happens, a process of some sort, and the input is what the process happens to. Let's modify our function slightly to learn more about this.

In [30]:
def my_improved_function(data):

As you can see, we have slightly changed the way the function is defined. Now instead of having empty parenthesis following the name of the function, we are declaring data in there. This way data becomes a 'parameter' also called 'argument', which is really just a fancy word for somethign we input to the function, so the function can process it. Let's try it first...

In [31]:
TypeError                                 Traceback (most recent call last)
<ipython-input-31-c33741cfbfdb> in <module>
----> 1 my_improved_function()

TypeError: my_improved_function() missing 1 required positional argument: 'data'

Ah, our first error message. When something is wrong, Python is going to tell us exactly what is wrong. In this case, the error is telling us that even though our function is expecting to receive one argument (data), it is not getting it. Let's give our function an input and see what happens.

In [32]:
my_improved_function("hello world")
hello world

That's better. This makes the function quite a bit more powerful than the first version that just printed one thing every time. In fact, now we can print anything we like through this same function.

In [33]:
my_improved_function("My name is Mikko and I love to make computer programs with Python")
My name is Mikko and I love to make computer programs with Python

Sweet. Let's stop learning the basic concepts here and put it in to practice with something interesting in the next episode. But first, let's summarize the learnings of this introductory section.

Part 1 Summary

  • print() is the command to print something on the screen
  • Mathematical operations are very easy to perform in Python
  • Python deals with numbers based on data types
  • In Python there are two numerical data types; int and float
  • String data type can't be used for mathematical operations
  • Functions are powerful tools for easily performing operations
  • Functions may accept arguments (parameters) as input
  • Functions are computer processes, and arguments are what is being processed
  • It's very easy to create your own functions

That's it for the introduction. Actually, you've learn far more than you realize at this point. Even if you became the best programmer in the world, these ideas would be some of the key building blocks you'd be using again and again. Great job getting this far!

We did not quite get to prime numbers yet, so we'll cover that in the next part...