diff --git a/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/.ipynb_checkpoints/Untitled-checkpoint.ipynb
new file mode 100644
index 000000000..2fd64429b
--- /dev/null
+++ b/.ipynb_checkpoints/Untitled-checkpoint.ipynb
@@ -0,0 +1,6 @@
+{
+ "cells": [],
+ "metadata": {},
+ "nbformat": 4,
+ "nbformat_minor": 2
+}
diff --git a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/01-Numbers-checkpoint.ipynb b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/01-Numbers-checkpoint.ipynb
new file mode 100644
index 000000000..d840c0184
--- /dev/null
+++ b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/01-Numbers-checkpoint.ipynb
@@ -0,0 +1,564 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "___\n",
+ "\n",
+ "\n",
+ "___\n",
+ "
Examples | \n", + "Number \"Type\" | \n", + "
---|---|
1,2,-5,1000 | \n", + "Integers | \n", + "
1.2,-0.5,2e2,3E2 | \n", + "Floating-point numbers | \n", + "
def
then a space followed by the name of the function. Try to keep names relevant, for example len() is a good name for a length() function. Also be careful with names, you wouldn't want to call a function the same name as a [built-in function in Python](https://docs.python.org/2/library/functions.html) (such as len).\n",
+ "We begin with def
then a space followed by the name of the function. Try to keep names relevant, for example len() is a good name for a length() function. Also be careful with names, you wouldn't want to call a function the same name as a [built-in function in Python](https://docs.python.org/3/library/functions.html) (such as len).\n",
"\n",
"Next come a pair of parentheses with a number of arguments separated by a comma. These arguments are the inputs for your function. You'll be able to use these inputs in your function and reference them. After this you put a colon.\n",
"\n",
"Now here is the important step, you must indent to begin the code inside your function correctly. Python makes use of *whitespace* to organize code. Lots of other programing languages do not do this, so keep that in mind.\n",
"\n",
- "Next you'll see the docstring, this is where you write a basic description of the function. Using iPython and iPython Notebooks, you'll be able to read these docstrings by pressing Shift+Tab after a function name. Docstrings are not necessary for simple functions, but it's good practice to put them in so you or other people can easily understand the code you write.\n",
+ "Next you'll see the docstring, this is where you write a basic description of the function. Using Jupyter and Jupyter Notebooks, you'll be able to read these docstrings by pressing Shift+Tab after a function name. Docstrings are not necessary for simple functions, but it's good practice to put them in so you or other people can easily understand the code you write.\n",
"\n",
"After all this you begin writing the code you wish to execute.\n",
"\n",
@@ -63,19 +103,28 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "### Example 1: A simple print 'hello' function"
+ "### Simple example of a function"
]
},
{
"cell_type": "code",
- "execution_count": 2,
- "metadata": {},
+ "execution_count": 4,
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"def say_hello():\n",
" print('hello')"
]
},
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Calling a function with ()"
+ ]
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -85,7 +134,7 @@
},
{
"cell_type": "code",
- "execution_count": 3,
+ "execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -104,23 +153,52 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "### Example 2: A simple greeting function\n",
- "Let's write a function that greets people with their name."
+ "If you forget the parenthesis (), it will simply display the fact that say_hello is a function. Later on we will learn we can actually pass in functions into other functions! But for now, simply remember to call functions with ()."
]
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "return
statement. return
allows a function to *return* a result that can then be stored as a variable, or used in whatever manner a user wants.\n",
"\n",
- "### Example 3: Addition function"
+ "### Example: Addition function"
]
},
{
"cell_type": "code",
"execution_count": 6,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"def add_num(num1,num2):\n",
@@ -178,7 +260,9 @@
{
"cell_type": "code",
"execution_count": 8,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"# Can also save as variable due to return\n",
@@ -233,130 +317,1036 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "Note that because we don't declare variable types in Python, this function could be used to add numbers or sequences together! We'll later learn about adding in checks to make sure a user puts in the correct arguments into a function.\n",
+ "## Very Common Question: \"What is the difference between *return* and *print*?\"\n",
"\n",
- "Let's also start using break
, continue
, and pass
statements in our code. We introduced these during the while
lecture."
+ "**The return keyword allows you to actually save the result of the output of a function as a variable. The print() function simply displays the output to you, but doesn't save it for future use. Let's explore this in more detail**"
]
},
{
- "cell_type": "markdown",
+ "cell_type": "code",
+ "execution_count": 1,
"metadata": {
"collapsed": true
},
+ "outputs": [],
"source": [
- "Finally let's go over a full example of creating a function to check if a number is prime (a common interview exercise).\n",
- "\n",
- "We know a number is prime if that number is only evenly divisible by 1 and itself. Let's write our first version of the function to check all the numbers from 1 to N and perform modulo checks."
+ "def print_result(a,b):\n",
+ " print(a+b)"
]
},
{
"cell_type": "code",
- "execution_count": 11,
- "metadata": {},
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
- "def is_prime(num):\n",
- " '''\n",
- " Naive method of checking for primes. \n",
- " '''\n",
- " for n in range(2,num):\n",
- " if num % n == 0:\n",
- " print(num,'is not prime')\n",
- " break\n",
- " else: # If never mod zero, then prime\n",
- " print(num,'is prime!')"
+ "def return_result(a,b):\n",
+ " return a+b"
]
},
{
"cell_type": "code",
- "execution_count": 12,
+ "execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "16 is not prime\n"
+ "15\n"
]
}
],
"source": [
- "is_prime(16)"
+ "print_result(10,5)"
]
},
{
"cell_type": "code",
- "execution_count": 13,
+ "execution_count": 4,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "15"
+ ]
+ },
+ "execution_count": 4,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# You won't see any output if you run this in a .py script\n",
+ "return_result(10,5)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "**But what happens if we actually want to save this result for later use?**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "17 is prime!\n"
+ "40\n"
]
}
],
"source": [
- "is_prime(17)"
+ "my_result = print_result(20,20)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "my_result"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "NoneType"
+ ]
+ },
+ "execution_count": 7,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "type(my_result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "Note how the else
lines up under for
and not if
. This is because we want the for
loop to exhaust all possibilities in the range before printing our number is prime.\n",
- "\n",
- "Also note how we break the code after the first print statement. As soon as we determine that a number is not prime we break out of the for
loop.\n",
- "\n",
- "We can actually improve this function by only checking to the square root of the target number, and by disregarding all even numbers after checking for 2. We'll also switch to returning a boolean value to get an example of using return statements:"
+ "**Be careful! Notice how print_result() doesn't let you actually save the result to a variable! It only prints it out, with print() returning None for the assignment!**"
]
},
{
"cell_type": "code",
- "execution_count": 14,
- "metadata": {},
+ "execution_count": 8,
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
- "import math\n",
- "\n",
- "def is_prime2(num):\n",
- " '''\n",
- " Better method of checking for primes. \n",
- " '''\n",
- " if num % 2 == 0 and num > 2: \n",
- " return False\n",
- " for i in range(3, int(math.sqrt(num)) + 1, 2):\n",
- " if num % i == 0:\n",
- " return False\n",
- " return True"
+ "my_result = return_result(20,20)"
]
},
{
"cell_type": "code",
- "execution_count": 15,
+ "execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "False"
+ "40"
]
},
- "execution_count": 15,
+ "execution_count": 9,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "my_result"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "80"
+ ]
+ },
+ "execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
- "is_prime2(18)"
+ "my_result + my_result"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Adding Logic to Internal Function Operations\n",
+ "\n",
+ "So far we know quite a bit about constructing logical statements with Python, such as if/else/elif statements, for and while loops, checking if an item is **in** a list or **not in** a list (Useful Operators Lecture). Let's now see how we can perform these operations within a function."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Check if a number is even "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "Why don't we have any break
statements? It should be noted that as soon as a function *returns* something, it shuts down. A function can deliver multiple print statements, but it will only obey one return
."
+ "**Recall the mod operator % which returns the remainder after division, if a number is even then mod 2 (% 2) should be == to zero.**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0"
+ ]
+ },
+ "execution_count": 11,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "2 % 2"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0"
+ ]
+ },
+ "execution_count": 12,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "20 % 2"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "1"
+ ]
+ },
+ "execution_count": 14,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "21 % 2"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 15,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "20 % 2 == 0"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 16,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "False"
+ ]
+ },
+ "execution_count": 16,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "21 % 2 == 0"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "** Let's use this to construct a function. Notice how we simply return the boolean check.**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 18,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def even_check(number):\n",
+ " return number % 2 == 0"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 19,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 19,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "even_check(20)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 21,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "False"
+ ]
+ },
+ "execution_count": 21,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "even_check(21)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Check if any number in a list is even\n",
+ "\n",
+ "Let's return a boolean indicating if **any** number in a list is even. Notice here how **return** breaks out of the loop and exits the function"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 25,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def check_even_list(num_list):\n",
+ " # Go through each number\n",
+ " for number in num_list:\n",
+ " # Once we get a \"hit\" on an even number, we return True\n",
+ " if number % 2 == 0:\n",
+ " return True\n",
+ " # Otherwise we don't do anything\n",
+ " else:\n",
+ " pass"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "** Is this enough? NO! We're not returning anything if they are all odds!**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 26,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 26,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "check_even_list([1,2,3])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 27,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "check_even_list([1,1,1])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "** VERY COMMON MISTAKE!! LET'S SEE A COMMON LOGIC ERROR, NOTE THIS IS WRONG!!!**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 28,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def check_even_list(num_list):\n",
+ " # Go through each number\n",
+ " for number in num_list:\n",
+ " # Once we get a \"hit\" on an even number, we return True\n",
+ " if number % 2 == 0:\n",
+ " return True\n",
+ " # This is WRONG! This returns False at the very first odd number!\n",
+ " # It doesn't end up checking the other numbers in the list!\n",
+ " else:\n",
+ " return False"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 30,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "False"
+ ]
+ },
+ "execution_count": 30,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# UH OH! It is returning False after hitting the first 1\n",
+ "check_even_list([1,2,3])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "** Correct Approach: We need to initiate a return False AFTER running through the entire loop**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 31,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def check_even_list(num_list):\n",
+ " # Go through each number\n",
+ " for number in num_list:\n",
+ " # Once we get a \"hit\" on an even number, we return True\n",
+ " if number % 2 == 0:\n",
+ " return True\n",
+ " # Don't do anything if its not even\n",
+ " else:\n",
+ " pass\n",
+ " # Notice the indentation! This ensures we run through the entire for loop \n",
+ " return False"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 32,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 32,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "check_even_list([1,2,3])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 34,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "False"
+ ]
+ },
+ "execution_count": 34,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "check_even_list([1,3,5])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Return all even numbers in a list\n",
+ "\n",
+ "Let's add more complexity, we now will return all the even numbers in a list, otherwise return an empty list."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 35,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def check_even_list(num_list):\n",
+ " \n",
+ " even_numbers = []\n",
+ " \n",
+ " # Go through each number\n",
+ " for number in num_list:\n",
+ " # Once we get a \"hit\" on an even number, we append the even number\n",
+ " if number % 2 == 0:\n",
+ " even_numbers.append(number)\n",
+ " # Don't do anything if its not even\n",
+ " else:\n",
+ " pass\n",
+ " # Notice the indentation! This ensures we run through the entire for loop \n",
+ " return even_numbers"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 36,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[2, 4, 6]"
+ ]
+ },
+ "execution_count": 36,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "check_even_list([1,2,3,4,5,6])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 37,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[]"
+ ]
+ },
+ "execution_count": 37,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "check_even_list([1,3,5])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Returning Tuples for Unpacking"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "** Recall we can loop through a list of tuples and \"unpack\" the values within them**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 38,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "stock_prices = [('AAPL',200),('GOOG',300),('MSFT',400)]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 39,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "('AAPL', 200)\n",
+ "('GOOG', 300)\n",
+ "('MSFT', 400)\n"
+ ]
+ }
+ ],
+ "source": [
+ "for item in stock_prices:\n",
+ " print(item)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 41,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "AAPL\n",
+ "GOOG\n",
+ "MSFT\n"
+ ]
+ }
+ ],
+ "source": [
+ "for stock,price in stock_prices:\n",
+ " print(stock)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 42,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "200\n",
+ "300\n",
+ "400\n"
+ ]
+ }
+ ],
+ "source": [
+ "for stock,price in stock_prices:\n",
+ " print(price)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "**Similarly, functions often return tuples, to easily return multiple results for later use.**"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Let's imagine the following list:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 46,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "work_hours = [('Abby',100),('Billy',400),('Cassie',800)]"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "The employee of the month function will return both the name and number of hours worked for the top performer (judged by number of hours worked)."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 47,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def employee_check(work_hours):\n",
+ " \n",
+ " # Set some max value to intially beat, like zero hours\n",
+ " current_max = 0\n",
+ " # Set some empty value before the loop\n",
+ " employee_of_month = ''\n",
+ " \n",
+ " for employee,hours in work_hours:\n",
+ " if hours > current_max:\n",
+ " current_max = hours\n",
+ " employee_of_month = employee\n",
+ " else:\n",
+ " pass\n",
+ " \n",
+ " # Notice the indentation here\n",
+ " return (employee_of_month,current_max)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 48,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "('Cassie', 800)"
+ ]
+ },
+ "execution_count": 48,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "employee_check(work_hours)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Interactions between functions\n",
+ "\n",
+ "Functions often use results from other functions, let's see a simple example through a guessing game. There will be 3 positions in the list, one of which is an 'O', a function will shuffle the list, another will take a player's guess, and finally another will check to see if it is correct. This is based on the classic carnival game of guessing which cup a red ball is under."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "**How to shuffle a list in Python**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "example = [1,2,3,4,5]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "from random import shuffle"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "# Note shuffle is in-place\n",
+ "shuffle(example)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[3, 1, 4, 5, 2]"
+ ]
+ },
+ "execution_count": 11,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "example"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "**OK, let's create our simple game**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "mylist = [' ','O',' ']"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def shuffle_list(mylist):\n",
+ " # Take in list, and returned shuffle versioned\n",
+ " shuffle(mylist)\n",
+ " \n",
+ " return mylist"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[' ', 'O', ' ']"
+ ]
+ },
+ "execution_count": 14,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "mylist "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[' ', ' ', 'O']"
+ ]
+ },
+ "execution_count": 15,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "shuffle_list(mylist)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 18,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def player_guess():\n",
+ " \n",
+ " guess = ''\n",
+ " \n",
+ " while guess not in ['0','1','2']:\n",
+ " \n",
+ " # Recall input() returns a string\n",
+ " guess = input(\"Pick a number: 0, 1, or 2: \")\n",
+ " \n",
+ " return int(guess) "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 24,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Pick a number: 0, 1, or 2: 1\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "1"
+ ]
+ },
+ "execution_count": 24,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "player_guess()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Now we will check the user's guess. Notice we only print here, since we have no need to save a user's guess or the shuffled list."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 22,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def check_guess(mylist,guess):\n",
+ " if mylist[guess] == 'O':\n",
+ " print('Correct Guess!')\n",
+ " else:\n",
+ " print('Wrong! Better luck next time')\n",
+ " print(mylist)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Now we create a little setup logic to run all the functions. Notice how they interact with each other!"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 23,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Pick a number: 0, 1, or 2: 1\n",
+ "Wrong! Better luck next time\n",
+ "[' ', ' ', 'O']\n"
+ ]
+ }
+ ],
+ "source": [
+ "# Initial List\n",
+ "mylist = [' ','O',' ']\n",
+ "\n",
+ "# Shuffle It\n",
+ "mixedup_list = shuffle_list(mylist)\n",
+ "\n",
+ "# Get User's Guess\n",
+ "guess = player_guess()\n",
+ "\n",
+ "# Check User's Guess\n",
+ "#------------------------\n",
+ "# Notice how this function takes in the input \n",
+ "# based on the output of other functions!\n",
+ "check_guess(mixedup_list,guess)"
]
},
{
@@ -383,7 +1373,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.6.2"
+ "version": "3.6.6"
}
},
"nbformat": 4,
diff --git a/03-Methods and Functions/.ipynb_checkpoints/03-Function Practice Exercises-checkpoint.ipynb b/03-Methods and Functions/.ipynb_checkpoints/03-Function Practice Exercises-checkpoint.ipynb
index cb906dbe6..e21473093 100644
--- a/03-Methods and Functions/.ipynb_checkpoints/03-Function Practice Exercises-checkpoint.ipynb
+++ b/03-Methods and Functions/.ipynb_checkpoints/03-Function Practice Exercises-checkpoint.ipynb
@@ -1,5 +1,16 @@
{
"cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "___\n",
+ "\n",
+ "global
statement\n",
+ "If you want to assign a value to a name defined at the top level of the program (i.e. not inside any kind of scope such as functions or classes), then you have to tell Python that the name is not local, but it is global. We do this using the global
statement. It is impossible to assign a value to a variable defined outside a function without the global statement.\n",
+ "\n",
+ "You can use the values of such variables defined outside the function (assuming there is no variable with the same name within the function). However, this is not encouraged and should be avoided since it becomes unclear to the reader of the program as to where that variable’s definition is. Using the global
statement makes it amply clear that the variable is defined in an outermost block.\n",
+ "\n",
+ "Example:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Before calling func(), x is: 50\n",
+ "This function is now using the global x!\n",
+ "Because of global x is: 50\n",
+ "Ran func(), changed global x to 2\n",
+ "Value of x (outside of func()) is: 2\n"
+ ]
+ }
+ ],
+ "source": [
+ "x = 50\n",
+ "\n",
+ "def func():\n",
+ " global x\n",
+ " print('This function is now using the global x!')\n",
+ " print('Because of global x is: ', x)\n",
+ " x = 2\n",
+ " print('Ran func(), changed global x to', x)\n",
+ "\n",
+ "print('Before calling func(), x is: ', x)\n",
+ "func()\n",
+ "print('Value of x (outside of func()) is: ', x)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "collapsed": true
+ },
+ "source": [
+ "The global
statement is used to declare that **x** is a global variable - hence, when we assign a value to **x** inside the function, that change is reflected when we use the value of **x** in the main block.\n",
+ "\n",
+ "You can specify more than one global variable using the same global statement e.g. global x, y, z
."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "collapsed": true
+ },
+ "source": [
+ "## Conclusion\n",
+ "You should now have a good understanding of Scope (you may have already intuitively felt right about Scope which is great!) One last mention is that you can use the **globals()** and **locals()** functions to check what are your current local and global variables.\n",
+ "\n",
+ "Another thing to keep in mind is that everything in Python is an object! I can assign variables to functions just like I can with numbers! We will go over this again in the decorator section of the course!"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.6.6"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 1
+}
diff --git a/03-Methods and Functions/.ipynb_checkpoints/07-args and kwargs-checkpoint.ipynb b/03-Methods and Functions/.ipynb_checkpoints/07-args and kwargs-checkpoint.ipynb
index 47fa02ad6..3624b3e8b 100644
--- a/03-Methods and Functions/.ipynb_checkpoints/07-args and kwargs-checkpoint.ipynb
+++ b/03-Methods and Functions/.ipynb_checkpoints/07-args and kwargs-checkpoint.ipynb
@@ -1,5 +1,16 @@
{
"cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "___\n",
+ "\n",
+ "The volume of a sphere is given as $$\\frac{4}{3} πr^3$$
" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def vol(rad):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "33.49333333333333" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Check\n", + "vol(2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "**Write a function that checks whether a number is in a given range (inclusive of high and low)**" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def ran_check(num,low,high):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 is in the range between 2 and 7\n" + ] + } + ], + "source": [ + "# Check\n", + "ran_check(5,2,7)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you only wanted to return a boolean:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def ran_bool(num,low,high):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ran_bool(3,1,10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "____\n", + "**Write a Python function that accepts a string and calculates the number of upper case letters and lower case letters.**\n", + "\n", + " Sample String : 'Hello Mr. Rogers, how are you this fine Tuesday?'\n", + " Expected Output : \n", + " No. of Upper case characters : 4\n", + " No. of Lower case Characters : 33\n", + "\n", + "HINT: Two string methods that might prove useful: **.isupper()** and **.islower()**\n", + "\n", + "If you feel ambitious, explore the Collections module to solve this problem!" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def up_low(s):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Original String : Hello Mr. Rogers, how are you this fine Tuesday?\n", + "No. of Upper case characters : 4\n", + "No. of Lower case Characters : 33\n" + ] + } + ], + "source": [ + "s = 'Hello Mr. Rogers, how are you this fine Tuesday?'\n", + "up_low(s)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "____\n", + "**Write a Python function that takes a list and returns a new list with unique elements of the first list.**\n", + "\n", + " Sample List : [1,1,1,1,2,2,3,3,3,3,4,5]\n", + " Unique List : [1, 2, 3, 4, 5]" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def unique_list(lst):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unique_list([1,1,1,1,2,2,3,3,3,3,4,5])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "____\n", + "**Write a Python function to multiply all the numbers in a list.**\n", + "\n", + " Sample List : [1, 2, 3, -4]\n", + " Expected Output : -24" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def multiply(numbers): \n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-24" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "multiply([1,2,3,-4])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "____\n", + "**Write a Python function that checks whether a word or phrase is palindrome or not.**\n", + "\n", + "Note: A palindrome is word, phrase, or sequence that reads the same backward as forward, e.g., madam,kayak,racecar, or a phrase \"nurses run\". Hint: You may want to check out the .replace() method in a string to help out with dealing with spaces. Also google search how to reverse a string in Python, there are some clever ways to do it with slicing notation." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def palindrome(s):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "palindrome('helleh')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "____\n", + "#### Hard:\n", + "\n", + "**Write a Python function to check whether a string is pangram or not. (Assume the string passed in does not have any punctuation)**\n", + "\n", + " Note : Pangrams are words or sentences containing every letter of the alphabet at least once.\n", + " For example : \"The quick brown fox jumps over the lazy dog\"\n", + "\n", + "Hint: You may want to use .replace() method to get rid of spaces.\n", + "\n", + "Hint: Look at the [string module](https://stackoverflow.com/questions/16060899/alphabet-range-in-python)\n", + "\n", + "Hint: In case you want to use [set comparisons](https://medium.com/better-programming/a-visual-guide-to-set-comparisons-in-python-6ab7edb9ec41)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import string\n", + "\n", + "def ispangram(str1, alphabet=string.ascii_lowercase):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ispangram(\"The quick brown fox jumps over the lazy dog\")" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'abcdefghijklmnopqrstuvwxyz'" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "string.ascii_lowercase" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "#### Great Job!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/03-Methods and Functions/.ipynb_checkpoints/09-Functions and Methods Homework - Solutions-checkpoint.ipynb b/03-Methods and Functions/.ipynb_checkpoints/09-Functions and Methods Homework - Solutions-checkpoint.ipynb new file mode 100644 index 000000000..6fd10540b --- /dev/null +++ b/03-Methods and Functions/.ipynb_checkpoints/09-Functions and Methods Homework - Solutions-checkpoint.ipynb @@ -0,0 +1,461 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "def
then a space followed by the name of the function. Try to keep names relevant, for example len() is a good name for a length() function. Also be careful with names, you wouldn't want to call a function the same name as a [built-in function in Python](https://docs.python.org/2/library/functions.html) (such as len).\n",
+ "We begin with def
then a space followed by the name of the function. Try to keep names relevant, for example len() is a good name for a length() function. Also be careful with names, you wouldn't want to call a function the same name as a [built-in function in Python](https://docs.python.org/3/library/functions.html) (such as len).\n",
"\n",
"Next come a pair of parentheses with a number of arguments separated by a comma. These arguments are the inputs for your function. You'll be able to use these inputs in your function and reference them. After this you put a colon.\n",
"\n",
"Now here is the important step, you must indent to begin the code inside your function correctly. Python makes use of *whitespace* to organize code. Lots of other programing languages do not do this, so keep that in mind.\n",
"\n",
- "Next you'll see the docstring, this is where you write a basic description of the function. Using iPython and iPython Notebooks, you'll be able to read these docstrings by pressing Shift+Tab after a function name. Docstrings are not necessary for simple functions, but it's good practice to put them in so you or other people can easily understand the code you write.\n",
+ "Next you'll see the docstring, this is where you write a basic description of the function. Using Jupyter and Jupyter Notebooks, you'll be able to read these docstrings by pressing Shift+Tab after a function name. Docstrings are not necessary for simple functions, but it's good practice to put them in so you or other people can easily understand the code you write.\n",
"\n",
"After all this you begin writing the code you wish to execute.\n",
"\n",
@@ -63,19 +103,28 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "### Example 1: A simple print 'hello' function"
+ "### Simple example of a function"
]
},
{
"cell_type": "code",
- "execution_count": 2,
- "metadata": {},
+ "execution_count": 4,
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"def say_hello():\n",
" print('hello')"
]
},
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Calling a function with ()"
+ ]
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -85,7 +134,7 @@
},
{
"cell_type": "code",
- "execution_count": 3,
+ "execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -104,23 +153,52 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "### Example 2: A simple greeting function\n",
- "Let's write a function that greets people with their name."
+ "If you forget the parenthesis (), it will simply display the fact that say_hello is a function. Later on we will learn we can actually pass in functions into other functions! But for now, simply remember to call functions with ()."
]
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "return
statement. return
allows a function to *return* a result that can then be stored as a variable, or used in whatever manner a user wants.\n",
"\n",
- "### Example 3: Addition function"
+ "### Example: Addition function"
]
},
{
"cell_type": "code",
"execution_count": 6,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"def add_num(num1,num2):\n",
@@ -178,7 +260,9 @@
{
"cell_type": "code",
"execution_count": 8,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"# Can also save as variable due to return\n",
@@ -233,130 +317,1036 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "Note that because we don't declare variable types in Python, this function could be used to add numbers or sequences together! We'll later learn about adding in checks to make sure a user puts in the correct arguments into a function.\n",
+ "## Very Common Question: \"What is the difference between *return* and *print*?\"\n",
"\n",
- "Let's also start using break
, continue
, and pass
statements in our code. We introduced these during the while
lecture."
+ "**The return keyword allows you to actually save the result of the output of a function as a variable. The print() function simply displays the output to you, but doesn't save it for future use. Let's explore this in more detail**"
]
},
{
- "cell_type": "markdown",
+ "cell_type": "code",
+ "execution_count": 1,
"metadata": {
"collapsed": true
},
+ "outputs": [],
"source": [
- "Finally let's go over a full example of creating a function to check if a number is prime (a common interview exercise).\n",
- "\n",
- "We know a number is prime if that number is only evenly divisible by 1 and itself. Let's write our first version of the function to check all the numbers from 1 to N and perform modulo checks."
+ "def print_result(a,b):\n",
+ " print(a+b)"
]
},
{
"cell_type": "code",
- "execution_count": 11,
- "metadata": {},
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
- "def is_prime(num):\n",
- " '''\n",
- " Naive method of checking for primes. \n",
- " '''\n",
- " for n in range(2,num):\n",
- " if num % n == 0:\n",
- " print(num,'is not prime')\n",
- " break\n",
- " else: # If never mod zero, then prime\n",
- " print(num,'is prime!')"
+ "def return_result(a,b):\n",
+ " return a+b"
]
},
{
"cell_type": "code",
- "execution_count": 12,
+ "execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "16 is not prime\n"
+ "15\n"
]
}
],
"source": [
- "is_prime(16)"
+ "print_result(10,5)"
]
},
{
"cell_type": "code",
- "execution_count": 13,
+ "execution_count": 4,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "15"
+ ]
+ },
+ "execution_count": 4,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# You won't see any output if you run this in a .py script\n",
+ "return_result(10,5)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "**But what happens if we actually want to save this result for later use?**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "17 is prime!\n"
+ "40\n"
]
}
],
"source": [
- "is_prime(17)"
+ "my_result = print_result(20,20)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "my_result"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "NoneType"
+ ]
+ },
+ "execution_count": 7,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "type(my_result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "Note how the else
lines up under for
and not if
. This is because we want the for
loop to exhaust all possibilities in the range before printing our number is prime.\n",
- "\n",
- "Also note how we break the code after the first print statement. As soon as we determine that a number is not prime we break out of the for
loop.\n",
- "\n",
- "We can actually improve this function by only checking to the square root of the target number, and by disregarding all even numbers after checking for 2. We'll also switch to returning a boolean value to get an example of using return statements:"
+ "**Be careful! Notice how print_result() doesn't let you actually save the result to a variable! It only prints it out, with print() returning None for the assignment!**"
]
},
{
"cell_type": "code",
- "execution_count": 14,
- "metadata": {},
+ "execution_count": 8,
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
- "import math\n",
- "\n",
- "def is_prime2(num):\n",
- " '''\n",
- " Better method of checking for primes. \n",
- " '''\n",
- " if num % 2 == 0 and num > 2: \n",
- " return False\n",
- " for i in range(3, int(math.sqrt(num)) + 1, 2):\n",
- " if num % i == 0:\n",
- " return False\n",
- " return True"
+ "my_result = return_result(20,20)"
]
},
{
"cell_type": "code",
- "execution_count": 15,
+ "execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "False"
+ "40"
]
},
- "execution_count": 15,
+ "execution_count": 9,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "my_result"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "80"
+ ]
+ },
+ "execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
- "is_prime2(18)"
+ "my_result + my_result"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Adding Logic to Internal Function Operations\n",
+ "\n",
+ "So far we know quite a bit about constructing logical statements with Python, such as if/else/elif statements, for and while loops, checking if an item is **in** a list or **not in** a list (Useful Operators Lecture). Let's now see how we can perform these operations within a function."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Check if a number is even "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
- "Why don't we have any break
statements? It should be noted that as soon as a function *returns* something, it shuts down. A function can deliver multiple print statements, but it will only obey one return
."
+ "**Recall the mod operator % which returns the remainder after division, if a number is even then mod 2 (% 2) should be == to zero.**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0"
+ ]
+ },
+ "execution_count": 11,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "2 % 2"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0"
+ ]
+ },
+ "execution_count": 12,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "20 % 2"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "1"
+ ]
+ },
+ "execution_count": 14,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "21 % 2"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 15,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "20 % 2 == 0"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 16,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "False"
+ ]
+ },
+ "execution_count": 16,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "21 % 2 == 0"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "** Let's use this to construct a function. Notice how we simply return the boolean check.**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 18,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def even_check(number):\n",
+ " return number % 2 == 0"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 19,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 19,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "even_check(20)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 21,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "False"
+ ]
+ },
+ "execution_count": 21,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "even_check(21)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Check if any number in a list is even\n",
+ "\n",
+ "Let's return a boolean indicating if **any** number in a list is even. Notice here how **return** breaks out of the loop and exits the function"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 25,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def check_even_list(num_list):\n",
+ " # Go through each number\n",
+ " for number in num_list:\n",
+ " # Once we get a \"hit\" on an even number, we return True\n",
+ " if number % 2 == 0:\n",
+ " return True\n",
+ " # Otherwise we don't do anything\n",
+ " else:\n",
+ " pass"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "** Is this enough? NO! We're not returning anything if they are all odds!**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 26,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 26,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "check_even_list([1,2,3])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 27,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "check_even_list([1,1,1])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "** VERY COMMON MISTAKE!! LET'S SEE A COMMON LOGIC ERROR, NOTE THIS IS WRONG!!!**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 28,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def check_even_list(num_list):\n",
+ " # Go through each number\n",
+ " for number in num_list:\n",
+ " # Once we get a \"hit\" on an even number, we return True\n",
+ " if number % 2 == 0:\n",
+ " return True\n",
+ " # This is WRONG! This returns False at the very first odd number!\n",
+ " # It doesn't end up checking the other numbers in the list!\n",
+ " else:\n",
+ " return False"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 30,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "False"
+ ]
+ },
+ "execution_count": 30,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# UH OH! It is returning False after hitting the first 1\n",
+ "check_even_list([1,2,3])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "** Correct Approach: We need to initiate a return False AFTER running through the entire loop**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 31,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def check_even_list(num_list):\n",
+ " # Go through each number\n",
+ " for number in num_list:\n",
+ " # Once we get a \"hit\" on an even number, we return True\n",
+ " if number % 2 == 0:\n",
+ " return True\n",
+ " # Don't do anything if its not even\n",
+ " else:\n",
+ " pass\n",
+ " # Notice the indentation! This ensures we run through the entire for loop \n",
+ " return False"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 32,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 32,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "check_even_list([1,2,3])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 34,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "False"
+ ]
+ },
+ "execution_count": 34,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "check_even_list([1,3,5])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Return all even numbers in a list\n",
+ "\n",
+ "Let's add more complexity, we now will return all the even numbers in a list, otherwise return an empty list."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 35,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def check_even_list(num_list):\n",
+ " \n",
+ " even_numbers = []\n",
+ " \n",
+ " # Go through each number\n",
+ " for number in num_list:\n",
+ " # Once we get a \"hit\" on an even number, we append the even number\n",
+ " if number % 2 == 0:\n",
+ " even_numbers.append(number)\n",
+ " # Don't do anything if its not even\n",
+ " else:\n",
+ " pass\n",
+ " # Notice the indentation! This ensures we run through the entire for loop \n",
+ " return even_numbers"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 36,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[2, 4, 6]"
+ ]
+ },
+ "execution_count": 36,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "check_even_list([1,2,3,4,5,6])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 37,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[]"
+ ]
+ },
+ "execution_count": 37,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "check_even_list([1,3,5])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Returning Tuples for Unpacking"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "** Recall we can loop through a list of tuples and \"unpack\" the values within them**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 38,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "stock_prices = [('AAPL',200),('GOOG',300),('MSFT',400)]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 39,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "('AAPL', 200)\n",
+ "('GOOG', 300)\n",
+ "('MSFT', 400)\n"
+ ]
+ }
+ ],
+ "source": [
+ "for item in stock_prices:\n",
+ " print(item)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 41,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "AAPL\n",
+ "GOOG\n",
+ "MSFT\n"
+ ]
+ }
+ ],
+ "source": [
+ "for stock,price in stock_prices:\n",
+ " print(stock)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 42,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "200\n",
+ "300\n",
+ "400\n"
+ ]
+ }
+ ],
+ "source": [
+ "for stock,price in stock_prices:\n",
+ " print(price)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "**Similarly, functions often return tuples, to easily return multiple results for later use.**"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Let's imagine the following list:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 46,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "work_hours = [('Abby',100),('Billy',400),('Cassie',800)]"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "The employee of the month function will return both the name and number of hours worked for the top performer (judged by number of hours worked)."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 47,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def employee_check(work_hours):\n",
+ " \n",
+ " # Set some max value to intially beat, like zero hours\n",
+ " current_max = 0\n",
+ " # Set some empty value before the loop\n",
+ " employee_of_month = ''\n",
+ " \n",
+ " for employee,hours in work_hours:\n",
+ " if hours > current_max:\n",
+ " current_max = hours\n",
+ " employee_of_month = employee\n",
+ " else:\n",
+ " pass\n",
+ " \n",
+ " # Notice the indentation here\n",
+ " return (employee_of_month,current_max)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 48,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "('Cassie', 800)"
+ ]
+ },
+ "execution_count": 48,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "employee_check(work_hours)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Interactions between functions\n",
+ "\n",
+ "Functions often use results from other functions, let's see a simple example through a guessing game. There will be 3 positions in the list, one of which is an 'O', a function will shuffle the list, another will take a player's guess, and finally another will check to see if it is correct. This is based on the classic carnival game of guessing which cup a red ball is under."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "**How to shuffle a list in Python**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "example = [1,2,3,4,5]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "from random import shuffle"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "# Note shuffle is in-place\n",
+ "shuffle(example)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[3, 1, 4, 5, 2]"
+ ]
+ },
+ "execution_count": 11,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "example"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "**OK, let's create our simple game**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "mylist = [' ','O',' ']"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def shuffle_list(mylist):\n",
+ " # Take in list, and returned shuffle versioned\n",
+ " shuffle(mylist)\n",
+ " \n",
+ " return mylist"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[' ', 'O', ' ']"
+ ]
+ },
+ "execution_count": 14,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "mylist "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[' ', ' ', 'O']"
+ ]
+ },
+ "execution_count": 15,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "shuffle_list(mylist)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 18,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def player_guess():\n",
+ " \n",
+ " guess = ''\n",
+ " \n",
+ " while guess not in ['0','1','2']:\n",
+ " \n",
+ " # Recall input() returns a string\n",
+ " guess = input(\"Pick a number: 0, 1, or 2: \")\n",
+ " \n",
+ " return int(guess) "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 24,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Pick a number: 0, 1, or 2: 1\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "1"
+ ]
+ },
+ "execution_count": 24,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "player_guess()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Now we will check the user's guess. Notice we only print here, since we have no need to save a user's guess or the shuffled list."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 22,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "def check_guess(mylist,guess):\n",
+ " if mylist[guess] == 'O':\n",
+ " print('Correct Guess!')\n",
+ " else:\n",
+ " print('Wrong! Better luck next time')\n",
+ " print(mylist)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Now we create a little setup logic to run all the functions. Notice how they interact with each other!"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 23,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Pick a number: 0, 1, or 2: 1\n",
+ "Wrong! Better luck next time\n",
+ "[' ', ' ', 'O']\n"
+ ]
+ }
+ ],
+ "source": [
+ "# Initial List\n",
+ "mylist = [' ','O',' ']\n",
+ "\n",
+ "# Shuffle It\n",
+ "mixedup_list = shuffle_list(mylist)\n",
+ "\n",
+ "# Get User's Guess\n",
+ "guess = player_guess()\n",
+ "\n",
+ "# Check User's Guess\n",
+ "#------------------------\n",
+ "# Notice how this function takes in the input \n",
+ "# based on the output of other functions!\n",
+ "check_guess(mixedup_list,guess)"
]
},
{
@@ -383,7 +1373,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.6.2"
+ "version": "3.6.6"
}
},
"nbformat": 4,
diff --git a/03-Methods and Functions/03-Function Practice Exercises.ipynb b/03-Methods and Functions/03-Function Practice Exercises.ipynb
index cb906dbe6..e21473093 100644
--- a/03-Methods and Functions/03-Function Practice Exercises.ipynb
+++ b/03-Methods and Functions/03-Function Practice Exercises.ipynb
@@ -1,5 +1,16 @@
{
"cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "___\n",
+ "\n",
+ "import
command.\n",
"\n",
"To import a module, we use the import
command. Check out the full list of built-in modules in the Python standard library [here](https://docs.python.org/3/py-modindex.html).\n",
@@ -212,8 +42,10 @@
},
{
"cell_type": "code",
- "execution_count": 7,
- "metadata": {},
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"# import the library\n",
@@ -222,7 +54,7 @@
},
{
"cell_type": "code",
- "execution_count": 8,
+ "execution_count": 2,
"metadata": {},
"outputs": [
{
@@ -231,7 +63,7 @@
"3"
]
},
- "execution_count": 8,
+ "execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
@@ -253,7 +85,7 @@
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 3,
"metadata": {},
"outputs": [
{
@@ -278,7 +110,7 @@
},
{
"cell_type": "code",
- "execution_count": 10,
+ "execution_count": 4,
"metadata": {},
"outputs": [
{
@@ -332,7 +164,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"# OR could do it this way\n",
@@ -378,7 +212,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.6.2"
+ "version": "3.6.6"
}
},
"nbformat": 4,
diff --git a/06-Modules and Packages/Useful_Info_Notebook.ipynb b/06-Modules and Packages/Useful_Info_Notebook.ipynb
index f7317b76d..b22b6d95e 100644
--- a/06-Modules and Packages/Useful_Info_Notebook.ipynb
+++ b/06-Modules and Packages/Useful_Info_Notebook.ipynb
@@ -4,203 +4,33 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "# Modules and Packages\n",
- "\n",
- "In this section we briefly:\n",
- "* code out a basic module and show how to import it into a Python script\n",
- "* run a Python script from a Jupyter cell\n",
- "* show how command line arguments can be passed into a script\n",
- "\n",
- "Check out the video lectures for more info and resources for this.\n",
- "\n",
- "The best online resource is the official docs:\n",
- "https://docs.python.org/3/tutorial/modules.html#packages\n",
+ "___\n",
"\n",
- "But I really like the info here: https://python4astronomers.github.io/installation/packages.html\n",
- "\n",
- "## Writing modules"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Writing file1.py\n"
- ]
- }
- ],
- "source": [
- "%%writefile file1.py\n",
- "def myfunc(x):\n",
- " return [num for num in range(x) if num%2==0]\n",
- "list1 = myfunc(11)"
+ "import
command.\n",
"\n",
"To import a module, we use the import
command. Check out the full list of built-in modules in the Python standard library [here](https://docs.python.org/3/py-modindex.html).\n",
@@ -212,8 +42,10 @@
},
{
"cell_type": "code",
- "execution_count": 7,
- "metadata": {},
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"# import the library\n",
@@ -222,7 +54,7 @@
},
{
"cell_type": "code",
- "execution_count": 8,
+ "execution_count": 2,
"metadata": {},
"outputs": [
{
@@ -231,7 +63,7 @@
"3"
]
},
- "execution_count": 8,
+ "execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
@@ -253,7 +85,7 @@
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 3,
"metadata": {},
"outputs": [
{
@@ -278,7 +110,7 @@
},
{
"cell_type": "code",
- "execution_count": 10,
+ "execution_count": 4,
"metadata": {},
"outputs": [
{
@@ -332,7 +164,9 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"# OR could do it this way\n",
@@ -378,7 +212,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.6.2"
+ "version": "3.6.6"
}
},
"nbformat": 4,
diff --git a/06-Modules and Packages/__pycache__/file1.cpython-36.pyc b/06-Modules and Packages/__pycache__/file1.cpython-36.pyc
deleted file mode 100644
index 33faa982b..000000000
Binary files a/06-Modules and Packages/__pycache__/file1.cpython-36.pyc and /dev/null differ
diff --git a/06-Modules and Packages/file1.py b/06-Modules and Packages/file1.py
deleted file mode 100644
index 67b593624..000000000
--- a/06-Modules and Packages/file1.py
+++ /dev/null
@@ -1,3 +0,0 @@
-def myfunc(x):
- return [num for num in range(x) if num%2==0]
-list1 = myfunc(11)
\ No newline at end of file
diff --git a/06-Modules and Packages/file2.py b/06-Modules and Packages/file2.py
deleted file mode 100644
index 4b63a64a4..000000000
--- a/06-Modules and Packages/file2.py
+++ /dev/null
@@ -1,3 +0,0 @@
-import file1
-file1.list1.append(12)
-print(file1.list1)
\ No newline at end of file
diff --git a/06-Modules and Packages/file3.py b/06-Modules and Packages/file3.py
deleted file mode 100644
index dcc44eac5..000000000
--- a/06-Modules and Packages/file3.py
+++ /dev/null
@@ -1,4 +0,0 @@
-import sys
-import file1
-num = int(sys.argv[1])
-print(file1.myfunc(num))
\ No newline at end of file
diff --git a/07-Errors and Exception Handling/.ipynb_checkpoints/01-Errors and Exceptions Handling-checkpoint.ipynb b/07-Errors and Exception Handling/.ipynb_checkpoints/01-Errors and Exceptions Handling-checkpoint.ipynb
index c135164e8..f22c75756 100644
--- a/07-Errors and Exception Handling/.ipynb_checkpoints/01-Errors and Exceptions Handling-checkpoint.ipynb
+++ b/07-Errors and Exception Handling/.ipynb_checkpoints/01-Errors and Exceptions Handling-checkpoint.ipynb
@@ -1,5 +1,16 @@
{
"cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "___\n",
+ "\n",
+ "