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!
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.
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!
Congratulations, now you know how to write a simple program in Python language! Yet much remains to learn, so let's move on :)
Let's start with some basic numerical operations.
1 + 1
2 * 2
4 / 2
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 :)
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).
1, 2, 5, 6, 123, 345
0.1, 0.00004, 0.234
Let's now try a few examples to highlight this concept.
2 + 3
That was an integer (int).
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.
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.
From the 'hello world' example you might remember that we could deal with text by enclosing it inside quotes:
Single quotes work as well:
When something is enclosed in quotes, either double (") or single (') python consider it as a third data type called string (str for short).
Unlike with number (float and int) types, mathematical operations can't be performed with strings. Instead, two strings will be joined together.
'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!
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.
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.
def my_improved_function(data): print(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...
--------------------------------------------------------------------------- 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.
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.
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.
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...