Created
October 6, 2016 07:44
-
-
Save drvinceknight/ccb740d54f68fc3df33a18d1c866af87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Variables, conditional statements and loops" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Q1: Building variables" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"20\n" | |
] | |
} | |
], | |
"source": [ | |
"age = 20 \n", | |
"print(age)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"21\n" | |
] | |
} | |
], | |
"source": [ | |
"age = age + 1\n", | |
"print(age)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"22\n" | |
] | |
} | |
], | |
"source": [ | |
"age += 1\n", | |
"print(age)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"int" | |
] | |
}, | |
"execution_count": 49, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"type(age)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"float" | |
] | |
}, | |
"execution_count": 50, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"type(5.4)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The questions asks students to experiment with this further. Here are some further examples:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 51, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"28\n" | |
] | |
} | |
], | |
"source": [ | |
"age = 32\n", | |
"years_in_education = age - 4\n", | |
"print(years_in_education)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 52, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"784" | |
] | |
}, | |
"execution_count": 52, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"years_in_education **= 2 # Squaring\n", | |
"years_in_education" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 53, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0" | |
] | |
}, | |
"execution_count": 53, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"years_in_education % 4 # Remainder when dividing by 4" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 54, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"196.0" | |
] | |
}, | |
"execution_count": 54, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"years_in_education / 4 # Check that it is a multiple of 4" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Q2: Creating character variables" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"It is possible to create character variables:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 55, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Vince\n" | |
] | |
} | |
], | |
"source": [ | |
"firstname = \"Vince\"\n", | |
"print(firstname)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"These are called **strings** in Python (`str` for short):" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 56, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<class 'str'>\n" | |
] | |
} | |
], | |
"source": [ | |
"print(type(firstname))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We can combine strings:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 57, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'Vince Knight'" | |
] | |
}, | |
"execution_count": 57, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"lastname = \"Knight\"\n", | |
"fullname = firstname + \" \" + lastname\n", | |
"fullname # Note that when using Jupyter we don't need to use 'print'" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Q3: Boolean variables and if statements\n", | |
"\n", | |
"This is a type of variable that is either `True` or `False`" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 58, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 58, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a = 4\n", | |
"b = 8 / 2\n", | |
"a == b " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 59, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"False" | |
] | |
}, | |
"execution_count": 59, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a != b" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 60, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 60, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a >= b" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 61, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 61, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a < b + 1" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We can (just like with any other variable) assign a boolean value to a variable:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 62, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 62, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"statement = a == b\n", | |
"statement" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 63, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"bool" | |
] | |
}, | |
"execution_count": 63, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"type(statement)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We can use this in conjunction with `if`, `elif` and `else` statements to create code that is equivalent to the following mathematical function:\n", | |
"\n", | |
"$$f(n)=\\begin{cases}\n", | |
"1&\\text{ if } n\\leq 5\\\\\n", | |
"2&\\text{ if } n> 5\\text{ and } n \\text{ even}\\\\\n", | |
"3&\\text{ otherwise }\\\\\n", | |
"\\end{cases}$$" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 64, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"execution_count": 64, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"n = 11 # Experiment by changing the value of n\n", | |
"if n <= 5:\n", | |
" value = 1\n", | |
"elif n % 2 == 0: # Otherwise if (else if)\n", | |
" value = 2\n", | |
"else: # Otherwise\n", | |
" value = 3\n", | |
"value" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Q4: While loops\n", | |
"\n", | |
"It is possible to repeat code until a particular boolean variable becomes `False`:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 65, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"55" | |
] | |
}, | |
"execution_count": 65, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"count = 0 # A variable to count\n", | |
"total = 0 # We will sum the first ten numbers\n", | |
"while count < 10: # Keep repeating until count if >= 10\n", | |
" count += 1 # Adding 1 to count\n", | |
" total += count # Adding the count to the total\n", | |
"total" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Q5: Worked example\n", | |
"\n", | |
"This example gets a lot of numerical evidence for the following equality:\n", | |
"\n", | |
"$$\n", | |
"\\sum_{i=0}^n i ^ 2 = \\frac{n(n+1)(2n+1)}{6}\n", | |
"$$" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Here is the right hand side (for $n=20$):" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 66, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2870.0" | |
] | |
}, | |
"execution_count": 66, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"n = 20\n", | |
"rhs = n * (n + 1) * (2 * n + 1) / 6\n", | |
"rhs" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Here is the left hand side, note that we make use of a `while` loop to keep 'summing':" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 67, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2870" | |
] | |
}, | |
"execution_count": 67, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"lhs = 0\n", | |
"i = 0\n", | |
"while i < n:\n", | |
" i += 1\n", | |
" lhs += i ** 2\n", | |
"lhs" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Finally here is a boolean to check if the two values are equal (so that we don't have to check the numbers ourselves):" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 68, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 68, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"lhs == rhs" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Now we can automate the checking of the above for various values of $n$ using another `while` loop:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"max_n = 2000\n", | |
"n = 0\n", | |
"while n < max_n: # checking the equality for n up until 2000\n", | |
" n += 1\n", | |
" rhs = n * (n + 1) * (2 * n + 1) / 6\n", | |
" lhs = 0\n", | |
" i = 0\n", | |
" while i < n:\n", | |
" i += 1\n", | |
" lhs += i ** 2\n", | |
" if lhs != rhs:\n", | |
" print(False) # Will print a False if it ever finds an error" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Q6 Debugging exercise\n", | |
"\n", | |
"Here is the correct version of the code:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"max_n = 2000\n", | |
"n = 0\n", | |
"#while n > max_n: This inequality is the wrong way around:\n", | |
"while n < max_n:\n", | |
" #n += 2 We should be incrementing by 1\n", | |
" n += 1\n", | |
" #rhs = ((n ** 2 + 2 * n) ** 2) / 4 This is the incorrect formula\n", | |
" rhs = ((n ** 2 + n) ** 2) / 4\n", | |
" lhs = 0\n", | |
" i = 0\n", | |
" while i < n:\n", | |
" i += 1\n", | |
" #lhs += i ** 2 Incrementing the incorrect amount\n", | |
" lhs += i ** 3\n", | |
" if lhs != rhs:\n", | |
" print(False)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Q7\n", | |
"\n", | |
"This is the same as the first part of the previous question but with a simpler equality:\n", | |
"\n", | |
"$$\n", | |
"\\sum_{i=0}^{n}i=\\frac{n(n+1)}{2}\n", | |
"$$" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Here is the right hand side:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"210.0" | |
] | |
}, | |
"execution_count": 30, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"n = 20\n", | |
"rhs = n * (n + 1) / 2\n", | |
"rhs" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Here is the left hand side:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"210" | |
] | |
}, | |
"execution_count": 33, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"lhs = 0\n", | |
"i = 0\n", | |
"while i < n:\n", | |
" i += 1\n", | |
" lhs += i # This is the only line that is different to before\n", | |
"lhs" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 34, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"lhs == rhs" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Q8: TICKABLE" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We do the same as in Q5: wrap the code in another `while` loop:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"max_n = 2000\n", | |
"n = 0\n", | |
"while n < max_n: # checking the equality for n up until 2000\n", | |
" n += 1\n", | |
" rhs = n * (n + 1) / 2\n", | |
" lhs = 0\n", | |
" i = 0\n", | |
" while i < n:\n", | |
" i += 1\n", | |
" lhs += i\n", | |
" if lhs != rhs:\n", | |
" print(False) # Will print a False if it ever finds an error" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Nothing is output when we run the above cell so the equality has been check for the first 2000 values of n." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Q9\n", | |
"\n", | |
"Here we use a `while` loop to get the sum of the first numbers less than 1000 that are divisible by 3:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"333667" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"upper_limit = 1000\n", | |
"\n", | |
"total = 0\n", | |
"number = 0\n", | |
"\n", | |
"while number < upper_limit:\n", | |
" number += 1 # Increment the number\n", | |
" if number % 3 != 0: # Check if it is divisible by 3\n", | |
" total += number\n", | |
"total" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Q10\n", | |
"\n", | |
"Here we use a `while` loop to get the sum of the first 1000 numbers that are divisible by 3:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"750000" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"upper_count = 1000\n", | |
"\n", | |
"total = 0\n", | |
"number = 0\n", | |
"count = 0\n", | |
"\n", | |
"while count < upper_count:\n", | |
" number += 1 # Increment the number\n", | |
" if number % 3 != 0: # Check if it is divisible by 3\n", | |
" total += number\n", | |
" # Keep track of how many numbers we have that are divisible by 3\n", | |
" count += 1 \n", | |
"total " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Q11\n", | |
"\n", | |
"Implement the following algorithm for calculating the square root of a number:\n", | |
"\n", | |
"$$\n", | |
"x_{n+1}=\\frac{x_n + K / x_{n}}{2}\n", | |
"$$" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"13.5\n", | |
"10.157407407407408\n", | |
"9.607418380093858\n", | |
"9.591675965316126\n" | |
] | |
} | |
], | |
"source": [ | |
"epsilon = 0.001\n", | |
"K = 92 #This sets an initial value to take the square root of\n", | |
"X = K / 4.0 # This picks an initial value for the sequence\n", | |
"while abs(X**2 - K) > epsilon: # abs is the absolute value\n", | |
" X = (X + K / X) / 2\n", | |
" print(X)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Let us check:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"92.00024782362304" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"X ** 2" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"That is correct up to a difference of 0.001 (which was the precision we required)." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"display_name": "Python [Root]", | |
"language": "python", | |
"name": "Python [Root]" | |
}, | |
"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.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment