{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# NUMERICAL COMPUTING IS FUN" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A guide to principles of computer science and numerical computing for all ages" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "------------------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# OVERVIEW OF PART 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this third part, we will focus on putting in to practice what we learned in [Part 1](https://nbviewer.jupyter.org/github/mikkokotila/jupyter4kids/blob/master/notebooks/numerical-computing-is-fun-1.ipynb) and [Part 2](https://nbviewer.jupyter.org/github/mikkokotila/jupyter4kids/blob/master/notebooks/numerical-computing-is-fun-2.ipynb) 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](https://nbviewer.jupyter.org/github/mikkokotila/jupyter4kids/blob/master/notebooks/numerical-computing-is-fun-1.ipynb), 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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# PART 3 : A Life of an Algorithm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3.1. Creating a Simple Algoritm" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def first_algo(left, right):\n", " \n", " left % right" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "first_algo(7, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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. " ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def first_algo(left, right):\n", " \n", " print(left % right)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n" ] } ], "source": [ "first_algo(8, 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def first_algo(left, right):\n", " \n", " return left % right" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "first_algo(8, 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "first_algo(5, 2)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "first_algo(15, 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We could also input much larger numbers." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "120990" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "first_algo(1523434234234, 234323)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "136878" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "first_algo(32942098, 234323)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "def second_algo(left, right):\n", " \n", " return left % right is not 0" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "second_algo(8, 4)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "second_algo(30, 2)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "second_algo(7, 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3.2. Conditional Statements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**\"I will go to play football if it's not going to rain\"**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello world\n" ] } ], "source": [ "if 1 is 1: \n", " \n", " print(\"hello world\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "if 1 is 2: \n", " \n", " print(\"hello world\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We know 1 is not 2, so we also know that there will be no print out. \n", "\n", "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: " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**\"I will go to play football if it's not going to rain, otherwise I will play playstation\"**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello world\n" ] } ], "source": [ "if 1 is 1: \n", " \n", " print(\"hello world\")\n", " \n", "else: \n", " \n", " print(\"bye world\")" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bye world\n" ] } ], "source": [ "if 1 is 2: \n", " \n", " print(\"hello world\")\n", " \n", "else: \n", " \n", " print(\"bye world\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "At this point, let's put some of the concepts we've learn together in to something just slightly more involving. " ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "def third_algo(left, right):\n", " \n", " if left % right is 0:\n", " return True\n", " \n", " else: \n", " return False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, we are now returning True when the remnant is 0 and False when it's not." ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "third_algo(8, 2)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "third_algo(8, 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before moving on the nex section, where we will cover generating numbers, let's consider a conditional statement with one more clause. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**\"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\"**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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. \n", "\n", "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." ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "def third_algo(left, right):\n", " \n", " # it will not rain\n", " if left % right is 0:\n", " return 'Play football'\n", " \n", " # it will rain a little\n", " elif left % right is 1: \n", " return 'Go for a walk'\n", " \n", " # it will rain heavily\n", " else: \n", " return 'Play playstation'" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Play football'" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "third_algo(8, 2)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Go for a walk'" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "third_algo(9, 4)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Play playstation'" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "third_algo(25, 11)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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. \n", "\n", "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Part 3 Summary\n", "\n", "- Algoritms are like insides of factories \n", "- Algoritms process inputs to produce outputs\n", "- Conditional statements are a tool for putting boolean logic in to action\n", "- Conditional statements are part of \"flow control\"\n", "- Flow controls give us the ability to create rules for computer programs\n", "- The three conditional statements in Python are if, else and elif\n", "- Even just if alone can be used to create a conditional statement\n", "\n", "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.6" } }, "nbformat": 4, "nbformat_minor": 1 }