From a7c77f7c6b2f39b1c56c8ece528beb2b31285da0 Mon Sep 17 00:00:00 2001
From: Scott <32791534+Doc-Scott@users.noreply.github.com>
Date: Sun, 8 Jul 2018 15:24:58 +0100
Subject: [PATCH 1/3] Add files via upload
mixed print statements (Py3 to Py2) all changed to Py3
---
numerical-computing-is-fun-3.ipynb | 1442 ++++++++++++++++++++++++++++
1 file changed, 1442 insertions(+)
create mode 100644 numerical-computing-is-fun-3.ipynb
diff --git a/numerical-computing-is-fun-3.ipynb b/numerical-computing-is-fun-3.ipynb
new file mode 100644
index 0000000..dbd2649
--- /dev/null
+++ b/numerical-computing-is-fun-3.ipynb
@@ -0,0 +1,1442 @@
+{
+ "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, after playing with computers and numbers for nearly 4 decares, I've also made this as a reminder for myself on 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 second third, 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 we input is divisible by the second 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": 1,
+ "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": 2,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "first_algo(8, 4)"
+ ]
+ },
+ {
+ "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 explicitly stating that we want to print something out. This is easy to fix by modifying our function slightly. "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def first_algo(left, right):\n",
+ " \n",
+ " print(left % right)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "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, later you'll learn why, 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."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def first_algo(left, right):\n",
+ " \n",
+ " return left % right"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0"
+ ]
+ },
+ "execution_count": 6,
+ "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": 7,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "1"
+ ]
+ },
+ "execution_count": 7,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "first_algo(5,2)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0"
+ ]
+ },
+ "execution_count": 8,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "first_algo(15,3)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "120990"
+ ]
+ },
+ "execution_count": 9,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "first_algo(1523434234234, 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 input with the right input. Let's apply some Boolean logic to the our algoritmh."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def second_algo(left, right):\n",
+ " \n",
+ " return left % right is 0"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 11,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "second_algo(8, 4)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 12,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "second_algo(30, 2)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "False"
+ ]
+ },
+ "execution_count": 13,
+ "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. 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 a typical boolean statement. 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 learn so far, conditional statements are incredibly intuitive to use in Python. Let's see a simple example."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "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": 15,
+ "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. Let's introduce a common continuation to this, 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 playstations\"**"
+ ]
+ },
+ {
+ "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": 16,
+ "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": 17,
+ "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": 18,
+ "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": "code",
+ "execution_count": 19,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 19,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "third_algo(8, 2)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 20,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "False"
+ ]
+ },
+ "execution_count": 20,
+ "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. 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 output of the function in anyway."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 21,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def third_algo(left, right):\n",
+ " \n",
+ " # it will not rain\n",
+ " if left % right is 0:\n",
+ " return True\n",
+ " \n",
+ " # it will rain a little\n",
+ " elif left % right is 1: \n",
+ " return \n",
+ " \n",
+ " # it will rain heavily\n",
+ " else: \n",
+ " return False"
+ ]
+ },
+ {
+ "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."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### 3.3. Generating Numbers"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Before moving on to the final section of this part, which will cover loops, another important basic building block of algoritms, let's have some straighforward computing fun by generating numbers. This will become handy soon, so we don't have to key in the many numbers we want to check in terms of if they are prime or not. To do this, we will use a function called range()."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 22,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "range(0, 3)"
+ ]
+ },
+ "execution_count": 22,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "range(3)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 23,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "range(0, 10)"
+ ]
+ },
+ "execution_count": 23,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "range(10)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 24,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "range(10, 20)"
+ ]
+ },
+ "execution_count": 24,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "range(10, 20)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "In the first two examples, we are simply telling the range() function to generate numbers starting from zero. In the third example we are giving also a second input, and now get numbers between the two inputs. Let's see a few more examples to make sure this is clear."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 25,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "range(25, 28)"
+ ]
+ },
+ "execution_count": 25,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "range(25, 28)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 26,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "range(11, 18)"
+ ]
+ },
+ "execution_count": 26,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "range(11, 18)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "We can also add a 'step' argument, which gives us even more control over the range of numbers we want to create. For example with step argument 2, we will get every other number in a range:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 27,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "range(2, 20, 2)"
+ ]
+ },
+ "execution_count": 27,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "range(2,20,2)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "This way we only get the even numbers between 2 and 2. Let's try the same for odd numbers."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 28,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "range(1, 20, 2)"
+ ]
+ },
+ "execution_count": 28,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "range(1,20,2)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "There are many other ways you can use to create numbers, including random numbers, but this will be more than enough for what we want to do. Let's move on to the next section and learn about loops."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### 3.4. Using Loops to Automate Things"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Even though our previous algo examples technically speaking are algoritms, it's rare that an algoritm is used without introducing at least some kind of a loop as part of the system. You guessed it, loops are just as easy and intuitive to use in Python language as everything else we've learn so far. For now, we're going to focus on the most commonly used type of loop, a 'for' loop. A for loop is a way to say that some process will go on *for* as long as something is true. Consider this example: "
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "**\"I will play football as long as 5 goals are scored\"**"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "To do this, we can use a very simple loop which keeps going for a number of rounds. In other words, for a range of numbers."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 30,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "0\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "4\n"
+ ]
+ }
+ ],
+ "source": [
+ "for number in range(5):\n",
+ " \n",
+ " print (number)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "That's, there is nothing more to it. You've learn yet another fundamental building block of data science. Note how the numbers in the range start from 0 unless we tell range() to start from somewhere else. In Python language and most other programming languages, numbers start from 0 and not 1. Let's put this together with our algo and also introduce one more new concept (I promise it's the last one for a while), storing something on to a variable. Storing something in to the computer memory is nothing like storing something in to human memory."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Even though we talk about storing something in to the memory, it'a also nothing like storing something in to a safety deposit box where things will be kept for a long time. Computer memory is a temporary storage for something we're going to use as part of our computer program. Let's see some examples."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 31,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "answer = 1 is 1"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 32,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 32,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "answer"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Instead of getting the answer straight away, we store it in memory so we can access it later. This is useful when we want to use the same value many times. For example, we could use this approach to simplify the most recent version of our algo. Take a note below how we are using the same operation twice:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 33,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def third_algo(left, right):\n",
+ " \n",
+ " # it will not rain\n",
+ " if left % right is 0: # < -- here first time\n",
+ " return True\n",
+ " \n",
+ " # it will rain a little\n",
+ " elif left % right is 1: # < -- here second time\n",
+ " return \n",
+ " \n",
+ " # it will rain heavily\n",
+ " else: \n",
+ " return False"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Now let's make a slight simplifcation by storing the operation we do twice in to memory first."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 34,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def fourth_algo(left, right):\n",
+ " \n",
+ " stored_value = left % right\n",
+ " \n",
+ " # it will not rain\n",
+ " if stored_value is 0:\n",
+ " return True\n",
+ " \n",
+ " # it will rain a little\n",
+ " elif stored_value is 1:\n",
+ " return \n",
+ " \n",
+ " # it will rain heavily\n",
+ " else: \n",
+ " return False"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Before wrapping up for now, let's put all that we've learn together in one example."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### 3.5. Putting it All Together"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 36,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "True\n",
+ "True\n",
+ "True\n",
+ "True\n",
+ "True\n",
+ "True\n",
+ "True\n",
+ "True\n",
+ "True\n"
+ ]
+ }
+ ],
+ "source": [
+ "# first we create a range of numbers\n",
+ "numbers = range(1,10)\n",
+ "\n",
+ "# then we create a loop\n",
+ "for number in numbers: \n",
+ "\n",
+ " # then we perform the modulus operation \n",
+ " result = number % number\n",
+ " \n",
+ " # then we create a conditional statement for cases when it's true\n",
+ " if result is 0: \n",
+ " \n",
+ " print (True)\n",
+ " \n",
+ " # and finish with else for cases when it's false \n",
+ " else: \n",
+ "\n",
+ " print (False)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Obviously we are getting True as result everytime because we are always having both the right number and the left number the same (for example 8 % 8). Let's make a slight modification to take us step closer to something that will help us a great deal in finding prime numbers later. This time I'm removing the comments to keep the code neat."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 37,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "True\n",
+ "True\n",
+ "False\n",
+ "True\n",
+ "True\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n"
+ ]
+ }
+ ],
+ "source": [
+ "left = 20\n",
+ "right_numbers = range(1,10)\n",
+ "\n",
+ "for right in numbers: \n",
+ " result = left % right\n",
+ " \n",
+ " if result is 0: \n",
+ " print (True)\n",
+ " \n",
+ " else: \n",
+ " print (False)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "So what we are doing now, is fixing the left number to be 20, and then checking it against every number in the range of 1 to 10 and see if it's divisible. This makes checking if a number is prime a whole lot simpler! Let's try an example where we know it's a prime nubmer, for example 13 (it's not divisisble by any other number than 1 and itself)."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 38,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "True\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n"
+ ]
+ }
+ ],
+ "source": [
+ "left = 13 # <-- changed\n",
+ "right_numbers = range(1,12) # <-- changed\n",
+ "\n",
+ "for right in numbers: \n",
+ " result = left % right\n",
+ " \n",
+ " if result is 0: \n",
+ " print (True)\n",
+ " \n",
+ " else: \n",
+ " print (False)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Because we are starting our range from 1, one get one True in the beginning, so we have to start the range from 2 instead to get the right answer. As you can see, I changed the second line so that we scan until 12 which is the last number before 13. Let's put this inside a function as our fifth algo version and make the range start from 2 instead of 1."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 39,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def fifth_algo(left, right): \n",
+ "\n",
+ " right_numbers = range(2, right) # <-- changed\n",
+ "\n",
+ " for right in right_numbers: # <-- changed\n",
+ " result = left % right\n",
+ "\n",
+ " if result is 0: \n",
+ " print (True)\n",
+ "\n",
+ " else: \n",
+ " print (False)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Now things are starting to look good. We could now remove 'left' variable entirely as it comes as an argument from the function, and also instead of having to modify the function for the last number of the range, we also input that as an argument."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 40,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n"
+ ]
+ }
+ ],
+ "source": [
+ "fifth_algo(7,6)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "That's it, we're prime number checking now! :) Because the result is False for all, we know for sure that our input, in this case 7, is a prime. There is one more very small change we can do using the skill we've already learn to make a nice improvement to what we already have. Instead of requiring the user to input the end of the range, we can automatically compute it as it's always the last number before left. In other words, it's left - 1."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 43,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def sixth_algo(left): # <-- changed\n",
+ "\n",
+ " right_numbers = range(2, left - 1) # <-- changed\n",
+ "\n",
+ " for right in right_numbers:\n",
+ " result = left % right\n",
+ "\n",
+ " if result is 0: \n",
+ " print (True)\n",
+ "\n",
+ " else: \n",
+ " print (False)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 44,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "False\n",
+ "True\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n"
+ ]
+ }
+ ],
+ "source": [
+ "sixth_algo(9)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 45,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n"
+ ]
+ }
+ ],
+ "source": [
+ "sixth_algo(11)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 46,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n",
+ "False\n"
+ ]
+ }
+ ],
+ "source": [
+ "sixth_algo(19)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Things are working real nicely now. But clearly we will later have a problem with larger numbers with this current approach, as if we input 1,000, we will have 1,000 True or False values printed on the screen. To overcome this, we can make a small change to our latest version."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 47,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def seventh_algo(left):\n",
+ "\n",
+ " right_numbers = range(2, left - 1)\n",
+ " output = 0 # <-- changed\n",
+ " \n",
+ " for right in right_numbers:\n",
+ " result = left % right\n",
+ "\n",
+ " if result is 0: \n",
+ " output += 1 # <-- changed\n",
+ "\n",
+ " else: \n",
+ " output += 0 # <-- changed\n",
+ " \n",
+ " return output # <-- changed"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "What we are doing, is first we declare a variable 'output' with starting value 0. Then instead of printing out True, we silently add 1 to output, and in case of False we add 0. Only in the end we print the value out, with the return statement that is outside of the for loop (note how it's indentation is equal to the for statement, meaning it will be processed only once the for loop has completed its job)."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 48,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0"
+ ]
+ },
+ "execution_count": 48,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "seventh_algo(19)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Nice. Now we can key in much larger numbers, and just get one output."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 49,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0"
+ ]
+ },
+ "execution_count": 49,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "seventh_algo(127)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Before wrapping up, let's simplify our code slightly and instead of outputting a number, output a True or False statement. True for 'it's a prime' and False for 'it's not a prime'."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 50,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def eight_algo(left):\n",
+ "\n",
+ " right_numbers = range(2, left - 1)\n",
+ " output = 0\n",
+ " \n",
+ " for right in right_numbers:\n",
+ " result = left % right\n",
+ "\n",
+ " if result is 0: \n",
+ " output += 1\n",
+ " \n",
+ " return output is 0 # <-- changed"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 51,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 51,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "eight_algo(19)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 52,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 52,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "eight_algo(127)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 53,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "False"
+ ]
+ },
+ "execution_count": 53,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "eight_algo(12)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Note how we removed the else statements entirely. Because we are doing nothing in the cases where the left number is not divisible by the right number. In other words, whenever the product of the modulus operation is not zero, we do nothing. Therefore it's enough to just have the if statement without the else. This is quite common. "
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Part 3 Summary\n",
+ "\n",
+ "- Algoritms are like insides of factories \n",
+ "- Algoritms take inputs and give outputs\n",
+ "- Conditional statements are a tool for putting boolean logic in to action\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",
+ "- With small changes to our code, we can make big improvements in capability\n",
+ "- It's very convinient to store values in to memory\n",
+ "- Computer memory is nothing like human memory, and also not like a safe deposit box\n",
+ "- Any value can be stored in to memory \n",
+ "- Numbers can be automatically generated with range() function\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": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "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.5.4"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 1
+}
From 29e375cb476d1398ca3eaefe91e6488e7a809a4f Mon Sep 17 00:00:00 2001
From: Scott <32791534+Doc-Scott@users.noreply.github.com>
Date: Sun, 8 Jul 2018 15:26:19 +0100
Subject: [PATCH 2/3] Add files via upload
mixed print statements Py2 to (Py3) all changed to print()
---
notebooks/numerical-computing-is-fun-3.ipynb | 255 +++++++++----------
1 file changed, 116 insertions(+), 139 deletions(-)
diff --git a/notebooks/numerical-computing-is-fun-3.ipynb b/notebooks/numerical-computing-is-fun-3.ipynb
index 665cfd5..dbd2649 100644
--- a/notebooks/numerical-computing-is-fun-3.ipynb
+++ b/notebooks/numerical-computing-is-fun-3.ipynb
@@ -79,10 +79,8 @@
},
{
"cell_type": "code",
- "execution_count": 148,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 1,
+ "metadata": {},
"outputs": [],
"source": [
"def first_algo(left, right):\n",
@@ -99,10 +97,8 @@
},
{
"cell_type": "code",
- "execution_count": 149,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 2,
+ "metadata": {},
"outputs": [],
"source": [
"first_algo(8, 4)"
@@ -117,10 +113,8 @@
},
{
"cell_type": "code",
- "execution_count": 150,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 3,
+ "metadata": {},
"outputs": [],
"source": [
"def first_algo(left, right):\n",
@@ -130,7 +124,7 @@
},
{
"cell_type": "code",
- "execution_count": 151,
+ "execution_count": 4,
"metadata": {},
"outputs": [
{
@@ -154,10 +148,8 @@
},
{
"cell_type": "code",
- "execution_count": 152,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 5,
+ "metadata": {},
"outputs": [],
"source": [
"def first_algo(left, right):\n",
@@ -167,7 +159,7 @@
},
{
"cell_type": "code",
- "execution_count": 153,
+ "execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -176,7 +168,7 @@
"0"
]
},
- "execution_count": 153,
+ "execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
@@ -194,7 +186,7 @@
},
{
"cell_type": "code",
- "execution_count": 154,
+ "execution_count": 7,
"metadata": {},
"outputs": [
{
@@ -203,7 +195,7 @@
"1"
]
},
- "execution_count": 154,
+ "execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
@@ -214,7 +206,7 @@
},
{
"cell_type": "code",
- "execution_count": 155,
+ "execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -223,7 +215,7 @@
"0"
]
},
- "execution_count": 155,
+ "execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
@@ -234,7 +226,7 @@
},
{
"cell_type": "code",
- "execution_count": 156,
+ "execution_count": 9,
"metadata": {},
"outputs": [
{
@@ -243,7 +235,7 @@
"120990"
]
},
- "execution_count": 156,
+ "execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
@@ -261,10 +253,8 @@
},
{
"cell_type": "code",
- "execution_count": 157,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 10,
+ "metadata": {},
"outputs": [],
"source": [
"def second_algo(left, right):\n",
@@ -274,7 +264,7 @@
},
{
"cell_type": "code",
- "execution_count": 159,
+ "execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -283,7 +273,7 @@
"True"
]
},
- "execution_count": 159,
+ "execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@@ -294,7 +284,7 @@
},
{
"cell_type": "code",
- "execution_count": 160,
+ "execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -303,7 +293,7 @@
"True"
]
},
- "execution_count": 160,
+ "execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@@ -314,7 +304,7 @@
},
{
"cell_type": "code",
- "execution_count": 161,
+ "execution_count": 13,
"metadata": {},
"outputs": [
{
@@ -323,7 +313,7 @@
"False"
]
},
- "execution_count": 161,
+ "execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
@@ -362,7 +352,7 @@
},
{
"cell_type": "code",
- "execution_count": 162,
+ "execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -388,10 +378,8 @@
},
{
"cell_type": "code",
- "execution_count": 163,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 15,
+ "metadata": {},
"outputs": [],
"source": [
"if 1 is 2: \n",
@@ -422,7 +410,7 @@
},
{
"cell_type": "code",
- "execution_count": 164,
+ "execution_count": 16,
"metadata": {},
"outputs": [
{
@@ -445,7 +433,7 @@
},
{
"cell_type": "code",
- "execution_count": 165,
+ "execution_count": 17,
"metadata": {},
"outputs": [
{
@@ -475,10 +463,8 @@
},
{
"cell_type": "code",
- "execution_count": 166,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 18,
+ "metadata": {},
"outputs": [],
"source": [
"def third_algo(left, right):\n",
@@ -492,7 +478,7 @@
},
{
"cell_type": "code",
- "execution_count": 167,
+ "execution_count": 19,
"metadata": {},
"outputs": [
{
@@ -501,7 +487,7 @@
"True"
]
},
- "execution_count": 167,
+ "execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
@@ -512,7 +498,7 @@
},
{
"cell_type": "code",
- "execution_count": 168,
+ "execution_count": 20,
"metadata": {},
"outputs": [
{
@@ -521,7 +507,7 @@
"False"
]
},
- "execution_count": 168,
+ "execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
@@ -553,10 +539,8 @@
},
{
"cell_type": "code",
- "execution_count": 169,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 21,
+ "metadata": {},
"outputs": [],
"source": [
"def third_algo(left, right):\n",
@@ -597,16 +581,16 @@
},
{
"cell_type": "code",
- "execution_count": 170,
+ "execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "[0, 1, 2]"
+ "range(0, 3)"
]
},
- "execution_count": 170,
+ "execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
@@ -617,16 +601,16 @@
},
{
"cell_type": "code",
- "execution_count": 171,
+ "execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
+ "range(0, 10)"
]
},
- "execution_count": 171,
+ "execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
@@ -637,16 +621,16 @@
},
{
"cell_type": "code",
- "execution_count": 172,
+ "execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]"
+ "range(10, 20)"
]
},
- "execution_count": 172,
+ "execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
@@ -664,16 +648,16 @@
},
{
"cell_type": "code",
- "execution_count": 173,
+ "execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "[25, 26, 27]"
+ "range(25, 28)"
]
},
- "execution_count": 173,
+ "execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
@@ -684,16 +668,16 @@
},
{
"cell_type": "code",
- "execution_count": 174,
+ "execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "[11, 12, 13, 14, 15, 16, 17]"
+ "range(11, 18)"
]
},
- "execution_count": 174,
+ "execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
@@ -711,16 +695,16 @@
},
{
"cell_type": "code",
- "execution_count": 175,
+ "execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "[2, 4, 6, 8, 10, 12, 14, 16, 18]"
+ "range(2, 20, 2)"
]
},
- "execution_count": 175,
+ "execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
@@ -738,16 +722,16 @@
},
{
"cell_type": "code",
- "execution_count": 176,
+ "execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]"
+ "range(1, 20, 2)"
]
},
- "execution_count": 176,
+ "execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
@@ -793,7 +777,7 @@
},
{
"cell_type": "code",
- "execution_count": 177,
+ "execution_count": 30,
"metadata": {},
"outputs": [
{
@@ -811,7 +795,7 @@
"source": [
"for number in range(5):\n",
" \n",
- " print number"
+ " print (number)"
]
},
{
@@ -837,10 +821,8 @@
},
{
"cell_type": "code",
- "execution_count": 178,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 31,
+ "metadata": {},
"outputs": [],
"source": [
"answer = 1 is 1"
@@ -848,7 +830,7 @@
},
{
"cell_type": "code",
- "execution_count": 179,
+ "execution_count": 32,
"metadata": {},
"outputs": [
{
@@ -857,7 +839,7 @@
"True"
]
},
- "execution_count": 179,
+ "execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
@@ -875,10 +857,8 @@
},
{
"cell_type": "code",
- "execution_count": 180,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 33,
+ "metadata": {},
"outputs": [],
"source": [
"def third_algo(left, right):\n",
@@ -905,10 +885,8 @@
},
{
"cell_type": "code",
- "execution_count": 181,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 34,
+ "metadata": {},
"outputs": [],
"source": [
"def fourth_algo(left, right):\n",
@@ -944,7 +922,7 @@
},
{
"cell_type": "code",
- "execution_count": 184,
+ "execution_count": 36,
"metadata": {},
"outputs": [
{
@@ -976,12 +954,12 @@
" # then we create a conditional statement for cases when it's true\n",
" if result is 0: \n",
" \n",
- " print True\n",
+ " print (True)\n",
" \n",
" # and finish with else for cases when it's false \n",
" else: \n",
"\n",
- " print False"
+ " print (False)"
]
},
{
@@ -993,7 +971,7 @@
},
{
"cell_type": "code",
- "execution_count": 188,
+ "execution_count": 37,
"metadata": {},
"outputs": [
{
@@ -1020,10 +998,10 @@
" result = left % right\n",
" \n",
" if result is 0: \n",
- " print True\n",
+ " print (True)\n",
" \n",
" else: \n",
- " print False"
+ " print (False)"
]
},
{
@@ -1035,7 +1013,7 @@
},
{
"cell_type": "code",
- "execution_count": 190,
+ "execution_count": 38,
"metadata": {},
"outputs": [
{
@@ -1062,10 +1040,10 @@
" result = left % right\n",
" \n",
" if result is 0: \n",
- " print True\n",
+ " print (True)\n",
" \n",
" else: \n",
- " print False"
+ " print (False)"
]
},
{
@@ -1077,10 +1055,8 @@
},
{
"cell_type": "code",
- "execution_count": 196,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 39,
+ "metadata": {},
"outputs": [],
"source": [
"def fifth_algo(left, right): \n",
@@ -1091,10 +1067,10 @@
" result = left % right\n",
"\n",
" if result is 0: \n",
- " print True\n",
+ " print (True)\n",
"\n",
" else: \n",
- " print False"
+ " print (False)"
]
},
{
@@ -1106,7 +1082,7 @@
},
{
"cell_type": "code",
- "execution_count": 197,
+ "execution_count": 40,
"metadata": {},
"outputs": [
{
@@ -1133,10 +1109,8 @@
},
{
"cell_type": "code",
- "execution_count": 200,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 43,
+ "metadata": {},
"outputs": [],
"source": [
"def sixth_algo(left): # <-- changed\n",
@@ -1147,15 +1121,15 @@
" result = left % right\n",
"\n",
" if result is 0: \n",
- " print True\n",
+ " print (True)\n",
"\n",
" else: \n",
- " print False"
+ " print (False)"
]
},
{
"cell_type": "code",
- "execution_count": 202,
+ "execution_count": 44,
"metadata": {},
"outputs": [
{
@@ -1177,7 +1151,7 @@
},
{
"cell_type": "code",
- "execution_count": 203,
+ "execution_count": 45,
"metadata": {},
"outputs": [
{
@@ -1201,7 +1175,7 @@
},
{
"cell_type": "code",
- "execution_count": 204,
+ "execution_count": 46,
"metadata": {},
"outputs": [
{
@@ -1240,10 +1214,8 @@
},
{
"cell_type": "code",
- "execution_count": 206,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 47,
+ "metadata": {},
"outputs": [],
"source": [
"def seventh_algo(left):\n",
@@ -1272,7 +1244,7 @@
},
{
"cell_type": "code",
- "execution_count": 207,
+ "execution_count": 48,
"metadata": {},
"outputs": [
{
@@ -1281,7 +1253,7 @@
"0"
]
},
- "execution_count": 207,
+ "execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
@@ -1299,7 +1271,7 @@
},
{
"cell_type": "code",
- "execution_count": 211,
+ "execution_count": 49,
"metadata": {},
"outputs": [
{
@@ -1308,7 +1280,7 @@
"0"
]
},
- "execution_count": 211,
+ "execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
@@ -1326,10 +1298,8 @@
},
{
"cell_type": "code",
- "execution_count": 212,
- "metadata": {
- "collapsed": true
- },
+ "execution_count": 50,
+ "metadata": {},
"outputs": [],
"source": [
"def eight_algo(left):\n",
@@ -1348,7 +1318,7 @@
},
{
"cell_type": "code",
- "execution_count": 213,
+ "execution_count": 51,
"metadata": {},
"outputs": [
{
@@ -1357,7 +1327,7 @@
"True"
]
},
- "execution_count": 213,
+ "execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
@@ -1368,7 +1338,7 @@
},
{
"cell_type": "code",
- "execution_count": 214,
+ "execution_count": 52,
"metadata": {},
"outputs": [
{
@@ -1377,7 +1347,7 @@
"True"
]
},
- "execution_count": 214,
+ "execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
@@ -1388,7 +1358,7 @@
},
{
"cell_type": "code",
- "execution_count": 215,
+ "execution_count": 53,
"metadata": {},
"outputs": [
{
@@ -1397,7 +1367,7 @@
"False"
]
},
- "execution_count": 215,
+ "execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
@@ -1439,25 +1409,32 @@
"source": [
"
"
]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
}
],
"metadata": {
"kernelspec": {
- "display_name": "Python 2",
+ "display_name": "Python 3",
"language": "python",
- "name": "python2"
+ "name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
- "version": 2
+ "version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
- "pygments_lexer": "ipython2",
- "version": "2.7.10"
+ "pygments_lexer": "ipython3",
+ "version": "3.5.4"
}
},
"nbformat": 4,
From 685d8828ec4dd3ebeb900618089828480e0e2c47 Mon Sep 17 00:00:00 2001
From: Scott <32791534+Doc-Scott@users.noreply.github.com>
Date: Sun, 8 Jul 2018 15:29:39 +0100
Subject: [PATCH 3/3] Delete numerical-computing-is-fun-3.ipynb
---
numerical-computing-is-fun-3.ipynb | 1442 ----------------------------
1 file changed, 1442 deletions(-)
delete mode 100644 numerical-computing-is-fun-3.ipynb
diff --git a/numerical-computing-is-fun-3.ipynb b/numerical-computing-is-fun-3.ipynb
deleted file mode 100644
index dbd2649..0000000
--- a/numerical-computing-is-fun-3.ipynb
+++ /dev/null
@@ -1,1442 +0,0 @@
-{
- "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, after playing with computers and numbers for nearly 4 decares, I've also made this as a reminder for myself on 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 second third, 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 we input is divisible by the second 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": 1,
- "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": 2,
- "metadata": {},
- "outputs": [],
- "source": [
- "first_algo(8, 4)"
- ]
- },
- {
- "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 explicitly stating that we want to print something out. This is easy to fix by modifying our function slightly. "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [],
- "source": [
- "def first_algo(left, right):\n",
- " \n",
- " print(left % right)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "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, later you'll learn why, 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."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [],
- "source": [
- "def first_algo(left, right):\n",
- " \n",
- " return left % right"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "0"
- ]
- },
- "execution_count": 6,
- "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": 7,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "1"
- ]
- },
- "execution_count": 7,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "first_algo(5,2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "0"
- ]
- },
- "execution_count": 8,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "first_algo(15,3)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "120990"
- ]
- },
- "execution_count": 9,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "first_algo(1523434234234, 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 input with the right input. Let's apply some Boolean logic to the our algoritmh."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [],
- "source": [
- "def second_algo(left, right):\n",
- " \n",
- " return left % right is 0"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "True"
- ]
- },
- "execution_count": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "second_algo(8, 4)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "True"
- ]
- },
- "execution_count": 12,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "second_algo(30, 2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "False"
- ]
- },
- "execution_count": 13,
- "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. 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 a typical boolean statement. 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 learn so far, conditional statements are incredibly intuitive to use in Python. Let's see a simple example."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "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": 15,
- "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. Let's introduce a common continuation to this, 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 playstations\"**"
- ]
- },
- {
- "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": 16,
- "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": 17,
- "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": 18,
- "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": "code",
- "execution_count": 19,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "True"
- ]
- },
- "execution_count": 19,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "third_algo(8, 2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "False"
- ]
- },
- "execution_count": 20,
- "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. 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 output of the function in anyway."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 21,
- "metadata": {},
- "outputs": [],
- "source": [
- "def third_algo(left, right):\n",
- " \n",
- " # it will not rain\n",
- " if left % right is 0:\n",
- " return True\n",
- " \n",
- " # it will rain a little\n",
- " elif left % right is 1: \n",
- " return \n",
- " \n",
- " # it will rain heavily\n",
- " else: \n",
- " return False"
- ]
- },
- {
- "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."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 3.3. Generating Numbers"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Before moving on to the final section of this part, which will cover loops, another important basic building block of algoritms, let's have some straighforward computing fun by generating numbers. This will become handy soon, so we don't have to key in the many numbers we want to check in terms of if they are prime or not. To do this, we will use a function called range()."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "range(0, 3)"
- ]
- },
- "execution_count": 22,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "range(3)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "range(0, 10)"
- ]
- },
- "execution_count": 23,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "range(10)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 24,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "range(10, 20)"
- ]
- },
- "execution_count": 24,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "range(10, 20)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "In the first two examples, we are simply telling the range() function to generate numbers starting from zero. In the third example we are giving also a second input, and now get numbers between the two inputs. Let's see a few more examples to make sure this is clear."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 25,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "range(25, 28)"
- ]
- },
- "execution_count": 25,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "range(25, 28)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 26,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "range(11, 18)"
- ]
- },
- "execution_count": 26,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "range(11, 18)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "We can also add a 'step' argument, which gives us even more control over the range of numbers we want to create. For example with step argument 2, we will get every other number in a range:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "range(2, 20, 2)"
- ]
- },
- "execution_count": 27,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "range(2,20,2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "This way we only get the even numbers between 2 and 2. Let's try the same for odd numbers."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 28,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "range(1, 20, 2)"
- ]
- },
- "execution_count": 28,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "range(1,20,2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "There are many other ways you can use to create numbers, including random numbers, but this will be more than enough for what we want to do. Let's move on to the next section and learn about loops."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 3.4. Using Loops to Automate Things"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Even though our previous algo examples technically speaking are algoritms, it's rare that an algoritm is used without introducing at least some kind of a loop as part of the system. You guessed it, loops are just as easy and intuitive to use in Python language as everything else we've learn so far. For now, we're going to focus on the most commonly used type of loop, a 'for' loop. A for loop is a way to say that some process will go on *for* as long as something is true. Consider this example: "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**\"I will play football as long as 5 goals are scored\"**"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "To do this, we can use a very simple loop which keeps going for a number of rounds. In other words, for a range of numbers."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 30,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "0\n",
- "1\n",
- "2\n",
- "3\n",
- "4\n"
- ]
- }
- ],
- "source": [
- "for number in range(5):\n",
- " \n",
- " print (number)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "That's, there is nothing more to it. You've learn yet another fundamental building block of data science. Note how the numbers in the range start from 0 unless we tell range() to start from somewhere else. In Python language and most other programming languages, numbers start from 0 and not 1. Let's put this together with our algo and also introduce one more new concept (I promise it's the last one for a while), storing something on to a variable. Storing something in to the computer memory is nothing like storing something in to human memory."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "
"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Even though we talk about storing something in to the memory, it'a also nothing like storing something in to a safety deposit box where things will be kept for a long time. Computer memory is a temporary storage for something we're going to use as part of our computer program. Let's see some examples."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 31,
- "metadata": {},
- "outputs": [],
- "source": [
- "answer = 1 is 1"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 32,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "True"
- ]
- },
- "execution_count": 32,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "answer"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Instead of getting the answer straight away, we store it in memory so we can access it later. This is useful when we want to use the same value many times. For example, we could use this approach to simplify the most recent version of our algo. Take a note below how we are using the same operation twice:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 33,
- "metadata": {},
- "outputs": [],
- "source": [
- "def third_algo(left, right):\n",
- " \n",
- " # it will not rain\n",
- " if left % right is 0: # < -- here first time\n",
- " return True\n",
- " \n",
- " # it will rain a little\n",
- " elif left % right is 1: # < -- here second time\n",
- " return \n",
- " \n",
- " # it will rain heavily\n",
- " else: \n",
- " return False"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Now let's make a slight simplifcation by storing the operation we do twice in to memory first."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 34,
- "metadata": {},
- "outputs": [],
- "source": [
- "def fourth_algo(left, right):\n",
- " \n",
- " stored_value = left % right\n",
- " \n",
- " # it will not rain\n",
- " if stored_value is 0:\n",
- " return True\n",
- " \n",
- " # it will rain a little\n",
- " elif stored_value is 1:\n",
- " return \n",
- " \n",
- " # it will rain heavily\n",
- " else: \n",
- " return False"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Before wrapping up for now, let's put all that we've learn together in one example."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 3.5. Putting it All Together"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 36,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "True\n",
- "True\n",
- "True\n",
- "True\n",
- "True\n",
- "True\n",
- "True\n",
- "True\n",
- "True\n"
- ]
- }
- ],
- "source": [
- "# first we create a range of numbers\n",
- "numbers = range(1,10)\n",
- "\n",
- "# then we create a loop\n",
- "for number in numbers: \n",
- "\n",
- " # then we perform the modulus operation \n",
- " result = number % number\n",
- " \n",
- " # then we create a conditional statement for cases when it's true\n",
- " if result is 0: \n",
- " \n",
- " print (True)\n",
- " \n",
- " # and finish with else for cases when it's false \n",
- " else: \n",
- "\n",
- " print (False)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Obviously we are getting True as result everytime because we are always having both the right number and the left number the same (for example 8 % 8). Let's make a slight modification to take us step closer to something that will help us a great deal in finding prime numbers later. This time I'm removing the comments to keep the code neat."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 37,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "True\n",
- "True\n",
- "False\n",
- "True\n",
- "True\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n"
- ]
- }
- ],
- "source": [
- "left = 20\n",
- "right_numbers = range(1,10)\n",
- "\n",
- "for right in numbers: \n",
- " result = left % right\n",
- " \n",
- " if result is 0: \n",
- " print (True)\n",
- " \n",
- " else: \n",
- " print (False)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "So what we are doing now, is fixing the left number to be 20, and then checking it against every number in the range of 1 to 10 and see if it's divisible. This makes checking if a number is prime a whole lot simpler! Let's try an example where we know it's a prime nubmer, for example 13 (it's not divisisble by any other number than 1 and itself)."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 38,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "True\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n"
- ]
- }
- ],
- "source": [
- "left = 13 # <-- changed\n",
- "right_numbers = range(1,12) # <-- changed\n",
- "\n",
- "for right in numbers: \n",
- " result = left % right\n",
- " \n",
- " if result is 0: \n",
- " print (True)\n",
- " \n",
- " else: \n",
- " print (False)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Because we are starting our range from 1, one get one True in the beginning, so we have to start the range from 2 instead to get the right answer. As you can see, I changed the second line so that we scan until 12 which is the last number before 13. Let's put this inside a function as our fifth algo version and make the range start from 2 instead of 1."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 39,
- "metadata": {},
- "outputs": [],
- "source": [
- "def fifth_algo(left, right): \n",
- "\n",
- " right_numbers = range(2, right) # <-- changed\n",
- "\n",
- " for right in right_numbers: # <-- changed\n",
- " result = left % right\n",
- "\n",
- " if result is 0: \n",
- " print (True)\n",
- "\n",
- " else: \n",
- " print (False)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Now things are starting to look good. We could now remove 'left' variable entirely as it comes as an argument from the function, and also instead of having to modify the function for the last number of the range, we also input that as an argument."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 40,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "False\n",
- "False\n",
- "False\n",
- "False\n"
- ]
- }
- ],
- "source": [
- "fifth_algo(7,6)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "That's it, we're prime number checking now! :) Because the result is False for all, we know for sure that our input, in this case 7, is a prime. There is one more very small change we can do using the skill we've already learn to make a nice improvement to what we already have. Instead of requiring the user to input the end of the range, we can automatically compute it as it's always the last number before left. In other words, it's left - 1."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 43,
- "metadata": {},
- "outputs": [],
- "source": [
- "def sixth_algo(left): # <-- changed\n",
- "\n",
- " right_numbers = range(2, left - 1) # <-- changed\n",
- "\n",
- " for right in right_numbers:\n",
- " result = left % right\n",
- "\n",
- " if result is 0: \n",
- " print (True)\n",
- "\n",
- " else: \n",
- " print (False)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 44,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "False\n",
- "True\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n"
- ]
- }
- ],
- "source": [
- "sixth_algo(9)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 45,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n"
- ]
- }
- ],
- "source": [
- "sixth_algo(11)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 46,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n",
- "False\n"
- ]
- }
- ],
- "source": [
- "sixth_algo(19)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Things are working real nicely now. But clearly we will later have a problem with larger numbers with this current approach, as if we input 1,000, we will have 1,000 True or False values printed on the screen. To overcome this, we can make a small change to our latest version."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 47,
- "metadata": {},
- "outputs": [],
- "source": [
- "def seventh_algo(left):\n",
- "\n",
- " right_numbers = range(2, left - 1)\n",
- " output = 0 # <-- changed\n",
- " \n",
- " for right in right_numbers:\n",
- " result = left % right\n",
- "\n",
- " if result is 0: \n",
- " output += 1 # <-- changed\n",
- "\n",
- " else: \n",
- " output += 0 # <-- changed\n",
- " \n",
- " return output # <-- changed"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "What we are doing, is first we declare a variable 'output' with starting value 0. Then instead of printing out True, we silently add 1 to output, and in case of False we add 0. Only in the end we print the value out, with the return statement that is outside of the for loop (note how it's indentation is equal to the for statement, meaning it will be processed only once the for loop has completed its job)."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 48,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "0"
- ]
- },
- "execution_count": 48,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "seventh_algo(19)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Nice. Now we can key in much larger numbers, and just get one output."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 49,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "0"
- ]
- },
- "execution_count": 49,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "seventh_algo(127)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Before wrapping up, let's simplify our code slightly and instead of outputting a number, output a True or False statement. True for 'it's a prime' and False for 'it's not a prime'."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 50,
- "metadata": {},
- "outputs": [],
- "source": [
- "def eight_algo(left):\n",
- "\n",
- " right_numbers = range(2, left - 1)\n",
- " output = 0\n",
- " \n",
- " for right in right_numbers:\n",
- " result = left % right\n",
- "\n",
- " if result is 0: \n",
- " output += 1\n",
- " \n",
- " return output is 0 # <-- changed"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 51,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "True"
- ]
- },
- "execution_count": 51,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "eight_algo(19)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 52,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "True"
- ]
- },
- "execution_count": 52,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "eight_algo(127)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 53,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "False"
- ]
- },
- "execution_count": 53,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "eight_algo(12)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Note how we removed the else statements entirely. Because we are doing nothing in the cases where the left number is not divisible by the right number. In other words, whenever the product of the modulus operation is not zero, we do nothing. Therefore it's enough to just have the if statement without the else. This is quite common. "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Part 3 Summary\n",
- "\n",
- "- Algoritms are like insides of factories \n",
- "- Algoritms take inputs and give outputs\n",
- "- Conditional statements are a tool for putting boolean logic in to action\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",
- "- With small changes to our code, we can make big improvements in capability\n",
- "- It's very convinient to store values in to memory\n",
- "- Computer memory is nothing like human memory, and also not like a safe deposit box\n",
- "- Any value can be stored in to memory \n",
- "- Numbers can be automatically generated with range() function\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": [
- "
"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "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.5.4"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 1
-}