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", + "
Content Copyright by Pierian Data
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Numbers and more in Python!\n", + "\n", + "In this lecture, we will learn about numbers in Python and how to use them.\n", + "\n", + "We'll learn about the following topics:\n", + "\n", + " 1.) Types of Numbers in Python\n", + " 2.) Basic Arithmetic\n", + " 3.) Differences between classic division and floor division\n", + " 4.) Object Assignment in Python" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Types of numbers\n", + "\n", + "Python has various \"types\" of numbers (numeric literals). We'll mainly focus on integers and floating point numbers.\n", + "\n", + "Integers are just whole numbers, positive or negative. For example: 2 and -2 are examples of integers.\n", + "\n", + "Floating point numbers in Python are notable because they have a decimal point in them, or use an exponential (e) to define the number. For example 2.0 and -2.1 are examples of floating point numbers. 4E2 (4 times 10 to the power of 2) is also an example of a floating point number in Python.\n", + "\n", + "Throughout this course we will be mainly working with integers or simple float number types.\n", + "\n", + "Here is a table of the two main types we will spend most of our time working with some examples:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "\n", + " \n", + " \n", + "\n", + "\n", + "\n", + " \n", + " \n", + "\n", + "\n", + "\n", + " \n", + " \n", + "\n", + "
ExamplesNumber \"Type\"
1,2,-5,1000Integers
1.2,-0.5,2e2,3E2Floating-point numbers
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " \n", + " \n", + "Now let's start with some basic arithmetic." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Basic Arithmetic" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Addition\n", + "2+1" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Subtraction\n", + "2-1" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Multiplication\n", + "2*2" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1.5" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Division\n", + "3/2" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Floor Division\n", + "7//4" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Whoa! What just happened? Last time I checked, 7 divided by 4 equals 1.75 not 1!**\n", + "\n", + "The reason we get this result is because we are using \"*floor*\" division. The // operator (two forward slashes) truncates the decimal without rounding, and returns an integer result." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**So what if we just want the remainder after division?**" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Modulo\n", + "7%4" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "4 goes into 7 once, with a remainder of 3. The % operator returns the remainder after division." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Arithmetic continued" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Powers\n", + "2**3" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2.0" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Can also do roots this way\n", + "4**0.5" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "105" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Order of Operations followed in Python\n", + "2 + 10 * 10 + 3" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "156" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Can use parentheses to specify orders\n", + "(2+10) * (10+3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Variable Assignments\n", + "\n", + "Now that we've seen how to use numbers in Python as a calculator let's see how we can assign names and create variables.\n", + "\n", + "We use a single equals sign to assign labels to variables. Let's see a few examples of how we can do this." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Let's create an object called \"a\" and assign it the number 5\n", + "a = 5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now if I call *a* in my Python script, Python will treat it as the number 5." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Adding the objects\n", + "a+a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "What happens on reassignment? Will Python let us write it over?" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Reassignment\n", + "a = 10" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Check\n", + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Yes! Python allows you to write over assigned variable names. We can also use the variables themselves when doing the reassignment. Here is an example of what I mean:" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Check\n", + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Use A to redefine A\n", + "a = a + a" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Check \n", + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The names you use when creating these labels need to follow a few rules:\n", + "\n", + " 1. Names can not start with a number.\n", + " 2. There can be no spaces in the name, use _ instead.\n", + " 3. Can't use any of these symbols :'\",<>/?|\\()!@#$%^&*~-+\n", + " 4. It's considered best practice (PEP8) that names are lowercase.\n", + " 5. Avoid using the characters 'l' (lowercase letter el), 'O' (uppercase letter oh), \n", + " or 'I' (uppercase letter eye) as single character variable names.\n", + " 6. Avoid using words that have special meaning in Python like \"list\" and \"str\"\n", + "\n", + "\n", + "Using variable names can be a very useful way to keep track of different variables in Python. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Use object names to keep better track of what's going on in your code!\n", + "my_income = 100\n", + "\n", + "tax_rate = 0.1\n", + "\n", + "my_taxes = my_income*tax_rate" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10.0" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Show my taxes!\n", + "my_taxes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So what have we learned? We learned some of the basics of numbers in Python. We also learned how to do arithmetic and use Python as a basic calculator. We then wrapped it up with learning about Variable Assignment in Python.\n", + "\n", + "Up next we'll learn about Strings!" + ] + } + ], + "metadata": { + "anaconda-cloud": {}, + "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/00-Python Object and Data Structure Basics/.ipynb_checkpoints/01-Variable Assignment-checkpoint.ipynb b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/01-Variable Assignment-checkpoint.ipynb index c5af87b91..c869c3ab6 100644 --- a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/01-Variable Assignment-checkpoint.ipynb +++ b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/01-Variable Assignment-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -30,7 +41,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "my_dogs = 2" @@ -59,7 +72,9 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "my_dogs = ['Sammy', 'Frankie']" @@ -110,7 +125,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "a = 5" @@ -146,7 +163,9 @@ { "cell_type": "code", "execution_count": 7, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "a = 10" @@ -210,7 +229,9 @@ { "cell_type": "code", "execution_count": 10, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "a = a + 10" @@ -246,7 +267,9 @@ { "cell_type": "code", "execution_count": 12, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "a += 10" @@ -275,7 +298,9 @@ { "cell_type": "code", "execution_count": 14, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "a *= 2" @@ -340,7 +365,9 @@ { "cell_type": "code", "execution_count": 17, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "a = (1,2)" @@ -377,7 +404,9 @@ { "cell_type": "code", "execution_count": 19, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "my_income = 100\n", @@ -429,7 +458,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/02-Strings-checkpoint.ipynb b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/02-Strings-checkpoint.ipynb index 1ef2117d2..28a42ec48 100644 --- a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/02-Strings-checkpoint.ipynb +++ b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/02-Strings-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -289,7 +300,9 @@ { "cell_type": "code", "execution_count": 10, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Assign s as a string\n", @@ -725,7 +738,9 @@ { "cell_type": "code", "execution_count": 29, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# We can reassign s completely though!\n", @@ -779,7 +794,9 @@ { "cell_type": "code", "execution_count": 32, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "letter = 'z'" @@ -995,7 +1012,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/03-Print Formatting with Strings-checkpoint.ipynb b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/03-Print Formatting with Strings-checkpoint.ipynb index 7661e9f74..7d1e2d568 100644 --- a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/03-Print Formatting with Strings-checkpoint.ipynb +++ b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/03-Print Formatting with Strings-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -705,7 +716,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/04-Lists-checkpoint.ipynb b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/04-Lists-checkpoint.ipynb new file mode 100644 index 000000000..860c2e71c --- /dev/null +++ b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/04-Lists-checkpoint.ipynb @@ -0,0 +1,793 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Lists\n", + "\n", + "Earlier when discussing strings we introduced the concept of a *sequence* in Python. Lists can be thought of the most general version of a *sequence* in Python. Unlike strings, they are mutable, meaning the elements inside a list can be changed!\n", + "\n", + "In this section we will learn about:\n", + " \n", + " 1.) Creating lists\n", + " 2.) Indexing and Slicing Lists\n", + " 3.) Basic List Methods\n", + " 4.) Nesting Lists\n", + " 5.) Introduction to List Comprehensions\n", + "\n", + "Lists are constructed with brackets [] and commas separating every element in the list.\n", + "\n", + "Let's go ahead and see how we can construct lists!" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Assign a list to an variable named my_list\n", + "my_list = [1,2,3]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We just created a list of integers, but lists can actually hold different object types. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_list = ['A string',23,100.232,'o']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Just like strings, the len() function will tell you how many items are in the sequence of the list." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(my_list)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Indexing and Slicing\n", + "Indexing and slicing work just like in strings. Let's make a new list to remind ourselves of how this works:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_list = ['one','two','three',4,5]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'one'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Grab element at index 0\n", + "my_list[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['two', 'three', 4, 5]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Grab index 1 and everything past it\n", + "my_list[1:]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['one', 'two', 'three']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Grab everything UP TO index 3\n", + "my_list[:3]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can also use + to concatenate lists, just like we did for strings." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['one', 'two', 'three', 4, 5, 'new item']" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_list + ['new item']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Note: This doesn't actually change the original list!" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['one', 'two', 'three', 4, 5]" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_list" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You would have to reassign the list to make the change permanent." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Reassign\n", + "my_list = my_list + ['add new item permanently']" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['one', 'two', 'three', 4, 5, 'add new item permanently']" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_list" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can also use the * for a duplication method similar to strings:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['one',\n", + " 'two',\n", + " 'three',\n", + " 4,\n", + " 5,\n", + " 'add new item permanently',\n", + " 'one',\n", + " 'two',\n", + " 'three',\n", + " 4,\n", + " 5,\n", + " 'add new item permanently']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Make the list double\n", + "my_list * 2" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['one', 'two', 'three', 4, 5, 'add new item permanently']" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Again doubling not permanent\n", + "my_list" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Basic List Methods\n", + "\n", + "If you are familiar with another programming language, you might start to draw parallels between arrays in another language and lists in Python. Lists in Python however, tend to be more flexible than arrays in other languages for a two good reasons: they have no fixed size (meaning we don't have to specify how big a list will be), and they have no fixed type constraint (like we've seen above).\n", + "\n", + "Let's go ahead and explore some more special methods for lists:" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Create a new list\n", + "list1 = [1,2,3]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Use the **append** method to permanently add an item to the end of a list:" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Append\n", + "list1.append('append me!')" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 'append me!']" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Show\n", + "list1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Use **pop** to \"pop off\" an item from the list. By default pop takes off the last index, but you can also specify which index to pop off. Let's see an example:" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Pop off the 0 indexed item\n", + "list1.pop(0)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 3, 'append me!']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Show\n", + "list1" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Assign the popped element, remember default popped index is -1\n", + "popped_item = list1.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'append me!'" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "popped_item" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 3]" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Show remaining list\n", + "list1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "It should also be noted that lists indexing will return an error if there is no element at that index. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "ename": "IndexError", + "evalue": "list index out of range", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mlist1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mIndexError\u001b[0m: list index out of range" + ] + } + ], + "source": [ + "list1[100]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can use the **sort** method and the **reverse** methods to also effect your lists:" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "new_list = ['a','e','x','b','c']" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a', 'e', 'x', 'b', 'c']" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Show\n", + "new_list" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Use reverse to reverse order (this is permanent!)\n", + "new_list.reverse()" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['c', 'b', 'x', 'e', 'a']" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_list" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Use sort to sort the list (in this case alphabetical order, but for numbers it will go ascending)\n", + "new_list.sort()" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a', 'b', 'c', 'e', 'x']" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_list" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Nesting Lists\n", + "A great feature of of Python data structures is that they support *nesting*. This means we can have data structures within data structures. For example: A list inside a list.\n", + "\n", + "Let's see how this works!" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Let's make three lists\n", + "lst_1=[1,2,3]\n", + "lst_2=[4,5,6]\n", + "lst_3=[7,8,9]\n", + "\n", + "# Make a list of lists to form a matrix\n", + "matrix = [lst_1,lst_2,lst_3]" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Show\n", + "matrix" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can again use indexing to grab elements, but now there are two levels for the index. The items in the matrix object, and then the items inside that list!" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3]" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Grab first item in matrix object\n", + "matrix[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Grab first item of the first item in the matrix object\n", + "matrix[0][0]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# List Comprehensions\n", + "Python has an advanced feature called list comprehensions. They allow for quick construction of lists. To fully understand list comprehensions we need to understand for loops. So don't worry if you don't completely understand this section, and feel free to just skip it since we will return to this topic later.\n", + "\n", + "But in case you want to know now, here are a few examples!" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Build a list comprehension by deconstructing a for loop within a []\n", + "first_col = [row[0] for row in matrix]" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4, 7]" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "first_col" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We used a list comprehension here to grab the first element of every row in the matrix object. We will cover this in much more detail later on!\n", + "\n", + "For more advanced methods and features of lists in Python, check out the Advanced Lists section later on in this 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/00-Python Object and Data Structure Basics/.ipynb_checkpoints/05-Dictionaries-checkpoint.ipynb b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/05-Dictionaries-checkpoint.ipynb new file mode 100644 index 000000000..00bb6dab8 --- /dev/null +++ b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/05-Dictionaries-checkpoint.ipynb @@ -0,0 +1,467 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Dictionaries\n", + "\n", + "We've been learning about *sequences* in Python but now we're going to switch gears and learn about *mappings* in Python. If you're familiar with other languages you can think of these Dictionaries as hash tables. \n", + "\n", + "This section will serve as a brief introduction to dictionaries and consist of:\n", + "\n", + " 1.) Constructing a Dictionary\n", + " 2.) Accessing objects from a dictionary\n", + " 3.) Nesting Dictionaries\n", + " 4.) Basic Dictionary Methods\n", + "\n", + "So what are mappings? Mappings are a collection of objects that are stored by a *key*, unlike a sequence that stored objects by their relative position. This is an important distinction, since mappings won't retain order since they have objects defined by a key.\n", + "\n", + "A Python dictionary consists of a key and then an associated value. That value can be almost any Python object.\n", + "\n", + "\n", + "## Constructing a Dictionary\n", + "Let's see how we can construct dictionaries to get a better understanding of how they work!" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Make a dictionary with {} and : to signify a key and a value\n", + "my_dict = {'key1':'value1','key2':'value2'}" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'value2'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Call values by their key\n", + "my_dict['key2']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Its important to note that dictionaries are very flexible in the data types they can hold. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_dict = {'key1':123,'key2':[12,23,33],'key3':['item0','item1','item2']}" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['item0', 'item1', 'item2']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Let's call items from the dictionary\n", + "my_dict['key3']" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'item0'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Can call an index on that value\n", + "my_dict['key3'][0]" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'ITEM0'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Can then even call methods on that value\n", + "my_dict['key3'][0].upper()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can affect the values of a key as well. For instance:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "123" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_dict['key1']" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Subtract 123 from the value\n", + "my_dict['key1'] = my_dict['key1'] - 123" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Check\n", + "my_dict['key1']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A quick note, Python has a built-in method of doing a self subtraction or addition (or multiplication or division). We could have also used += or -= for the above statement. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-123" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Set the object equal to itself minus 123 \n", + "my_dict['key1'] -= 123\n", + "my_dict['key1']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can also create keys by assignment. For instance if we started off with an empty dictionary, we could continually add to it:" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Create a new dictionary\n", + "d = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Create a new key through assignment\n", + "d['animal'] = 'Dog'" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Can do this with any object\n", + "d['answer'] = 42" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'animal': 'Dog', 'answer': 42}" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Show\n", + "d" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Nesting with Dictionaries\n", + "\n", + "Hopefully you're starting to see how powerful Python is with its flexibility of nesting objects and calling methods on them. Let's see a dictionary nested inside a dictionary:" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Dictionary nested inside a dictionary nested inside a dictionary\n", + "d = {'key1':{'nestkey':{'subnestkey':'value'}}}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Wow! That's a quite the inception of dictionaries! Let's see how we can grab that value:" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'value'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Keep calling the keys\n", + "d['key1']['nestkey']['subnestkey']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## A few Dictionary Methods\n", + "\n", + "There are a few methods we can call on a dictionary. Let's get a quick introduction to a few of them:" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Create a typical dictionary\n", + "d = {'key1':1,'key2':2,'key3':3}" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['key1', 'key2', 'key3'])" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Method to return a list of all keys \n", + "d.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_values([1, 2, 3])" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Method to grab all values\n", + "d.values()" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_items([('key1', 1), ('key2', 2), ('key3', 3)])" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Method to return tuples of all items (we'll learn about tuples soon)\n", + "d.items()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Hopefully you now have a good basic understanding how to construct dictionaries. There's a lot more to go into here, but we will revisit dictionaries at later time. After this section all you need to know is how to create a dictionary and how to retrieve values from it." + ] + } + ], + "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/00-Python Object and Data Structure Basics/.ipynb_checkpoints/06-Tuples-checkpoint.ipynb b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/06-Tuples-checkpoint.ipynb new file mode 100644 index 000000000..9d2b050e1 --- /dev/null +++ b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/06-Tuples-checkpoint.ipynb @@ -0,0 +1,279 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Tuples\n", + "\n", + "In Python tuples are very similar to lists, however, unlike lists they are *immutable* meaning they can not be changed. You would use tuples to present things that shouldn't be changed, such as days of the week, or dates on a calendar. \n", + "\n", + "In this section, we will get a brief overview of the following:\n", + "\n", + " 1.) Constructing Tuples\n", + " 2.) Basic Tuple Methods\n", + " 3.) Immutability\n", + " 4.) When to Use Tuples\n", + "\n", + "You'll have an intuition of how to use tuples based on what you've learned about lists. We can treat them very similarly with the major distinction being that tuples are immutable.\n", + "\n", + "## Constructing Tuples\n", + "\n", + "The construction of a tuples use () with elements separated by commas. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Create a tuple\n", + "t = (1,2,3)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Check len just like a list\n", + "len(t)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('one', 2)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Can also mix object types\n", + "t = ('one',2)\n", + "\n", + "# Show\n", + "t" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'one'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Use indexing just like we did in lists\n", + "t[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Slicing just like a list\n", + "t[-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Basic Tuple Methods\n", + "\n", + "Tuples have built-in methods, but not as many as lists do. Let's look at two of them:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Use .index to enter a value and return the index\n", + "t.index('one')" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Use .count to count the number of times a value appears\n", + "t.count('one')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Immutability\n", + "\n", + "It can't be stressed enough that tuples are immutable. To drive that point home:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m \u001b[1;34m'change'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "t[0]= 'change'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Because of this immutability, tuples can't grow. Once a tuple is made we can not add to it." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'tuple' object has no attribute 'append'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'nope'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" + ] + } + ], + "source": [ + "t.append('nope')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## When to use Tuples\n", + "\n", + "You may be wondering, \"Why bother using tuples when they have fewer available methods?\" To be honest, tuples are not used as often as lists in programming, but are used when immutability is necessary. If in your program you are passing around an object and need to make sure it does not get changed, then a tuple becomes your solution. It provides a convenient source of data integrity.\n", + "\n", + "You should now be able to create and use tuples in your programming as well as have an understanding of their immutability.\n", + "\n", + "Up next Files!" + ] + } + ], + "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/00-Python Object and Data Structure Basics/.ipynb_checkpoints/07-Sets and Booleans-checkpoint.ipynb b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/07-Sets and Booleans-checkpoint.ipynb index f0749fde0..6951ed42b 100644 --- a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/07-Sets and Booleans-checkpoint.ipynb +++ b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/07-Sets and Booleans-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -303,7 +314,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.1" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/08-Files-checkpoint.ipynb b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/08-Files-checkpoint.ipynb index 1932220d1..8bc0c1551 100644 --- a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/08-Files-checkpoint.ipynb +++ b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/08-Files-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -548,7 +559,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.1" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/09-Objects and Data Structures Assessment Test-checkpoint.ipynb b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/09-Objects and Data Structures Assessment Test-checkpoint.ipynb index 149e61af2..6b94d5182 100644 --- a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/09-Objects and Data Structures Assessment Test-checkpoint.ipynb +++ b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/09-Objects and Data Structures Assessment Test-checkpoint.ipynb @@ -4,6 +4,11 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
\n", "# Objects and Data Structures Assessment Test" ] }, @@ -24,13 +29,15 @@ "collapsed": true }, "source": [ - "Write a brief description of all the following Object Types and Data Structures we've learned about: " + "Write (or just say out loud to yourself) a brief description of all the following Object Types and Data Structures we've learned about. You can edit the cell below by double clicking on it. Really this is just to test if you know the difference between these, so feel free to just think about it, since your answers are self-graded." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ + "Double Click HERE to edit this markdown cell and write answers.\n", + "\n", "Numbers:\n", "\n", "Strings:\n", @@ -56,7 +63,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [] }, @@ -76,7 +85,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [] }, @@ -97,7 +108,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Square root:\n" @@ -106,7 +119,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Square:\n" @@ -129,7 +144,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "s = 'hello'\n", @@ -147,7 +164,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "s ='hello'\n", @@ -165,7 +184,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "s ='hello'\n", @@ -178,7 +199,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Method 2:\n", @@ -202,7 +225,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Method 1:\n" @@ -211,7 +236,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Method 2:\n" @@ -227,7 +254,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "list3 = [1,2,[3,4,'hello']]\n", @@ -244,7 +273,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "list4 = [5,3,4,6,1]\n", @@ -268,7 +299,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "d = {'simple_key':'hello'}\n", @@ -278,7 +311,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "d = {'k1':{'k2':'hello'}}\n", @@ -288,7 +323,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Getting a little tricker\n", @@ -300,7 +337,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# This will be hard and annoying!\n", @@ -359,7 +398,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "list5 = [1,2,2,33,4,4,11,22,3,3,2]\n", @@ -427,7 +468,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Answer before running cell\n", @@ -437,7 +480,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Answer before running cell\n", @@ -447,7 +492,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Answer before running cell\n", @@ -457,7 +504,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Answer before running cell\n", @@ -467,7 +516,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Answer before running cell\n", @@ -484,7 +535,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# two nested lists\n", @@ -520,7 +573,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/10-Objects and Data Structures Assessment Test-Solution-checkpoint.ipynb b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/10-Objects and Data Structures Assessment Test-Solution-checkpoint.ipynb index 952dfa080..ddcd3f48d 100644 --- a/00-Python Object and Data Structure Basics/.ipynb_checkpoints/10-Objects and Data Structures Assessment Test-Solution-checkpoint.ipynb +++ b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/10-Objects and Data Structures Assessment Test-Solution-checkpoint.ipynb @@ -4,6 +4,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
\n", + "\n", "# Objects and Data Structures Assessment Test" ] }, @@ -400,7 +406,9 @@ { "cell_type": "code", "execution_count": 13, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "list3 = [1,2,[3,4,'hello']]" @@ -409,7 +417,9 @@ { "cell_type": "code", "execution_count": 14, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "list3[2][2] = 'goodbye'" @@ -445,7 +455,9 @@ { "cell_type": "code", "execution_count": 16, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "list4 = [5,3,4,6,1]" @@ -557,7 +569,9 @@ { "cell_type": "code", "execution_count": 21, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Getting a little tricker\n", @@ -588,7 +602,9 @@ { "cell_type": "code", "execution_count": 23, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# This will be hard and annoying!\n", @@ -661,7 +677,9 @@ { "cell_type": "code", "execution_count": 25, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "t = (1,2,3)" @@ -698,7 +716,9 @@ { "cell_type": "code", "execution_count": 26, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "list5 = [1,2,2,33,4,4,11,22,3,3,2]" @@ -943,7 +963,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/01-Numbers.ipynb b/00-Python Object and Data Structure Basics/01-Numbers.ipynb index bc8e78aa4..d840c0184 100644 --- a/00-Python Object and Data Structure Basics/01-Numbers.ipynb +++ b/00-Python Object and Data Structure Basics/01-Numbers.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -325,7 +336,9 @@ { "cell_type": "code", "execution_count": 11, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Let's create an object called \"a\" and assign it the number 5\n", @@ -370,7 +383,9 @@ { "cell_type": "code", "execution_count": 13, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Reassignment\n", @@ -429,7 +444,9 @@ { "cell_type": "code", "execution_count": 16, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Use A to redefine A\n", @@ -478,7 +495,9 @@ { "cell_type": "code", "execution_count": 18, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Use object names to keep better track of what's going on in your code!\n", @@ -537,7 +556,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/01-Variable Assignment.ipynb b/00-Python Object and Data Structure Basics/01-Variable Assignment.ipynb index c5af87b91..c869c3ab6 100644 --- a/00-Python Object and Data Structure Basics/01-Variable Assignment.ipynb +++ b/00-Python Object and Data Structure Basics/01-Variable Assignment.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -30,7 +41,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "my_dogs = 2" @@ -59,7 +72,9 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "my_dogs = ['Sammy', 'Frankie']" @@ -110,7 +125,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "a = 5" @@ -146,7 +163,9 @@ { "cell_type": "code", "execution_count": 7, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "a = 10" @@ -210,7 +229,9 @@ { "cell_type": "code", "execution_count": 10, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "a = a + 10" @@ -246,7 +267,9 @@ { "cell_type": "code", "execution_count": 12, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "a += 10" @@ -275,7 +298,9 @@ { "cell_type": "code", "execution_count": 14, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "a *= 2" @@ -340,7 +365,9 @@ { "cell_type": "code", "execution_count": 17, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "a = (1,2)" @@ -377,7 +404,9 @@ { "cell_type": "code", "execution_count": 19, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "my_income = 100\n", @@ -429,7 +458,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/02-Strings.ipynb b/00-Python Object and Data Structure Basics/02-Strings.ipynb index 1ef2117d2..28a42ec48 100644 --- a/00-Python Object and Data Structure Basics/02-Strings.ipynb +++ b/00-Python Object and Data Structure Basics/02-Strings.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -289,7 +300,9 @@ { "cell_type": "code", "execution_count": 10, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Assign s as a string\n", @@ -725,7 +738,9 @@ { "cell_type": "code", "execution_count": 29, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# We can reassign s completely though!\n", @@ -779,7 +794,9 @@ { "cell_type": "code", "execution_count": 32, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "letter = 'z'" @@ -995,7 +1012,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/03-Print Formatting with Strings.ipynb b/00-Python Object and Data Structure Basics/03-Print Formatting with Strings.ipynb index 7661e9f74..7d1e2d568 100644 --- a/00-Python Object and Data Structure Basics/03-Print Formatting with Strings.ipynb +++ b/00-Python Object and Data Structure Basics/03-Print Formatting with Strings.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -705,7 +716,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/04-Lists.ipynb b/00-Python Object and Data Structure Basics/04-Lists.ipynb index 332c404b2..860c2e71c 100644 --- a/00-Python Object and Data Structure Basics/04-Lists.ipynb +++ b/00-Python Object and Data Structure Basics/04-Lists.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -24,7 +35,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Assign a list to an variable named my_list\n", @@ -41,7 +54,9 @@ { "cell_type": "code", "execution_count": 2, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "my_list = ['A string',23,100.232,'o']" @@ -85,7 +100,9 @@ { "cell_type": "code", "execution_count": 4, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "my_list = ['one','two','three',4,5]" @@ -218,7 +235,9 @@ { "cell_type": "code", "execution_count": 10, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Reassign\n", @@ -319,7 +338,9 @@ { "cell_type": "code", "execution_count": 14, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Create a new list\n", @@ -336,7 +357,9 @@ { "cell_type": "code", "execution_count": 15, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Append\n", @@ -416,7 +439,9 @@ { "cell_type": "code", "execution_count": 19, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Assign the popped element, remember default popped index is -1\n", @@ -502,7 +527,9 @@ { "cell_type": "code", "execution_count": 23, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "new_list = ['a','e','x','b','c']" @@ -532,7 +559,9 @@ { "cell_type": "code", "execution_count": 25, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Use reverse to reverse order (this is permanent!)\n", @@ -562,7 +591,9 @@ { "cell_type": "code", "execution_count": 27, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Use sort to sort the list (in this case alphabetical order, but for numbers it will go ascending)\n", @@ -602,7 +633,9 @@ { "cell_type": "code", "execution_count": 29, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Let's make three lists\n", @@ -697,7 +730,9 @@ { "cell_type": "code", "execution_count": 33, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Build a list comprehension by deconstructing a for loop within a []\n", @@ -750,7 +785,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/05-Dictionaries.ipynb b/00-Python Object and Data Structure Basics/05-Dictionaries.ipynb index 3207f0172..00bb6dab8 100644 --- a/00-Python Object and Data Structure Basics/05-Dictionaries.ipynb +++ b/00-Python Object and Data Structure Basics/05-Dictionaries.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -27,7 +38,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Make a dictionary with {} and : to signify a key and a value\n", @@ -65,7 +78,9 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "my_dict = {'key1':123,'key2':[12,23,33],'key3':['item0','item1','item2']}" @@ -164,7 +179,9 @@ { "cell_type": "code", "execution_count": 8, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Subtract 123 from the value\n", @@ -231,7 +248,9 @@ { "cell_type": "code", "execution_count": 11, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Create a new dictionary\n", @@ -241,7 +260,9 @@ { "cell_type": "code", "execution_count": 12, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Create a new key through assignment\n", @@ -251,7 +272,9 @@ { "cell_type": "code", "execution_count": 13, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Can do this with any object\n", @@ -291,7 +314,9 @@ { "cell_type": "code", "execution_count": 15, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Dictionary nested inside a dictionary nested inside a dictionary\n", @@ -338,7 +363,9 @@ { "cell_type": "code", "execution_count": 17, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Create a typical dictionary\n", @@ -432,7 +459,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/06-Tuples.ipynb b/00-Python Object and Data Structure Basics/06-Tuples.ipynb index 96907b165..9d2b050e1 100644 --- a/00-Python Object and Data Structure Basics/06-Tuples.ipynb +++ b/00-Python Object and Data Structure Basics/06-Tuples.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -25,7 +36,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Create a tuple\n", @@ -238,7 +251,7 @@ "\n", "You should now be able to create and use tuples in your programming as well as have an understanding of their immutability.\n", "\n", - "Up next Sets and Booleans!!" + "Up next Files!" ] } ], @@ -258,7 +271,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/07-Sets and Booleans.ipynb b/00-Python Object and Data Structure Basics/07-Sets and Booleans.ipynb index f0749fde0..6951ed42b 100644 --- a/00-Python Object and Data Structure Basics/07-Sets and Booleans.ipynb +++ b/00-Python Object and Data Structure Basics/07-Sets and Booleans.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -303,7 +314,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.1" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/08-Files.ipynb b/00-Python Object and Data Structure Basics/08-Files.ipynb index e5aa6cbb8..8bc0c1551 100644 --- a/00-Python Object and Data Structure Basics/08-Files.ipynb +++ b/00-Python Object and Data Structure Basics/08-Files.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -548,7 +559,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/09-Objects and Data Structures Assessment Test.ipynb b/00-Python Object and Data Structure Basics/09-Objects and Data Structures Assessment Test.ipynb index 149e61af2..6b94d5182 100644 --- a/00-Python Object and Data Structure Basics/09-Objects and Data Structures Assessment Test.ipynb +++ b/00-Python Object and Data Structure Basics/09-Objects and Data Structures Assessment Test.ipynb @@ -4,6 +4,11 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
\n", "# Objects and Data Structures Assessment Test" ] }, @@ -24,13 +29,15 @@ "collapsed": true }, "source": [ - "Write a brief description of all the following Object Types and Data Structures we've learned about: " + "Write (or just say out loud to yourself) a brief description of all the following Object Types and Data Structures we've learned about. You can edit the cell below by double clicking on it. Really this is just to test if you know the difference between these, so feel free to just think about it, since your answers are self-graded." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ + "Double Click HERE to edit this markdown cell and write answers.\n", + "\n", "Numbers:\n", "\n", "Strings:\n", @@ -56,7 +63,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [] }, @@ -76,7 +85,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [] }, @@ -97,7 +108,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Square root:\n" @@ -106,7 +119,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Square:\n" @@ -129,7 +144,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "s = 'hello'\n", @@ -147,7 +164,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "s ='hello'\n", @@ -165,7 +184,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "s ='hello'\n", @@ -178,7 +199,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Method 2:\n", @@ -202,7 +225,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Method 1:\n" @@ -211,7 +236,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Method 2:\n" @@ -227,7 +254,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "list3 = [1,2,[3,4,'hello']]\n", @@ -244,7 +273,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "list4 = [5,3,4,6,1]\n", @@ -268,7 +299,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "d = {'simple_key':'hello'}\n", @@ -278,7 +311,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "d = {'k1':{'k2':'hello'}}\n", @@ -288,7 +323,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Getting a little tricker\n", @@ -300,7 +337,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# This will be hard and annoying!\n", @@ -359,7 +398,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "list5 = [1,2,2,33,4,4,11,22,3,3,2]\n", @@ -427,7 +468,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Answer before running cell\n", @@ -437,7 +480,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Answer before running cell\n", @@ -447,7 +492,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Answer before running cell\n", @@ -457,7 +504,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Answer before running cell\n", @@ -467,7 +516,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Answer before running cell\n", @@ -484,7 +535,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# two nested lists\n", @@ -520,7 +573,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/00-Python Object and Data Structure Basics/10-Objects and Data Structures Assessment Test-Solution.ipynb b/00-Python Object and Data Structure Basics/10-Objects and Data Structures Assessment Test-Solution.ipynb index 952dfa080..ddcd3f48d 100644 --- a/00-Python Object and Data Structure Basics/10-Objects and Data Structures Assessment Test-Solution.ipynb +++ b/00-Python Object and Data Structure Basics/10-Objects and Data Structures Assessment Test-Solution.ipynb @@ -4,6 +4,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
\n", + "\n", "# Objects and Data Structures Assessment Test" ] }, @@ -400,7 +406,9 @@ { "cell_type": "code", "execution_count": 13, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "list3 = [1,2,[3,4,'hello']]" @@ -409,7 +417,9 @@ { "cell_type": "code", "execution_count": 14, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "list3[2][2] = 'goodbye'" @@ -445,7 +455,9 @@ { "cell_type": "code", "execution_count": 16, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "list4 = [5,3,4,6,1]" @@ -557,7 +569,9 @@ { "cell_type": "code", "execution_count": 21, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Getting a little tricker\n", @@ -588,7 +602,9 @@ { "cell_type": "code", "execution_count": 23, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# This will be hard and annoying!\n", @@ -661,7 +677,9 @@ { "cell_type": "code", "execution_count": 25, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "t = (1,2,3)" @@ -698,7 +716,9 @@ { "cell_type": "code", "execution_count": 26, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "list5 = [1,2,2,33,4,4,11,22,3,3,2]" @@ -943,7 +963,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/01-Python Comparison Operators/.ipynb_checkpoints/01-Comparison Operators-checkpoint.ipynb b/01-Python Comparison Operators/.ipynb_checkpoints/01-Comparison Operators-checkpoint.ipynb index d14ff8c99..ea8d0dd79 100644 --- a/01-Python Comparison Operators/.ipynb_checkpoints/01-Comparison Operators-checkpoint.ipynb +++ b/01-Python Comparison Operators/.ipynb_checkpoints/01-Comparison Operators-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -368,7 +379,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/01-Python Comparison Operators/.ipynb_checkpoints/02-Chained Comparison Operators-checkpoint.ipynb b/01-Python Comparison Operators/.ipynb_checkpoints/02-Chained Comparison Operators-checkpoint.ipynb index 7a567d9bc..bfc46eaf6 100644 --- a/01-Python Comparison Operators/.ipynb_checkpoints/02-Chained Comparison Operators-checkpoint.ipynb +++ b/01-Python Comparison Operators/.ipynb_checkpoints/02-Chained Comparison Operators-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -194,7 +205,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/01-Python Comparison Operators/01-Comparison Operators.ipynb b/01-Python Comparison Operators/01-Comparison Operators.ipynb index d14ff8c99..ea8d0dd79 100644 --- a/01-Python Comparison Operators/01-Comparison Operators.ipynb +++ b/01-Python Comparison Operators/01-Comparison Operators.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -368,7 +379,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/01-Python Comparison Operators/02-Chained Comparison Operators.ipynb b/01-Python Comparison Operators/02-Chained Comparison Operators.ipynb index 7a567d9bc..bfc46eaf6 100644 --- a/01-Python Comparison Operators/02-Chained Comparison Operators.ipynb +++ b/01-Python Comparison Operators/02-Chained Comparison Operators.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -194,7 +205,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/.ipynb_checkpoints/01-Introduction to Python Statements-checkpoint.ipynb b/02-Python Statements/.ipynb_checkpoints/01-Introduction to Python Statements-checkpoint.ipynb index 4de998b19..98f975083 100644 --- a/02-Python Statements/.ipynb_checkpoints/01-Introduction to Python Statements-checkpoint.ipynb +++ b/02-Python Statements/.ipynb_checkpoints/01-Introduction to Python Statements-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": { @@ -117,7 +128,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/.ipynb_checkpoints/02-if, elif, and else Statements-checkpoint.ipynb b/02-Python Statements/.ipynb_checkpoints/02-if, elif, and else Statements-checkpoint.ipynb index 262bd6415..cad56d2a4 100644 --- a/02-Python Statements/.ipynb_checkpoints/02-if, elif, and else Statements-checkpoint.ipynb +++ b/02-Python Statements/.ipynb_checkpoints/02-if, elif, and else Statements-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -200,7 +211,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/.ipynb_checkpoints/03-for Loops-checkpoint.ipynb b/02-Python Statements/.ipynb_checkpoints/03-for Loops-checkpoint.ipynb index 61f54fec6..9a0e0c94b 100644 --- a/02-Python Statements/.ipynb_checkpoints/03-for Loops-checkpoint.ipynb +++ b/02-Python Statements/.ipynb_checkpoints/03-for Loops-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -32,7 +43,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# We'll learn how to automate this sort of list in the next lecture\n", @@ -383,7 +396,9 @@ { "cell_type": "code", "execution_count": 13, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "list2 = [(2,4),(6,8),(10,12)]" @@ -447,7 +462,9 @@ { "cell_type": "code", "execution_count": 16, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "d = {'k1':1,'k2':2,'k3':3}" @@ -619,7 +636,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/.ipynb_checkpoints/04-while Loops-checkpoint.ipynb b/02-Python Statements/.ipynb_checkpoints/04-while Loops-checkpoint.ipynb index f8ff55903..322681c92 100644 --- a/02-Python Statements/.ipynb_checkpoints/04-while Loops-checkpoint.ipynb +++ b/02-Python Statements/.ipynb_checkpoints/04-while Loops-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": { @@ -254,7 +265,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# DO NOT RUN THIS CODE!!!! \n", @@ -288,7 +301,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/.ipynb_checkpoints/05-Useful-Operators-checkpoint.ipynb b/02-Python Statements/.ipynb_checkpoints/05-Useful-Operators-checkpoint.ipynb index 632406a02..99084c6eb 100644 --- a/02-Python Statements/.ipynb_checkpoints/05-Useful-Operators-checkpoint.ipynb +++ b/02-Python Statements/.ipynb_checkpoints/05-Useful-Operators-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -319,7 +330,7 @@ "source": [ "## in operator\n", "\n", - "We've already seen the **in** keyword durng the for loop, but we can also use it to quickly check if an object is in a list" + "We've already seen the **in** keyword during the for loop, but we can also use it to quickly check if an object is in a list" ] }, { @@ -362,6 +373,55 @@ "'x' in [1,2,3]" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## not in\n", + "\n", + "We can combine **in** with a **not** operator, to check if some object or variable is not present in a list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'x' not in ['x','y','z']" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'x' not in [1,2,3]" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -579,7 +639,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.1" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/.ipynb_checkpoints/06-List Comprehensions-checkpoint.ipynb b/02-Python Statements/.ipynb_checkpoints/06-List Comprehensions-checkpoint.ipynb index 256271bad..db21a0116 100644 --- a/02-Python Statements/.ipynb_checkpoints/06-List Comprehensions-checkpoint.ipynb +++ b/02-Python Statements/.ipynb_checkpoints/06-List Comprehensions-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": { @@ -17,7 +28,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Grab every letter in string\n", @@ -58,7 +71,9 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Square numbers in range and turn into list\n", @@ -96,7 +111,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Check for even numbers in a range\n", @@ -209,7 +226,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/.ipynb_checkpoints/07-Statements Assessment Test-checkpoint.ipynb b/02-Python Statements/.ipynb_checkpoints/07-Statements Assessment Test-checkpoint.ipynb index 4d9ee290f..26988bd8f 100644 --- a/02-Python Statements/.ipynb_checkpoints/07-Statements Assessment Test-checkpoint.ipynb +++ b/02-Python Statements/.ipynb_checkpoints/07-Statements Assessment Test-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": { @@ -20,7 +31,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "collapsed": true }, @@ -31,8 +42,10 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, + "execution_count": null, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "#Code here" @@ -48,8 +61,10 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": {}, + "execution_count": null, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "#Code Here" @@ -65,20 +80,11 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[]" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], "source": [ "#Code in this cell\n", "[]" @@ -94,7 +100,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": { "collapsed": true }, @@ -105,7 +111,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "collapsed": true }, @@ -125,7 +131,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "#Code in this cell" @@ -141,7 +149,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": { "collapsed": true }, @@ -152,7 +160,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "collapsed": true }, @@ -185,7 +193,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/.ipynb_checkpoints/08-Statements Assessment Test - Solutions-checkpoint.ipynb b/02-Python Statements/.ipynb_checkpoints/08-Statements Assessment Test - Solutions-checkpoint.ipynb index bf90566b8..e760ca199 100644 --- a/02-Python Statements/.ipynb_checkpoints/08-Statements Assessment Test - Solutions-checkpoint.ipynb +++ b/02-Python Statements/.ipynb_checkpoints/08-Statements Assessment Test - Solutions-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": { @@ -20,7 +31,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "st = 'Print only the words that start with s in this sentence'" @@ -114,7 +127,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "st = 'Print every word in this sentence that has an even number of letters'" @@ -158,7 +173,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "for num in range(1,101):\n", @@ -183,7 +200,9 @@ { "cell_type": "code", "execution_count": 7, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "st = 'Create a list of the first letters of every word in this string'" @@ -233,7 +252,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/.ipynb_checkpoints/09-Guessing Game Challenge-checkpoint.ipynb b/02-Python Statements/.ipynb_checkpoints/09-Guessing Game Challenge-checkpoint.ipynb index 97f6fb7eb..4a32dfceb 100644 --- a/02-Python Statements/.ipynb_checkpoints/09-Guessing Game Challenge-checkpoint.ipynb +++ b/02-Python Statements/.ipynb_checkpoints/09-Guessing Game Challenge-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -36,7 +47,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [] }, @@ -50,7 +63,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [] }, @@ -66,7 +81,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [] }, @@ -80,7 +97,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "while True:\n", @@ -103,7 +122,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "while True:\n", @@ -146,7 +167,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/.ipynb_checkpoints/10-Guessing Game Challenge - Solution-checkpoint.ipynb b/02-Python Statements/.ipynb_checkpoints/10-Guessing Game Challenge - Solution-checkpoint.ipynb index 3df229e00..b8b852e15 100644 --- a/02-Python Statements/.ipynb_checkpoints/10-Guessing Game Challenge - Solution-checkpoint.ipynb +++ b/02-Python Statements/.ipynb_checkpoints/10-Guessing Game Challenge - Solution-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -34,7 +45,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "import random\n", @@ -90,7 +103,9 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "guesses = [0]" @@ -250,7 +265,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/01-Introduction to Python Statements.ipynb b/02-Python Statements/01-Introduction to Python Statements.ipynb index 4de998b19..98f975083 100644 --- a/02-Python Statements/01-Introduction to Python Statements.ipynb +++ b/02-Python Statements/01-Introduction to Python Statements.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": { @@ -117,7 +128,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/02-if, elif, and else Statements.ipynb b/02-Python Statements/02-if, elif, and else Statements.ipynb index 262bd6415..cad56d2a4 100644 --- a/02-Python Statements/02-if, elif, and else Statements.ipynb +++ b/02-Python Statements/02-if, elif, and else Statements.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -200,7 +211,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/03-for Loops.ipynb b/02-Python Statements/03-for Loops.ipynb index 61f54fec6..9a0e0c94b 100644 --- a/02-Python Statements/03-for Loops.ipynb +++ b/02-Python Statements/03-for Loops.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -32,7 +43,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# We'll learn how to automate this sort of list in the next lecture\n", @@ -383,7 +396,9 @@ { "cell_type": "code", "execution_count": 13, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "list2 = [(2,4),(6,8),(10,12)]" @@ -447,7 +462,9 @@ { "cell_type": "code", "execution_count": 16, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "d = {'k1':1,'k2':2,'k3':3}" @@ -619,7 +636,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/04-while Loops.ipynb b/02-Python Statements/04-while Loops.ipynb index f8ff55903..322681c92 100644 --- a/02-Python Statements/04-while Loops.ipynb +++ b/02-Python Statements/04-while Loops.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": { @@ -254,7 +265,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# DO NOT RUN THIS CODE!!!! \n", @@ -288,7 +301,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/05-Useful-Operators.ipynb b/02-Python Statements/05-Useful-Operators.ipynb index 632406a02..99084c6eb 100644 --- a/02-Python Statements/05-Useful-Operators.ipynb +++ b/02-Python Statements/05-Useful-Operators.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -319,7 +330,7 @@ "source": [ "## in operator\n", "\n", - "We've already seen the **in** keyword durng the for loop, but we can also use it to quickly check if an object is in a list" + "We've already seen the **in** keyword during the for loop, but we can also use it to quickly check if an object is in a list" ] }, { @@ -362,6 +373,55 @@ "'x' in [1,2,3]" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## not in\n", + "\n", + "We can combine **in** with a **not** operator, to check if some object or variable is not present in a list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'x' not in ['x','y','z']" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'x' not in [1,2,3]" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -579,7 +639,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.1" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/06-List Comprehensions.ipynb b/02-Python Statements/06-List Comprehensions.ipynb index 256271bad..db21a0116 100644 --- a/02-Python Statements/06-List Comprehensions.ipynb +++ b/02-Python Statements/06-List Comprehensions.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": { @@ -17,7 +28,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Grab every letter in string\n", @@ -58,7 +71,9 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Square numbers in range and turn into list\n", @@ -96,7 +111,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Check for even numbers in a range\n", @@ -209,7 +226,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/07-Statements Assessment Test.ipynb b/02-Python Statements/07-Statements Assessment Test.ipynb index b9e545460..26988bd8f 100644 --- a/02-Python Statements/07-Statements Assessment Test.ipynb +++ b/02-Python Statements/07-Statements Assessment Test.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": { @@ -21,7 +32,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "st = 'Print only the words that start with s in this sentence'" @@ -30,7 +43,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "#Code here" @@ -47,7 +62,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "#Code Here" @@ -64,7 +81,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "#Code in this cell\n", @@ -82,7 +101,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "st = 'Print every word in this sentence that has an even number of letters'" @@ -91,7 +112,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "#Code in this cell" @@ -108,7 +131,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "#Code in this cell" @@ -125,7 +150,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "st = 'Create a list of the first letters of every word in this string'" @@ -134,7 +161,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "#Code in this cell" @@ -164,7 +193,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/08-Statements Assessment Test - Solutions.ipynb b/02-Python Statements/08-Statements Assessment Test - Solutions.ipynb index bf90566b8..e760ca199 100644 --- a/02-Python Statements/08-Statements Assessment Test - Solutions.ipynb +++ b/02-Python Statements/08-Statements Assessment Test - Solutions.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": { @@ -20,7 +31,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "st = 'Print only the words that start with s in this sentence'" @@ -114,7 +127,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "st = 'Print every word in this sentence that has an even number of letters'" @@ -158,7 +173,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "for num in range(1,101):\n", @@ -183,7 +200,9 @@ { "cell_type": "code", "execution_count": 7, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "st = 'Create a list of the first letters of every word in this string'" @@ -233,7 +252,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/09-Guessing Game Challenge.ipynb b/02-Python Statements/09-Guessing Game Challenge.ipynb index 97f6fb7eb..4a32dfceb 100644 --- a/02-Python Statements/09-Guessing Game Challenge.ipynb +++ b/02-Python Statements/09-Guessing Game Challenge.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -36,7 +47,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [] }, @@ -50,7 +63,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [] }, @@ -66,7 +81,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [] }, @@ -80,7 +97,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "while True:\n", @@ -103,7 +122,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "while True:\n", @@ -146,7 +167,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/02-Python Statements/10-Guessing Game Challenge - Solution.ipynb b/02-Python Statements/10-Guessing Game Challenge - Solution.ipynb index 3df229e00..b8b852e15 100644 --- a/02-Python Statements/10-Guessing Game Challenge - Solution.ipynb +++ b/02-Python Statements/10-Guessing Game Challenge - Solution.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -34,7 +45,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "import random\n", @@ -90,7 +103,9 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "guesses = [0]" @@ -250,7 +265,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/01-Methods-checkpoint.ipynb b/03-Methods and Functions/.ipynb_checkpoints/01-Methods-checkpoint.ipynb index fde90b46d..c628266ea 100644 --- a/03-Methods and Functions/.ipynb_checkpoints/01-Methods-checkpoint.ipynb +++ b/03-Methods and Functions/.ipynb_checkpoints/01-Methods-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -22,7 +33,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Create a simple list\n", @@ -57,7 +70,9 @@ { "cell_type": "code", "execution_count": 2, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "lst.append(6)" @@ -170,7 +185,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/02-Functions-checkpoint.ipynb b/03-Methods and Functions/.ipynb_checkpoints/02-Functions-checkpoint.ipynb index 1e9243d60..1386e2d25 100644 --- a/03-Methods and Functions/.ipynb_checkpoints/02-Functions-checkpoint.ipynb +++ b/03-Methods and Functions/.ipynb_checkpoints/02-Functions-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -10,7 +21,7 @@ "\n", "This lecture will consist of explaining what a function is in Python and how to create one. Functions will be one of our main building blocks when we construct larger and larger amounts of code to solve problems.\n", "\n", - "**So what is a function?**\n", + "### What is a function?\n", "\n", "Formally, a function is a useful device that groups together a set of statements so they can be run more than once. They can also let us specify parameters that can serve as inputs to the functions.\n", "\n", @@ -23,7 +34,33 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## def Statements\n", + "### Why even use functions?\n", + "\n", + "Put simply, you should use functions when you plan on using a block of code multiple times. The function will allow you to call the same block of code without having to write it multiple times. This in turn will allow you to create more complex Python scripts. To really understand this though, we should actually write our own functions! " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Function Topics\n", + "* def keyword\n", + "* simple example of a function\n", + "* calling a function with ()\n", + "* accepting parameters\n", + "* print versus return\n", + "* adding in logic inside a function\n", + "* multiple returns inside a function\n", + "* adding in loops inside a function\n", + "* tuple unpacking\n", + "* interactions between functions" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### def keyword\n", "\n", "Let's see how to build out a function's syntax in Python. It has the following form:" ] @@ -31,12 +68,15 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def name_of_function(arg1,arg2):\n", " '''\n", - " This is where the function's Document String (docstring) goes\n", + " This is where the function's Document String (docstring) goes.\n", + " When you call help() on your function it will be printed out.\n", " '''\n", " # Do stuff here\n", " # Return desired result" @@ -46,13 +86,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "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/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": [ + "" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "say_hello" + ] + }, + { + "cell_type": "markdown", "metadata": {}, + "source": [ + "### Accepting parameters (arguments)\n", + "Let's write a function that greets people with their name." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def greeting(name):\n", - " print('Hello %s' %(name))" + " print(f'Hello {name}')" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -140,15 +218,19 @@ "metadata": {}, "source": [ "## Using return\n", + "So far we've only seen print() used, but if we actually want to save the resulting variable we need to use the **return** keyword.\n", + "\n", "Let's see some example that use a 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", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -364,7 +375,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Check\n", @@ -374,7 +387,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Check\n", @@ -707,7 +722,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/04-Function Practice Exercises - Solutions-checkpoint.ipynb b/03-Methods and Functions/.ipynb_checkpoints/04-Function Practice Exercises - Solutions-checkpoint.ipynb index 33ea1eea2..9d0e02b47 100644 --- a/03-Methods and Functions/.ipynb_checkpoints/04-Function Practice Exercises - Solutions-checkpoint.ipynb +++ b/03-Methods and Functions/.ipynb_checkpoints/04-Function Practice Exercises - Solutions-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -32,7 +43,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def lesser_of_two_evens(a,b):\n", @@ -96,7 +109,9 @@ { "cell_type": "code", "execution_count": 4, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def animal_crackers(text):\n", @@ -160,7 +175,9 @@ { "cell_type": "code", "execution_count": 7, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def makes_twenty(n1,n2):\n", @@ -251,7 +268,9 @@ { "cell_type": "code", "execution_count": 11, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def old_macdonald(name):\n", @@ -295,7 +314,9 @@ { "cell_type": "code", "execution_count": 13, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def master_yoda(text):\n", @@ -361,7 +382,9 @@ { "cell_type": "code", "execution_count": 16, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def almost_there(n):\n", @@ -475,7 +498,9 @@ { "cell_type": "code", "execution_count": 21, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def has_33(nums):\n", @@ -565,7 +590,9 @@ { "cell_type": "code", "execution_count": 25, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def paper_doll(text):\n", @@ -630,7 +657,9 @@ { "cell_type": "code", "execution_count": 28, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def blackjack(a,b,c):\n", @@ -720,7 +749,9 @@ { "cell_type": "code", "execution_count": 32, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def summer_69(arr):\n", @@ -826,7 +857,9 @@ { "cell_type": "code", "execution_count": 36, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def spy_game(nums):\n", @@ -916,7 +949,9 @@ { "cell_type": "code", "execution_count": 40, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def count_primes(num):\n", @@ -975,7 +1010,9 @@ { "cell_type": "code", "execution_count": 42, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def count_primes2(num):\n", @@ -1043,7 +1080,9 @@ { "cell_type": "code", "execution_count": 44, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def print_big(letter):\n", @@ -1098,7 +1137,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/05-Lambda-Expressions-Map-and-Filter-checkpoint.ipynb b/03-Methods and Functions/.ipynb_checkpoints/05-Lambda-Expressions-Map-and-Filter-checkpoint.ipynb index 4243e6898..ee54beb7f 100644 --- a/03-Methods and Functions/.ipynb_checkpoints/05-Lambda-Expressions-Map-and-Filter-checkpoint.ipynb +++ b/03-Methods and Functions/.ipynb_checkpoints/05-Lambda-Expressions-Map-and-Filter-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -561,7 +572,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.1" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/03-Methods and Functions/.ipynb_checkpoints/06-Nested Statements and Scope-checkpoint.ipynb b/03-Methods and Functions/.ipynb_checkpoints/06-Nested Statements and Scope-checkpoint.ipynb new file mode 100644 index 000000000..31fbcd969 --- /dev/null +++ b/03-Methods and Functions/.ipynb_checkpoints/06-Nested Statements and Scope-checkpoint.ipynb @@ -0,0 +1,372 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Nested Statements and Scope \n", + "\n", + "Now that we have gone over writing our own functions, it's important to understand how Python deals with the variable names you assign. When you create a variable name in Python the name is stored in a *name-space*. Variable names also have a *scope*, the scope determines the visibility of that variable name to other parts of your code.\n", + "\n", + "Let's start with a quick thought experiment; imagine the following code:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "x = 25\n", + "\n", + "def printer():\n", + " x = 50\n", + " return x\n", + "\n", + "# print(x)\n", + "# print(printer())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "What do you imagine the output of printer() is? 25 or 50? What is the output of print x? 25 or 50?" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "25\n" + ] + } + ], + "source": [ + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "50\n" + ] + } + ], + "source": [ + "print(printer())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Interesting! But how does Python know which **x** you're referring to in your code? This is where the idea of scope comes in. Python has a set of rules it follows to decide what variables (such as **x** in this case) you are referencing in your code. Lets break down the rules:" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "This idea of scope in your code is very important to understand in order to properly assign and call variable names. \n", + "\n", + "In simple terms, the idea of scope can be described by 3 general rules:\n", + "\n", + "1. Name assignments will create or change local names by default.\n", + "2. Name references search (at most) four scopes, these are:\n", + " * local\n", + " * enclosing functions\n", + " * global\n", + " * built-in\n", + "3. Names declared in global and nonlocal statements map assigned names to enclosing module and function scopes.\n", + "\n", + "\n", + "The statement in #2 above can be defined by the LEGB rule.\n", + "\n", + "**LEGB Rule:**\n", + "\n", + "L: Local — Names assigned in any way within a function (def or lambda), and not declared global in that function.\n", + "\n", + "E: Enclosing function locals — Names in the local scope of any and all enclosing functions (def or lambda), from inner to outer.\n", + "\n", + "G: Global (module) — Names assigned at the top-level of a module file, or declared global in a def within the file.\n", + "\n", + "B: Built-in (Python) — Names preassigned in the built-in names module : open, range, SyntaxError,..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Quick examples of LEGB\n", + "\n", + "### Local" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# x is local here:\n", + "f = lambda x:x**2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Enclosing function locals\n", + "This occurs when we have a function inside a function (nested functions)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello Sammy\n" + ] + } + ], + "source": [ + "name = 'This is a global name'\n", + "\n", + "def greet():\n", + " # Enclosing function\n", + " name = 'Sammy'\n", + " \n", + " def hello():\n", + " print('Hello '+name)\n", + " \n", + " hello()\n", + "\n", + "greet()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Note how Sammy was used, because the hello() function was enclosed inside of the greet function!" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Global\n", + "Luckily in Jupyter a quick way to test for global variables is to see if another cell recognizes the variable!" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is a global name\n" + ] + } + ], + "source": [ + "print(name)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Built-in\n", + "These are the built-in function names in Python (don't overwrite these!)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Local Variables\n", + "When you declare variables inside a function definition, they are not related in any way to other variables with the same names used outside the function - i.e. variable names are local to the function. This is called the scope of the variable. All variables have the scope of the block they are declared in starting from the point of definition of the name.\n", + "\n", + "Example:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x is 50\n", + "Changed local x to 2\n", + "x is still 50\n" + ] + } + ], + "source": [ + "x = 50\n", + "\n", + "def func(x):\n", + " print('x is', x)\n", + " x = 2\n", + " print('Changed local x to', x)\n", + "\n", + "func(x)\n", + "print('x is still', x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The first time that we print the value of the name **x** with the first line in the function’s body, Python uses the value of the parameter declared in the main block, above the function definition.\n", + "\n", + "Next, we assign the value 2 to **x**. The name **x** is local to our function. So, when we change the value of **x** in the function, the **x** defined in the main block remains unaffected.\n", + "\n", + "With the last print statement, we display the value of **x** as defined in the main block, thereby confirming that it is actually unaffected by the local assignment within the previously called function.\n", + "\n", + "## The 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", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -264,7 +275,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/08-Functions and Methods Homework-checkpoint.ipynb b/03-Methods and Functions/.ipynb_checkpoints/08-Functions and Methods Homework-checkpoint.ipynb new file mode 100644 index 000000000..70763a3a5 --- /dev/null +++ b/03-Methods and Functions/.ipynb_checkpoints/08-Functions and Methods Homework-checkpoint.ipynb @@ -0,0 +1,417 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Functions and Methods Homework \n", + "\n", + "Complete the following questions:\n", + "____\n", + "**Write a function that computes the volume of a sphere given its radius.**\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", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Functions and Methods Homework Solutions\n", + "____\n", + "**Write a function that computes the volume of a sphere given its radius.**" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def vol(rad):\n", + " return (4/3)*(3.14)*(rad**3)" + ] + }, + { + "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", + " #Check if num is between low and high (including low and high)\n", + " if num in range(low,high+1):\n", + " print('{} is in the range between {} and {}'.format(num,low,high))\n", + " else:\n", + " print('The number is outside the range.')" + ] + }, + { + "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", + " return num in range(low,high+1)" + ] + }, + { + "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", + "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", + " d={\"upper\":0, \"lower\":0}\n", + " for c in s:\n", + " if c.isupper():\n", + " d[\"upper\"]+=1\n", + " elif c.islower():\n", + " d[\"lower\"]+=1\n", + " else:\n", + " pass\n", + " print(\"Original String : \", s)\n", + " print(\"No. of Upper case characters : \", d[\"upper\"])\n", + " print(\"No. of Lower case Characters : \", d[\"lower\"])" + ] + }, + { + "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", + " # Also possible to use list(set())\n", + " x = []\n", + " for a in lst:\n", + " if a not in x:\n", + " x.append(a)\n", + " return x" + ] + }, + { + "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", + " total = 1\n", + " for x in numbers:\n", + " total *= x\n", + " return total" + ] + }, + { + "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", + " \n", + " s = s.replace(' ','') # This replaces all spaces ' ' with no space ''. (Fixes issues with strings that have spaces)\n", + " return s == s[::-1] # Check through slicing" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "palindrome('nurses run')" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "palindrome('abcba')" + ] + }, + { + "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": 7, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import string\n", + "\n", + "def ispangram(str1, alphabet=string.ascii_lowercase): \n", + " # Create a set of the alphabet\n", + " alphaset = set(alphabet) \n", + " \n", + " # Remove spaces from str1\n", + " str1 = str1.replace(\" \",'')\n", + " \n", + " # Lowercase all strings in the passed in string\n", + " # Recall we assume no punctuation \n", + " str1 = str1.lower()\n", + " \n", + " # Grab all unique letters in the string as a set\n", + " str1 = set(str1)\n", + " \n", + " # Now check that the alpahbet set is same as string set\n", + " return str1 == alphaset" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ispangram(\"The quick brown fox jumps over the lazy dog\")" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'abcdefghijklmnopqrstuvwxyz'" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "string.ascii_lowercase" + ] + } + ], + "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/01-Methods.ipynb b/03-Methods and Functions/01-Methods.ipynb index fde90b46d..c628266ea 100644 --- a/03-Methods and Functions/01-Methods.ipynb +++ b/03-Methods and Functions/01-Methods.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -22,7 +33,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Create a simple list\n", @@ -57,7 +70,9 @@ { "cell_type": "code", "execution_count": 2, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "lst.append(6)" @@ -170,7 +185,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/02-Functions.ipynb b/03-Methods and Functions/02-Functions.ipynb index 1e9243d60..1386e2d25 100644 --- a/03-Methods and Functions/02-Functions.ipynb +++ b/03-Methods and Functions/02-Functions.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -10,7 +21,7 @@ "\n", "This lecture will consist of explaining what a function is in Python and how to create one. Functions will be one of our main building blocks when we construct larger and larger amounts of code to solve problems.\n", "\n", - "**So what is a function?**\n", + "### What is a function?\n", "\n", "Formally, a function is a useful device that groups together a set of statements so they can be run more than once. They can also let us specify parameters that can serve as inputs to the functions.\n", "\n", @@ -23,7 +34,33 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## def Statements\n", + "### Why even use functions?\n", + "\n", + "Put simply, you should use functions when you plan on using a block of code multiple times. The function will allow you to call the same block of code without having to write it multiple times. This in turn will allow you to create more complex Python scripts. To really understand this though, we should actually write our own functions! " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Function Topics\n", + "* def keyword\n", + "* simple example of a function\n", + "* calling a function with ()\n", + "* accepting parameters\n", + "* print versus return\n", + "* adding in logic inside a function\n", + "* multiple returns inside a function\n", + "* adding in loops inside a function\n", + "* tuple unpacking\n", + "* interactions between functions" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### def keyword\n", "\n", "Let's see how to build out a function's syntax in Python. It has the following form:" ] @@ -31,12 +68,15 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def name_of_function(arg1,arg2):\n", " '''\n", - " This is where the function's Document String (docstring) goes\n", + " This is where the function's Document String (docstring) goes.\n", + " When you call help() on your function it will be printed out.\n", " '''\n", " # Do stuff here\n", " # Return desired result" @@ -46,13 +86,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "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/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": [ + "" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "say_hello" + ] + }, + { + "cell_type": "markdown", "metadata": {}, + "source": [ + "### Accepting parameters (arguments)\n", + "Let's write a function that greets people with their name." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def greeting(name):\n", - " print('Hello %s' %(name))" + " print(f'Hello {name}')" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -140,15 +218,19 @@ "metadata": {}, "source": [ "## Using return\n", + "So far we've only seen print() used, but if we actually want to save the resulting variable we need to use the **return** keyword.\n", + "\n", "Let's see some example that use a 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", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -364,7 +375,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Check\n", @@ -374,7 +387,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# Check\n", @@ -707,7 +722,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/04-Function Practice Exercises - Solutions.ipynb b/03-Methods and Functions/04-Function Practice Exercises - Solutions.ipynb index 33ea1eea2..9d0e02b47 100644 --- a/03-Methods and Functions/04-Function Practice Exercises - Solutions.ipynb +++ b/03-Methods and Functions/04-Function Practice Exercises - Solutions.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -32,7 +43,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def lesser_of_two_evens(a,b):\n", @@ -96,7 +109,9 @@ { "cell_type": "code", "execution_count": 4, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def animal_crackers(text):\n", @@ -160,7 +175,9 @@ { "cell_type": "code", "execution_count": 7, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def makes_twenty(n1,n2):\n", @@ -251,7 +268,9 @@ { "cell_type": "code", "execution_count": 11, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def old_macdonald(name):\n", @@ -295,7 +314,9 @@ { "cell_type": "code", "execution_count": 13, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def master_yoda(text):\n", @@ -361,7 +382,9 @@ { "cell_type": "code", "execution_count": 16, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def almost_there(n):\n", @@ -475,7 +498,9 @@ { "cell_type": "code", "execution_count": 21, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def has_33(nums):\n", @@ -565,7 +590,9 @@ { "cell_type": "code", "execution_count": 25, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def paper_doll(text):\n", @@ -630,7 +657,9 @@ { "cell_type": "code", "execution_count": 28, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def blackjack(a,b,c):\n", @@ -720,7 +749,9 @@ { "cell_type": "code", "execution_count": 32, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def summer_69(arr):\n", @@ -826,7 +857,9 @@ { "cell_type": "code", "execution_count": 36, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def spy_game(nums):\n", @@ -916,7 +949,9 @@ { "cell_type": "code", "execution_count": 40, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def count_primes(num):\n", @@ -975,7 +1010,9 @@ { "cell_type": "code", "execution_count": 42, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def count_primes2(num):\n", @@ -1043,7 +1080,9 @@ { "cell_type": "code", "execution_count": 44, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def print_big(letter):\n", @@ -1098,7 +1137,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/05-Lambda-Expressions-Map-and-Filter.ipynb b/03-Methods and Functions/05-Lambda-Expressions-Map-and-Filter.ipynb index 4243e6898..ee54beb7f 100644 --- a/03-Methods and Functions/05-Lambda-Expressions-Map-and-Filter.ipynb +++ b/03-Methods and Functions/05-Lambda-Expressions-Map-and-Filter.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -561,7 +572,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.1" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/03-Methods and Functions/06-Nested Statements and Scope.ipynb b/03-Methods and Functions/06-Nested Statements and Scope.ipynb index c3871db28..31fbcd969 100644 --- a/03-Methods and Functions/06-Nested Statements and Scope.ipynb +++ b/03-Methods and Functions/06-Nested Statements and Scope.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -14,7 +25,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "x = 25\n", @@ -119,7 +132,9 @@ { "cell_type": "code", "execution_count": 4, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# x is local here:\n", @@ -349,7 +364,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/07-args and kwargs.ipynb b/03-Methods and Functions/07-args and kwargs.ipynb index 47fa02ad6..3624b3e8b 100644 --- a/03-Methods and Functions/07-args and kwargs.ipynb +++ b/03-Methods and Functions/07-args and kwargs.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -264,7 +275,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/08-Functions and Methods Homework.ipynb b/03-Methods and Functions/08-Functions and Methods Homework.ipynb index 95d610956..70763a3a5 100644 --- a/03-Methods and Functions/08-Functions and Methods Homework.ipynb +++ b/03-Methods and Functions/08-Functions and Methods Homework.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -15,7 +26,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def vol(rad):\n", @@ -54,7 +67,9 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def ran_check(num,low,high):\n", @@ -89,7 +104,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def ran_bool(num,low,high):\n", @@ -136,7 +153,9 @@ { "cell_type": "code", "execution_count": 7, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def up_low(s):\n", @@ -177,7 +196,9 @@ { "cell_type": "code", "execution_count": 9, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def unique_list(lst):\n", @@ -218,7 +239,9 @@ { "cell_type": "code", "execution_count": 11, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def multiply(numbers): \n", @@ -250,15 +273,17 @@ "metadata": {}, "source": [ "____\n", - "**Write a Python function that checks whether a passed in string is palindrome or not.**\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 or nurses run." + "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": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def palindrome(s):\n", @@ -292,18 +317,24 @@ "____\n", "#### Hard:\n", "\n", - "**Write a Python function to check whether a string is pangram or not.**\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: Look at the string module" + "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": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "import string\n", @@ -378,7 +409,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/09-Functions and Methods Homework - Solutions.ipynb b/03-Methods and Functions/09-Functions and Methods Homework - Solutions.ipynb index 68303e6bd..6fd10540b 100644 --- a/03-Methods and Functions/09-Functions and Methods Homework - Solutions.ipynb +++ b/03-Methods and Functions/09-Functions and Methods Homework - Solutions.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -12,7 +23,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def vol(rad):\n", @@ -51,7 +64,9 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def ran_check(num,low,high):\n", @@ -90,7 +105,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def ran_bool(num,low,high):\n", @@ -135,7 +152,9 @@ { "cell_type": "code", "execution_count": 7, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def up_low(s):\n", @@ -186,7 +205,9 @@ { "cell_type": "code", "execution_count": 9, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def unique_list(lst):\n", @@ -232,7 +253,9 @@ { "cell_type": "code", "execution_count": 11, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def multiply(numbers):\n", @@ -267,15 +290,17 @@ "metadata": {}, "source": [ "____\n", - "**Write a Python function that checks whether a passed string is palindrome or not.**\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 or nurses run." + "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": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def palindrome(s):\n", @@ -329,32 +354,51 @@ "metadata": {}, "source": [ "____\n", - "**Hard**:\n", + "#### Hard:\n", "\n", - "Write a Python function to check whether a string is pangram or not.\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: Look at the string module" + "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": 16, - "metadata": {}, + "execution_count": 7, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "import string\n", "\n", - "def ispangram(str1, alphabet=string.ascii_lowercase): \n", + "def ispangram(str1, alphabet=string.ascii_lowercase): \n", + " # Create a set of the alphabet\n", " alphaset = set(alphabet) \n", - " return alphaset <= set(str1.lower()) " + " \n", + " # Remove spaces from str1\n", + " str1 = str1.replace(\" \",'')\n", + " \n", + " # Lowercase all strings in the passed in string\n", + " # Recall we assume no punctuation \n", + " str1 = str1.lower()\n", + " \n", + " # Grab all unique letters in the string as a set\n", + " str1 = set(str1)\n", + " \n", + " # Now check that the alpahbet set is same as string set\n", + " return str1 == alphaset" ] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -363,7 +407,7 @@ "True" ] }, - "execution_count": 17, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -409,7 +453,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/04-Milestone Project - 1/.ipynb_checkpoints/00-Warm-Up-Project-Exercises-checkpoint.ipynb b/04-Milestone Project - 1/.ipynb_checkpoints/00-Warm-Up-Project-Exercises-checkpoint.ipynb new file mode 100644 index 000000000..900f5524a --- /dev/null +++ b/04-Milestone Project - 1/.ipynb_checkpoints/00-Warm-Up-Project-Exercises-checkpoint.ipynb @@ -0,0 +1,1028 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Warm Up Project Exercises\n", + "\n", + "It is time to get you to put together all your skills to start building usable projects! Before you jump into our full milestone project, we will go through some warm-up component exercises, to get you comfortable with a few key ideas we use in the milestone project and larger projects in general, specifically:\n", + "\n", + "* Getting User Input\n", + "* Creating Functions that edit variables based on user input\n", + "* Generating output\n", + "* Joining User Inputs and Logic Flow" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Function to Display Information\n", + "\n", + "**Creating a function that displays a list for the user**" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def display_list(mylist):\n", + " print(mylist)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n" + ] + } + ], + "source": [ + "mylist = [0,1,2,3,4,5,6,7,8,9,10]\n", + "display_list(mylist)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Accepting User Input\n", + "\n", + "**Creating function that takes in an input from user and returns the result in the correct data type. Be careful when using the input() function, running that cell twice without providing an input value will cause python to get hung up waiting for the initial value on the first run. You will notice an In[*\\] next to the cell if it gets stuck, in which case, simply restart the kernel and re-run any necessary cells.**" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a value: 2\n" + ] + }, + { + "data": { + "text/plain": [ + "'2'" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "input('Please enter a value: ')" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number: 2\n" + ] + } + ], + "source": [ + "result = input(\"Please enter a number: \")" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'2'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(result)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "int(result)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number: 2\n" + ] + } + ], + "source": [ + "result = int(input(\"Please enter a number: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(result)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number: two\n" + ] + }, + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: 'two'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Example of an error!\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Please enter a number: \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'two'" + ] + } + ], + "source": [ + "# Example of an error!\n", + "result = int(input(\"Please enter a number: \"))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Creating a function to hold this logic: **" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def user_choice():\n", + " '''\n", + " User inputs a number (0-10) and we return this in integer form.\n", + " No parameter is passed when calling this function.\n", + " '''\n", + " choice = input(\"Please input a number (0-10)\")\n", + " \n", + " return int(choice)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please input a number (0-10)2\n" + ] + }, + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "user_choice()" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please input a number (0-10)2\n" + ] + } + ], + "source": [ + "result = user_choice()" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(result)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Validating User Input\n", + "\n", + "** Check that input is valid before attempting to convert.** \n", + "\n", + "We'll use a simple method here.\n", + "\n", + "As you get more advanced, you can start looking at other ways of doing this (these methods will make more sense later on in the course, so don't worry about them for now).\n", + "\n", + "* [Various Posts on This](https://www.google.com/search?q=python+check+if+input+is+number)\n", + "* [StackOverflow Post 1](https://stackoverflow.com/questions/5424716/how-to-check-if-string-input-is-a-number)\n", + "* [StackOverflow Post 2](https://stackoverflow.com/questions/1265665/how-can-i-check-if-a-string-represents-an-int-without-using-try-except)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "some_input = '10'" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Lot's of .is methods availble on string\n", + "some_input.isdigit()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Edit the function to confirm against an acceptable value or type **" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def user_choice():\n", + " \n", + " # This original choice value can be anything that isn't an integer\n", + " choice = 'wrong'\n", + " \n", + " # While the choice is not a digit, keep asking for input.\n", + " while choice.isdigit() == False:\n", + " \n", + " # we shouldn't convert here, otherwise we get an error on a wrong input\n", + " choice = input(\"Choose a number: \")\n", + " \n", + " # We can convert once the while loop above has confirmed we have a digit.\n", + " return int(choice)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Choose a number: hello\n", + "Choose a number: two\n", + "Choose a number: 2\n" + ] + }, + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "user_choice()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Let's try adding an error message within the while loop!**" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def user_choice():\n", + " \n", + " # This original choice value can be anything that isn't an integer\n", + " choice = 'wrong'\n", + " \n", + " # While the choice is not a digit, keep asking for input.\n", + " while choice.isdigit() == False:\n", + " \n", + " # we shouldn't convert here, otherwise we get an error on a wrong input\n", + " choice = input(\"Choose a number: \")\n", + " \n", + " # Error Message Check\n", + " if choice.isdigit() == False:\n", + " print(\"Sorry, but you did not enter an integer. Please try again.\")\n", + " \n", + " # We can convert once the while loop above has confirmed we have a digit.\n", + " return int(choice)" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Choose a number: two\n", + "Sorry, but you did not enter an integer. Please try again.\n", + "Choose a number: 2\n" + ] + }, + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "user_choice()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Now let's explore how to \"clear\" the output, that way we don't see the history of the \"Choose a number\" statements.**\n", + "\n", + "**NOTE: Jupyter Notebook users will use the IPython method shown here. Other IDE users (PyCharm, VS, etc..) will use **" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "from IPython.display import clear_output\n", + "clear_output()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def user_choice():\n", + " \n", + " # This original choice value can be anything that isn't an integer\n", + " choice = 'wrong'\n", + " \n", + " # While the choice is not a digit, keep asking for input.\n", + " while choice.isdigit() == False:\n", + " \n", + " # we shouldn't convert here, otherwise we get an error on a wrong input\n", + " choice = input(\"Choose a number: \")\n", + " \n", + " if choice.isdigit() == False:\n", + " # THIS CLEARS THE CURRENT OUTPUT BELOW THE CELL\n", + " clear_output()\n", + " \n", + " print(\"Sorry, but you did not enter an integer. Please try again.\")\n", + " \n", + " \n", + " # Optionally you can clear everything after running the function\n", + " # clear_output()\n", + " \n", + " # We can convert once the while loop above has confirmed we have a digit.\n", + " return int(choice)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Choose a number: 2\n" + ] + }, + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "user_choice()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Checking Against Multiple Possible Values**" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "result = 'wrong value'\n", + "acceptable_values = ['0','1','2']" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result in acceptable_values" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result not in acceptable_values" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "from IPython.display import clear_output\n", + "clear_output()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def user_choice():\n", + " \n", + " # This original choice value can be anything that isn't an integer\n", + " choice = 'wrong'\n", + " \n", + " # While the choice is not a digit, keep asking for input.\n", + " while choice not in ['0','1','2']:\n", + " \n", + " # we shouldn't convert here, otherwise we get an error on a wrong input\n", + " choice = input(\"Choose one of these numbers (0,1,2): \")\n", + " \n", + " if choice not in ['0','1','2']:\n", + " # THIS CLEARS THE CURRENT OUTPUT BELOW THE CELL\n", + " clear_output()\n", + " \n", + " print(\"Sorry, but you did not choose a value in the correct range (0,1,2)\")\n", + " \n", + " \n", + " # Optionally you can clear everything after running the function\n", + " # clear_output()\n", + " \n", + " # We can convert once the while loop above has confirmed we have a digit.\n", + " return int(choice)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Choose one of these numbers (0,1,2): 1\n" + ] + }, + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "user_choice()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### More Flexible Example" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def user_choice():\n", + " \n", + " choice ='WRONG'\n", + " within_range = False\n", + " \n", + " while choice.isdigit() == False or within_range == False:\n", + " \n", + " \n", + " \n", + " choice = input(\"Please enter a number (0-10): \")\n", + " \n", + " if choice.isdigit() == False:\n", + " print(\"Sorry that is not a digit!\")\n", + " \n", + " if choice.isdigit() == True:\n", + " if int(choice) in range(0,10):\n", + " within_range = True\n", + " else:\n", + " within_range = False\n", + " \n", + " \n", + " return int(choice)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number (0-10): 12\n", + "Please enter a number (0-10): 2\n" + ] + }, + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "user_choice()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "-----\n", + "## Simple User Interaction\n", + "\n", + "**Finally let's combine all of these ideas to create a small game where a user can choose a \"position\" in an existing list and replace it with a value of their choice.**" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "game_list = [0,1,2]" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def display_game(game_list):\n", + " print(\"Here is the current list\")\n", + " print(game_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Here is the current list\n", + "['hi', 'no', 2]\n" + ] + } + ], + "source": [ + "display_game(game_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def position_choice():\n", + " \n", + " # This original choice value can be anything that isn't an integer\n", + " choice = 'wrong'\n", + " \n", + " # While the choice is not a digit, keep asking for input.\n", + " while choice not in ['0','1','2']:\n", + " \n", + " # we shouldn't convert here, otherwise we get an error on a wrong input\n", + " choice = input(\"Pick a position to replace (0,1,2): \")\n", + " \n", + " if choice not in ['0','1','2']:\n", + " # THIS CLEARS THE CURRENT OUTPUT BELOW THE CELL\n", + " clear_output()\n", + " \n", + " print(\"Sorry, but you did not choose a valid position (0,1,2)\")\n", + " \n", + " \n", + " # Optionally you can clear everything after running the function\n", + " # clear_output()\n", + " \n", + " # We can convert once the while loop above has confirmed we have a digit.\n", + " return int(choice)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def replacement_choice(game_list,position):\n", + " \n", + " user_placement = input(\"Type a string to place at the position\")\n", + " \n", + " game_list[position] = user_placement\n", + " \n", + " return game_list" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def gameon_choice():\n", + " \n", + " # This original choice value can be anything that isn't a Y or N\n", + " choice = 'wrong'\n", + " \n", + " # While the choice is not a digit, keep asking for input.\n", + " while choice not in ['Y','N']:\n", + " \n", + " # we shouldn't convert here, otherwise we get an error on a wrong input\n", + " choice = input(\"Would you like to keep playing? Y or N \")\n", + "\n", + " \n", + " if choice not in ['Y','N']:\n", + " # THIS CLEARS THE CURRENT OUTPUT BELOW THE CELL\n", + " clear_output()\n", + " \n", + " print(\"Sorry, I didn't understand. Please make sure to choose Y or N.\")\n", + " \n", + " \n", + " # Optionally you can clear everything after running the function\n", + " # clear_output()\n", + " \n", + " if choice == \"Y\":\n", + " # Game is still on\n", + " return True\n", + " else:\n", + " # Game is over\n", + " return False" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Game Logic All Together**" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Here is the current list\n", + "['34', 1, 'new value']\n", + "Would you like to keep playing? Y or N N\n" + ] + } + ], + "source": [ + "# Variable to keep game playing\n", + "game_on = True\n", + "\n", + "# First Game List\n", + "game_list = [0,1,2]\n", + "\n", + "\n", + "\n", + "while game_on:\n", + " \n", + " # Clear any historical output and show the game list\n", + " clear_output()\n", + " display_game(game_list)\n", + " \n", + " # Have player choose position\n", + " position = position_choice()\n", + " \n", + " # Rewrite that position and update game_list\n", + " game_list = replacement_choice(game_list,position)\n", + " \n", + " # Clear Screen and show the updated game list\n", + " clear_output()\n", + " display_game(game_list)\n", + " \n", + " # Ask if you want to keep playing\n", + " game_on = gameon_choice()\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Great work! You now have an understanding of bringing functions and loop logics together to build a simple game. This will be expanded upon in the Milestone project!**" + ] + } + ], + "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": 2 +} diff --git a/04-Milestone Project - 1/.ipynb_checkpoints/01-Milestone Project 1 - Assignment-checkpoint.ipynb b/04-Milestone Project - 1/.ipynb_checkpoints/01-Milestone Project 1 - Assignment-checkpoint.ipynb index b9b4a7634..2b887b9dd 100644 --- a/04-Milestone Project - 1/.ipynb_checkpoints/01-Milestone Project 1 - Assignment-checkpoint.ipynb +++ b/04-Milestone Project - 1/.ipynb_checkpoints/01-Milestone Project 1 - Assignment-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -54,7 +65,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/04-Milestone Project - 1/.ipynb_checkpoints/02-Milestone Project 1 - Walkthrough Steps Workbook-checkpoint.ipynb b/04-Milestone Project - 1/.ipynb_checkpoints/02-Milestone Project 1 - Walkthrough Steps Workbook-checkpoint.ipynb index 81a5a453f..5ecb08f95 100644 --- a/04-Milestone Project - 1/.ipynb_checkpoints/02-Milestone Project 1 - Walkthrough Steps Workbook-checkpoint.ipynb +++ b/04-Milestone Project - 1/.ipynb_checkpoints/02-Milestone Project 1 - Walkthrough Steps Workbook-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -43,7 +54,7 @@ }, { "cell_type": "code", - "execution_count": 87, + "execution_count": null, "metadata": { "collapsed": true }, @@ -56,6 +67,25 @@ " pass" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**TEST Step 1:** run your function on a test version of the board list, and make adjustments as necessary" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "test_board = ['#','X','O','X','O','X','O','X','O','X']\n", + "display_board(test_board)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -65,7 +95,7 @@ }, { "cell_type": "code", - "execution_count": 88, + "execution_count": null, "metadata": { "collapsed": true }, @@ -76,6 +106,24 @@ " pass" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**TEST Step 2:** run the function to make sure it returns the desired output" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "player_input()" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -85,7 +133,7 @@ }, { "cell_type": "code", - "execution_count": 89, + "execution_count": null, "metadata": { "collapsed": true }, @@ -96,6 +144,25 @@ " pass" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**TEST Step 3:** run the place marker function using test parameters and display the modified board" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "place_marker(test_board,'$',8)\n", + "display_board(test_board)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -105,7 +172,7 @@ }, { "cell_type": "code", - "execution_count": 90, + "execution_count": null, "metadata": { "collapsed": true }, @@ -116,6 +183,24 @@ " pass" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**TEST Step 4:** run the win_check function against our test_board - it should return True" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "win_check(test_board,'X')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -125,13 +210,14 @@ }, { "cell_type": "code", - "execution_count": 91, + "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import random\n", + "\n", "def choose_first():\n", " pass" ] @@ -145,7 +231,7 @@ }, { "cell_type": "code", - "execution_count": 92, + "execution_count": null, "metadata": { "collapsed": true }, @@ -165,7 +251,7 @@ }, { "cell_type": "code", - "execution_count": 93, + "execution_count": null, "metadata": { "collapsed": true }, @@ -185,7 +271,7 @@ }, { "cell_type": "code", - "execution_count": 94, + "execution_count": null, "metadata": { "collapsed": true }, @@ -205,7 +291,7 @@ }, { "cell_type": "code", - "execution_count": 95, + "execution_count": null, "metadata": { "collapsed": true }, @@ -227,17 +313,11 @@ }, { "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Welcome to Tic Tac Toe!\n" - ] - } - ], + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], "source": [ "print('Welcome to Tic Tac Toe!')\n", "\n", @@ -283,7 +363,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/04-Milestone Project - 1/.ipynb_checkpoints/03-Milestone Project 1 - Complete Walkthrough Solution-checkpoint.ipynb b/04-Milestone Project - 1/.ipynb_checkpoints/03-Milestone Project 1 - Complete Walkthrough Solution-checkpoint.ipynb index 8e84722d0..9f71957f9 100644 --- a/04-Milestone Project - 1/.ipynb_checkpoints/03-Milestone Project 1 - Complete Walkthrough Solution-checkpoint.ipynb +++ b/04-Milestone Project - 1/.ipynb_checkpoints/03-Milestone Project 1 - Complete Walkthrough Solution-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -52,9 +63,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -116,9 +125,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -171,9 +178,7 @@ { "cell_type": "code", "execution_count": 6, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -235,9 +240,7 @@ { "cell_type": "code", "execution_count": 8, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -375,27 +378,25 @@ }, { "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, + "execution_count": 14, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " | |\n", - " | O | \n", + " | O | O\n", " | |\n", "-----------\n", " | |\n", - " | O | X\n", + " | | \n", " | |\n", "-----------\n", " | |\n", - " X | O | X\n", + " X | X | X\n", " | |\n", - "Player 2 has won!\n", + "Congratulations! You have won the game!\n", "Do you want to play again? Enter Yes or No: No\n" ] } @@ -473,7 +474,7 @@ "metadata": { "anaconda-cloud": {}, "kernelspec": { - "display_name": "Python [default]", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -487,7 +488,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.3" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/04-Milestone Project - 1/.ipynb_checkpoints/04-OPTIONAL -Milestone Project 1 - Advanced Solution-checkpoint.ipynb b/04-Milestone Project - 1/.ipynb_checkpoints/04-OPTIONAL -Milestone Project 1 - Advanced Solution-checkpoint.ipynb deleted file mode 100644 index 0829bc577..000000000 --- a/04-Milestone Project - 1/.ipynb_checkpoints/04-OPTIONAL -Milestone Project 1 - Advanced Solution-checkpoint.ipynb +++ /dev/null @@ -1,264 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Tic Tac Toe - Advanced Solution\n", - "\n", - "This solution follows the same basic format as the Complete Walkthrough Solution, but takes advantage of some of the more advanced statements we have learned. Feel free to download the notebook to understand how it works!" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Specifically for the iPython Notebook environment for clearing output\n", - "from IPython.display import clear_output\n", - "import random\n", - "\n", - "# Global variables\n", - "theBoard = [' '] * 10 # a list of empty spaces\n", - "available = [str(num) for num in range(0,10)] # a List Comprehension\n", - "players = [0,'X','O'] # note that players[1] == 'X' and players[-1] == 'O'" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Available TIC-TAC-TOE\n", - " moves\n", - "\n", - " 7|8|9 | | \n", - " ----- -----\n", - " 4|5|6 | | \n", - " ----- -----\n", - " 1|2|3 | | \n", - "\n" - ] - } - ], - "source": [ - "def display_board(a,b):\n", - " print('Available TIC-TAC-TOE\\n'+\n", - " ' moves\\n\\n '+\n", - " a[7]+'|'+a[8]+'|'+a[9]+' '+b[7]+'|'+b[8]+'|'+b[9]+'\\n '+\n", - " '----- -----\\n '+\n", - " a[4]+'|'+a[5]+'|'+a[6]+' '+b[4]+'|'+b[5]+'|'+b[6]+'\\n '+\n", - " '----- -----\\n '+\n", - " a[1]+'|'+a[2]+'|'+a[3]+' '+b[1]+'|'+b[2]+'|'+b[3]+'\\n')\n", - "display_board(available,theBoard)" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Available TIC-TAC-TOE\n", - " moves\n", - "\n", - " 7|8|9 | | \n", - " ----- -----\n", - " 4|5|6 | | \n", - " ----- -----\n", - " 1|2|3 | | \n", - "\n" - ] - } - ], - "source": [ - "def display_board(a,b):\n", - " print(f'Available TIC-TAC-TOE\\n moves\\n\\n {a[7]}|{a[8]}|{a[9]} {b[7]}|{b[8]}|{b[9]}\\n ----- -----\\n {a[4]}|{a[5]}|{a[6]} {b[4]}|{b[5]}|{b[6]}\\n ----- -----\\n {a[1]}|{a[2]}|{a[3]} {b[1]}|{b[2]}|{b[3]}\\n')\n", - "display_board(available,theBoard)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def place_marker(avail,board,marker,position):\n", - " board[position] = marker\n", - " avail[position] = ' '" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def win_check(board,mark):\n", - "\n", - " return ((board[7] == board[8] == board[9] == mark) or # across the top\n", - " (board[4] == board[5] == board[6] == mark) or # across the middle\n", - " (board[1] == board[2] == board[3] == mark) or # across the bottom\n", - " (board[7] == board[4] == board[1] == mark) or # down the middle\n", - " (board[8] == board[5] == board[2] == mark) or # down the middle\n", - " (board[9] == board[6] == board[3] == mark) or # down the right side\n", - " (board[7] == board[5] == board[3] == mark) or # diagonal\n", - " (board[9] == board[5] == board[1] == mark)) # diagonal" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def random_player():\n", - " return random.choice((-1, 1))\n", - " \n", - "def space_check(board,position):\n", - " return board[position] == ' '\n", - "\n", - "def full_board_check(board):\n", - " return ' ' not in board[1:]" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def player_choice(board,player):\n", - " position = 0\n", - " \n", - " while position not in [1,2,3,4,5,6,7,8,9] or not space_check(board, position):\n", - " try:\n", - " position = int(input('Player %s, choose your next position: (1-9) '%(player)))\n", - " except:\n", - " print(\"I'm sorry, please try again.\")\n", - " \n", - " return position" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def replay():\n", - " \n", - " return input('Do you want to play again? Enter Yes or No: ').lower().startswith('y')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Welcome to Tic Tac Toe!\n", - "For this round, Player X will go first!\n" - ] - } - ], - "source": [ - "while True:\n", - " clear_output()\n", - " print('Welcome to Tic Tac Toe!')\n", - " \n", - " toggle = random_player()\n", - " player = players[toggle]\n", - " print('For this round, Player %s will go first!' %(player))\n", - " \n", - " game_on = True\n", - " input('Hit Enter to continue')\n", - " while game_on:\n", - " display_board(available,theBoard)\n", - " position = player_choice(theBoard,player)\n", - " place_marker(available,theBoard,player,position)\n", - "\n", - " if win_check(theBoard, player):\n", - " display_board(available,theBoard)\n", - " print('Congratulations! Player '+player+' wins!')\n", - " game_on = False\n", - " else:\n", - " if full_board_check(theBoard):\n", - " display_board(available,theBoard)\n", - " print('The game is a draw!')\n", - " break\n", - " else:\n", - " toggle *= -1\n", - " player = players[toggle]\n", - " clear_output()\n", - "\n", - " # reset the board and available moves list\n", - " theBoard = [' '] * 10\n", - " available = [str(num) for num in range(0,10)]\n", - " \n", - " if not replay():\n", - " break" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - } - ], - "metadata": { - "anaconda-cloud": {}, - "kernelspec": { - "display_name": "Python [default]", - "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.3" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/04-Milestone Project - 1/00-Warm-Up-Project-Exercises.ipynb b/04-Milestone Project - 1/00-Warm-Up-Project-Exercises.ipynb new file mode 100644 index 000000000..900f5524a --- /dev/null +++ b/04-Milestone Project - 1/00-Warm-Up-Project-Exercises.ipynb @@ -0,0 +1,1028 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Warm Up Project Exercises\n", + "\n", + "It is time to get you to put together all your skills to start building usable projects! Before you jump into our full milestone project, we will go through some warm-up component exercises, to get you comfortable with a few key ideas we use in the milestone project and larger projects in general, specifically:\n", + "\n", + "* Getting User Input\n", + "* Creating Functions that edit variables based on user input\n", + "* Generating output\n", + "* Joining User Inputs and Logic Flow" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Function to Display Information\n", + "\n", + "**Creating a function that displays a list for the user**" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def display_list(mylist):\n", + " print(mylist)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n" + ] + } + ], + "source": [ + "mylist = [0,1,2,3,4,5,6,7,8,9,10]\n", + "display_list(mylist)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Accepting User Input\n", + "\n", + "**Creating function that takes in an input from user and returns the result in the correct data type. Be careful when using the input() function, running that cell twice without providing an input value will cause python to get hung up waiting for the initial value on the first run. You will notice an In[*\\] next to the cell if it gets stuck, in which case, simply restart the kernel and re-run any necessary cells.**" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a value: 2\n" + ] + }, + { + "data": { + "text/plain": [ + "'2'" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "input('Please enter a value: ')" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number: 2\n" + ] + } + ], + "source": [ + "result = input(\"Please enter a number: \")" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'2'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "str" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(result)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "int(result)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number: 2\n" + ] + } + ], + "source": [ + "result = int(input(\"Please enter a number: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(result)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number: two\n" + ] + }, + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: 'two'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Example of an error!\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Please enter a number: \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'two'" + ] + } + ], + "source": [ + "# Example of an error!\n", + "result = int(input(\"Please enter a number: \"))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Creating a function to hold this logic: **" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def user_choice():\n", + " '''\n", + " User inputs a number (0-10) and we return this in integer form.\n", + " No parameter is passed when calling this function.\n", + " '''\n", + " choice = input(\"Please input a number (0-10)\")\n", + " \n", + " return int(choice)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please input a number (0-10)2\n" + ] + }, + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "user_choice()" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please input a number (0-10)2\n" + ] + } + ], + "source": [ + "result = user_choice()" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(result)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Validating User Input\n", + "\n", + "** Check that input is valid before attempting to convert.** \n", + "\n", + "We'll use a simple method here.\n", + "\n", + "As you get more advanced, you can start looking at other ways of doing this (these methods will make more sense later on in the course, so don't worry about them for now).\n", + "\n", + "* [Various Posts on This](https://www.google.com/search?q=python+check+if+input+is+number)\n", + "* [StackOverflow Post 1](https://stackoverflow.com/questions/5424716/how-to-check-if-string-input-is-a-number)\n", + "* [StackOverflow Post 2](https://stackoverflow.com/questions/1265665/how-can-i-check-if-a-string-represents-an-int-without-using-try-except)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "some_input = '10'" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Lot's of .is methods availble on string\n", + "some_input.isdigit()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "** Edit the function to confirm against an acceptable value or type **" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def user_choice():\n", + " \n", + " # This original choice value can be anything that isn't an integer\n", + " choice = 'wrong'\n", + " \n", + " # While the choice is not a digit, keep asking for input.\n", + " while choice.isdigit() == False:\n", + " \n", + " # we shouldn't convert here, otherwise we get an error on a wrong input\n", + " choice = input(\"Choose a number: \")\n", + " \n", + " # We can convert once the while loop above has confirmed we have a digit.\n", + " return int(choice)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Choose a number: hello\n", + "Choose a number: two\n", + "Choose a number: 2\n" + ] + }, + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "user_choice()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Let's try adding an error message within the while loop!**" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def user_choice():\n", + " \n", + " # This original choice value can be anything that isn't an integer\n", + " choice = 'wrong'\n", + " \n", + " # While the choice is not a digit, keep asking for input.\n", + " while choice.isdigit() == False:\n", + " \n", + " # we shouldn't convert here, otherwise we get an error on a wrong input\n", + " choice = input(\"Choose a number: \")\n", + " \n", + " # Error Message Check\n", + " if choice.isdigit() == False:\n", + " print(\"Sorry, but you did not enter an integer. Please try again.\")\n", + " \n", + " # We can convert once the while loop above has confirmed we have a digit.\n", + " return int(choice)" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Choose a number: two\n", + "Sorry, but you did not enter an integer. Please try again.\n", + "Choose a number: 2\n" + ] + }, + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "user_choice()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Now let's explore how to \"clear\" the output, that way we don't see the history of the \"Choose a number\" statements.**\n", + "\n", + "**NOTE: Jupyter Notebook users will use the IPython method shown here. Other IDE users (PyCharm, VS, etc..) will use **" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "from IPython.display import clear_output\n", + "clear_output()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def user_choice():\n", + " \n", + " # This original choice value can be anything that isn't an integer\n", + " choice = 'wrong'\n", + " \n", + " # While the choice is not a digit, keep asking for input.\n", + " while choice.isdigit() == False:\n", + " \n", + " # we shouldn't convert here, otherwise we get an error on a wrong input\n", + " choice = input(\"Choose a number: \")\n", + " \n", + " if choice.isdigit() == False:\n", + " # THIS CLEARS THE CURRENT OUTPUT BELOW THE CELL\n", + " clear_output()\n", + " \n", + " print(\"Sorry, but you did not enter an integer. Please try again.\")\n", + " \n", + " \n", + " # Optionally you can clear everything after running the function\n", + " # clear_output()\n", + " \n", + " # We can convert once the while loop above has confirmed we have a digit.\n", + " return int(choice)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Choose a number: 2\n" + ] + }, + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "user_choice()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Checking Against Multiple Possible Values**" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "result = 'wrong value'\n", + "acceptable_values = ['0','1','2']" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result in acceptable_values" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "result not in acceptable_values" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "from IPython.display import clear_output\n", + "clear_output()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def user_choice():\n", + " \n", + " # This original choice value can be anything that isn't an integer\n", + " choice = 'wrong'\n", + " \n", + " # While the choice is not a digit, keep asking for input.\n", + " while choice not in ['0','1','2']:\n", + " \n", + " # we shouldn't convert here, otherwise we get an error on a wrong input\n", + " choice = input(\"Choose one of these numbers (0,1,2): \")\n", + " \n", + " if choice not in ['0','1','2']:\n", + " # THIS CLEARS THE CURRENT OUTPUT BELOW THE CELL\n", + " clear_output()\n", + " \n", + " print(\"Sorry, but you did not choose a value in the correct range (0,1,2)\")\n", + " \n", + " \n", + " # Optionally you can clear everything after running the function\n", + " # clear_output()\n", + " \n", + " # We can convert once the while loop above has confirmed we have a digit.\n", + " return int(choice)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Choose one of these numbers (0,1,2): 1\n" + ] + }, + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "user_choice()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### More Flexible Example" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def user_choice():\n", + " \n", + " choice ='WRONG'\n", + " within_range = False\n", + " \n", + " while choice.isdigit() == False or within_range == False:\n", + " \n", + " \n", + " \n", + " choice = input(\"Please enter a number (0-10): \")\n", + " \n", + " if choice.isdigit() == False:\n", + " print(\"Sorry that is not a digit!\")\n", + " \n", + " if choice.isdigit() == True:\n", + " if int(choice) in range(0,10):\n", + " within_range = True\n", + " else:\n", + " within_range = False\n", + " \n", + " \n", + " return int(choice)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter a number (0-10): 12\n", + "Please enter a number (0-10): 2\n" + ] + }, + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "user_choice()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "-----\n", + "## Simple User Interaction\n", + "\n", + "**Finally let's combine all of these ideas to create a small game where a user can choose a \"position\" in an existing list and replace it with a value of their choice.**" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "game_list = [0,1,2]" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def display_game(game_list):\n", + " print(\"Here is the current list\")\n", + " print(game_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Here is the current list\n", + "['hi', 'no', 2]\n" + ] + } + ], + "source": [ + "display_game(game_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def position_choice():\n", + " \n", + " # This original choice value can be anything that isn't an integer\n", + " choice = 'wrong'\n", + " \n", + " # While the choice is not a digit, keep asking for input.\n", + " while choice not in ['0','1','2']:\n", + " \n", + " # we shouldn't convert here, otherwise we get an error on a wrong input\n", + " choice = input(\"Pick a position to replace (0,1,2): \")\n", + " \n", + " if choice not in ['0','1','2']:\n", + " # THIS CLEARS THE CURRENT OUTPUT BELOW THE CELL\n", + " clear_output()\n", + " \n", + " print(\"Sorry, but you did not choose a valid position (0,1,2)\")\n", + " \n", + " \n", + " # Optionally you can clear everything after running the function\n", + " # clear_output()\n", + " \n", + " # We can convert once the while loop above has confirmed we have a digit.\n", + " return int(choice)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def replacement_choice(game_list,position):\n", + " \n", + " user_placement = input(\"Type a string to place at the position\")\n", + " \n", + " game_list[position] = user_placement\n", + " \n", + " return game_list" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def gameon_choice():\n", + " \n", + " # This original choice value can be anything that isn't a Y or N\n", + " choice = 'wrong'\n", + " \n", + " # While the choice is not a digit, keep asking for input.\n", + " while choice not in ['Y','N']:\n", + " \n", + " # we shouldn't convert here, otherwise we get an error on a wrong input\n", + " choice = input(\"Would you like to keep playing? Y or N \")\n", + "\n", + " \n", + " if choice not in ['Y','N']:\n", + " # THIS CLEARS THE CURRENT OUTPUT BELOW THE CELL\n", + " clear_output()\n", + " \n", + " print(\"Sorry, I didn't understand. Please make sure to choose Y or N.\")\n", + " \n", + " \n", + " # Optionally you can clear everything after running the function\n", + " # clear_output()\n", + " \n", + " if choice == \"Y\":\n", + " # Game is still on\n", + " return True\n", + " else:\n", + " # Game is over\n", + " return False" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Game Logic All Together**" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Here is the current list\n", + "['34', 1, 'new value']\n", + "Would you like to keep playing? Y or N N\n" + ] + } + ], + "source": [ + "# Variable to keep game playing\n", + "game_on = True\n", + "\n", + "# First Game List\n", + "game_list = [0,1,2]\n", + "\n", + "\n", + "\n", + "while game_on:\n", + " \n", + " # Clear any historical output and show the game list\n", + " clear_output()\n", + " display_game(game_list)\n", + " \n", + " # Have player choose position\n", + " position = position_choice()\n", + " \n", + " # Rewrite that position and update game_list\n", + " game_list = replacement_choice(game_list,position)\n", + " \n", + " # Clear Screen and show the updated game list\n", + " clear_output()\n", + " display_game(game_list)\n", + " \n", + " # Ask if you want to keep playing\n", + " game_on = gameon_choice()\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Great work! You now have an understanding of bringing functions and loop logics together to build a simple game. This will be expanded upon in the Milestone project!**" + ] + } + ], + "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": 2 +} diff --git a/04-Milestone Project - 1/01-Milestone Project 1 - Assignment.ipynb b/04-Milestone Project - 1/01-Milestone Project 1 - Assignment.ipynb index b9b4a7634..2b887b9dd 100644 --- a/04-Milestone Project - 1/01-Milestone Project 1 - Assignment.ipynb +++ b/04-Milestone Project - 1/01-Milestone Project 1 - Assignment.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -54,7 +65,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/04-Milestone Project - 1/02-Milestone Project 1 - Walkthrough Steps Workbook.ipynb b/04-Milestone Project - 1/02-Milestone Project 1 - Walkthrough Steps Workbook.ipynb index 3242b1ddc..5ecb08f95 100644 --- a/04-Milestone Project - 1/02-Milestone Project 1 - Walkthrough Steps Workbook.ipynb +++ b/04-Milestone Project - 1/02-Milestone Project 1 - Walkthrough Steps Workbook.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -44,7 +55,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "from IPython.display import clear_output\n", @@ -64,7 +77,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "test_board = ['#','X','O','X','O','X','O','X','O','X']\n", @@ -81,7 +96,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def player_input():\n", @@ -99,7 +116,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "player_input()" @@ -115,7 +134,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def place_marker(board, marker, position):\n", @@ -133,7 +154,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "place_marker(test_board,'$',8)\n", @@ -150,7 +173,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def win_check(board, mark):\n", @@ -168,7 +193,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "win_check(test_board,'X')" @@ -184,7 +211,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "import random\n", @@ -203,7 +232,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def space_check(board, position):\n", @@ -221,7 +252,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def full_board_check(board):\n", @@ -239,7 +272,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def player_choice(board):\n", @@ -257,7 +292,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def replay():\n", @@ -277,7 +314,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "print('Welcome to Tic Tac Toe!')\n", @@ -324,7 +363,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/04-Milestone Project - 1/03-Milestone Project 1 - Complete Walkthrough Solution.ipynb b/04-Milestone Project - 1/03-Milestone Project 1 - Complete Walkthrough Solution.ipynb index cad94aa7e..9f71957f9 100644 --- a/04-Milestone Project - 1/03-Milestone Project 1 - Complete Walkthrough Solution.ipynb +++ b/04-Milestone Project - 1/03-Milestone Project 1 - Complete Walkthrough Solution.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -477,7 +488,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.1" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/04-Milestone Project - 1/04-OPTIONAL -Milestone Project 1 - Advanced Solution.ipynb b/04-Milestone Project - 1/04-OPTIONAL -Milestone Project 1 - Advanced Solution.ipynb deleted file mode 100644 index 0829bc577..000000000 --- a/04-Milestone Project - 1/04-OPTIONAL -Milestone Project 1 - Advanced Solution.ipynb +++ /dev/null @@ -1,264 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Tic Tac Toe - Advanced Solution\n", - "\n", - "This solution follows the same basic format as the Complete Walkthrough Solution, but takes advantage of some of the more advanced statements we have learned. Feel free to download the notebook to understand how it works!" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Specifically for the iPython Notebook environment for clearing output\n", - "from IPython.display import clear_output\n", - "import random\n", - "\n", - "# Global variables\n", - "theBoard = [' '] * 10 # a list of empty spaces\n", - "available = [str(num) for num in range(0,10)] # a List Comprehension\n", - "players = [0,'X','O'] # note that players[1] == 'X' and players[-1] == 'O'" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Available TIC-TAC-TOE\n", - " moves\n", - "\n", - " 7|8|9 | | \n", - " ----- -----\n", - " 4|5|6 | | \n", - " ----- -----\n", - " 1|2|3 | | \n", - "\n" - ] - } - ], - "source": [ - "def display_board(a,b):\n", - " print('Available TIC-TAC-TOE\\n'+\n", - " ' moves\\n\\n '+\n", - " a[7]+'|'+a[8]+'|'+a[9]+' '+b[7]+'|'+b[8]+'|'+b[9]+'\\n '+\n", - " '----- -----\\n '+\n", - " a[4]+'|'+a[5]+'|'+a[6]+' '+b[4]+'|'+b[5]+'|'+b[6]+'\\n '+\n", - " '----- -----\\n '+\n", - " a[1]+'|'+a[2]+'|'+a[3]+' '+b[1]+'|'+b[2]+'|'+b[3]+'\\n')\n", - "display_board(available,theBoard)" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Available TIC-TAC-TOE\n", - " moves\n", - "\n", - " 7|8|9 | | \n", - " ----- -----\n", - " 4|5|6 | | \n", - " ----- -----\n", - " 1|2|3 | | \n", - "\n" - ] - } - ], - "source": [ - "def display_board(a,b):\n", - " print(f'Available TIC-TAC-TOE\\n moves\\n\\n {a[7]}|{a[8]}|{a[9]} {b[7]}|{b[8]}|{b[9]}\\n ----- -----\\n {a[4]}|{a[5]}|{a[6]} {b[4]}|{b[5]}|{b[6]}\\n ----- -----\\n {a[1]}|{a[2]}|{a[3]} {b[1]}|{b[2]}|{b[3]}\\n')\n", - "display_board(available,theBoard)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def place_marker(avail,board,marker,position):\n", - " board[position] = marker\n", - " avail[position] = ' '" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def win_check(board,mark):\n", - "\n", - " return ((board[7] == board[8] == board[9] == mark) or # across the top\n", - " (board[4] == board[5] == board[6] == mark) or # across the middle\n", - " (board[1] == board[2] == board[3] == mark) or # across the bottom\n", - " (board[7] == board[4] == board[1] == mark) or # down the middle\n", - " (board[8] == board[5] == board[2] == mark) or # down the middle\n", - " (board[9] == board[6] == board[3] == mark) or # down the right side\n", - " (board[7] == board[5] == board[3] == mark) or # diagonal\n", - " (board[9] == board[5] == board[1] == mark)) # diagonal" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def random_player():\n", - " return random.choice((-1, 1))\n", - " \n", - "def space_check(board,position):\n", - " return board[position] == ' '\n", - "\n", - "def full_board_check(board):\n", - " return ' ' not in board[1:]" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def player_choice(board,player):\n", - " position = 0\n", - " \n", - " while position not in [1,2,3,4,5,6,7,8,9] or not space_check(board, position):\n", - " try:\n", - " position = int(input('Player %s, choose your next position: (1-9) '%(player)))\n", - " except:\n", - " print(\"I'm sorry, please try again.\")\n", - " \n", - " return position" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def replay():\n", - " \n", - " return input('Do you want to play again? Enter Yes or No: ').lower().startswith('y')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Welcome to Tic Tac Toe!\n", - "For this round, Player X will go first!\n" - ] - } - ], - "source": [ - "while True:\n", - " clear_output()\n", - " print('Welcome to Tic Tac Toe!')\n", - " \n", - " toggle = random_player()\n", - " player = players[toggle]\n", - " print('For this round, Player %s will go first!' %(player))\n", - " \n", - " game_on = True\n", - " input('Hit Enter to continue')\n", - " while game_on:\n", - " display_board(available,theBoard)\n", - " position = player_choice(theBoard,player)\n", - " place_marker(available,theBoard,player,position)\n", - "\n", - " if win_check(theBoard, player):\n", - " display_board(available,theBoard)\n", - " print('Congratulations! Player '+player+' wins!')\n", - " game_on = False\n", - " else:\n", - " if full_board_check(theBoard):\n", - " display_board(available,theBoard)\n", - " print('The game is a draw!')\n", - " break\n", - " else:\n", - " toggle *= -1\n", - " player = players[toggle]\n", - " clear_output()\n", - "\n", - " # reset the board and available moves list\n", - " theBoard = [' '] * 10\n", - " available = [str(num) for num in range(0,10)]\n", - " \n", - " if not replay():\n", - " break" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - } - ], - "metadata": { - "anaconda-cloud": {}, - "kernelspec": { - "display_name": "Python [default]", - "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.3" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/05-Object Oriented Programming/.ipynb_checkpoints/01-Object Oriented Programming-checkpoint.ipynb b/05-Object Oriented Programming/.ipynb_checkpoints/01-Object Oriented Programming-checkpoint.ipynb index 4d9e12e27..9e93e989d 100644 --- a/05-Object Oriented Programming/.ipynb_checkpoints/01-Object Oriented Programming-checkpoint.ipynb +++ b/05-Object Oriented Programming/.ipynb_checkpoints/01-Object Oriented Programming-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": { @@ -28,7 +39,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "lst = [1,2,3]" @@ -159,7 +172,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Dog:\n", @@ -244,7 +259,9 @@ { "cell_type": "code", "execution_count": 8, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Dog:\n", @@ -260,7 +277,9 @@ { "cell_type": "code", "execution_count": 9, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "sam = Dog('Lab','Sam')" @@ -415,7 +434,9 @@ { "cell_type": "code", "execution_count": 14, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Animal:\n", @@ -702,7 +723,9 @@ { "cell_type": "code", "execution_count": 23, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Book:\n", @@ -784,7 +807,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/05-Object Oriented Programming/.ipynb_checkpoints/02-Object Oriented Programming Homework-checkpoint.ipynb b/05-Object Oriented Programming/.ipynb_checkpoints/02-Object Oriented Programming Homework-checkpoint.ipynb index 3824be7be..7e10ddb75 100644 --- a/05-Object Oriented Programming/.ipynb_checkpoints/02-Object Oriented Programming Homework-checkpoint.ipynb +++ b/05-Object Oriented Programming/.ipynb_checkpoints/02-Object Oriented Programming Homework-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -14,7 +25,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Line:\n", @@ -32,7 +45,9 @@ { "cell_type": "code", "execution_count": 2, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# EXAMPLE OUTPUT\n", @@ -101,7 +116,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Cylinder:\n", @@ -119,7 +136,9 @@ { "cell_type": "code", "execution_count": 6, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# EXAMPLE OUTPUT\n", @@ -183,7 +202,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/05-Object Oriented Programming/.ipynb_checkpoints/03-Object Oriented Programming Homework - Solution-checkpoint.ipynb b/05-Object Oriented Programming/.ipynb_checkpoints/03-Object Oriented Programming Homework - Solution-checkpoint.ipynb index 81328ce4f..e17ef322c 100644 --- a/05-Object Oriented Programming/.ipynb_checkpoints/03-Object Oriented Programming Homework - Solution-checkpoint.ipynb +++ b/05-Object Oriented Programming/.ipynb_checkpoints/03-Object Oriented Programming Homework - Solution-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -14,7 +25,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Line(object):\n", @@ -37,7 +50,9 @@ { "cell_type": "code", "execution_count": 2, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "coordinate1 = (3,2)\n", @@ -104,7 +119,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Cylinder:\n", @@ -124,7 +141,9 @@ { "cell_type": "code", "execution_count": 6, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "c = Cylinder(2,3)" @@ -187,7 +206,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/05-Object Oriented Programming/.ipynb_checkpoints/04-OOP Challenge-checkpoint.ipynb b/05-Object Oriented Programming/.ipynb_checkpoints/04-OOP Challenge-checkpoint.ipynb index fb60ffb74..614576f12 100644 --- a/05-Object Oriented Programming/.ipynb_checkpoints/04-OOP Challenge-checkpoint.ipynb +++ b/05-Object Oriented Programming/.ipynb_checkpoints/04-OOP Challenge-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -24,7 +35,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Account:\n", @@ -34,7 +47,9 @@ { "cell_type": "code", "execution_count": 2, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# 1. Instantiate the class\n", @@ -179,7 +194,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/05-Object Oriented Programming/.ipynb_checkpoints/05-OOP Challenge - Solution-checkpoint.ipynb b/05-Object Oriented Programming/.ipynb_checkpoints/05-OOP Challenge - Solution-checkpoint.ipynb index 8c71a3e91..01f274c30 100644 --- a/05-Object Oriented Programming/.ipynb_checkpoints/05-OOP Challenge - Solution-checkpoint.ipynb +++ b/05-Object Oriented Programming/.ipynb_checkpoints/05-OOP Challenge - Solution-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -24,7 +35,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Account:\n", @@ -50,7 +63,9 @@ { "cell_type": "code", "execution_count": 2, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# 1. Instantiate the class\n", @@ -195,7 +210,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/05-Object Oriented Programming/01-Object Oriented Programming.ipynb b/05-Object Oriented Programming/01-Object Oriented Programming.ipynb index 4d9e12e27..9e93e989d 100644 --- a/05-Object Oriented Programming/01-Object Oriented Programming.ipynb +++ b/05-Object Oriented Programming/01-Object Oriented Programming.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": { @@ -28,7 +39,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "lst = [1,2,3]" @@ -159,7 +172,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Dog:\n", @@ -244,7 +259,9 @@ { "cell_type": "code", "execution_count": 8, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Dog:\n", @@ -260,7 +277,9 @@ { "cell_type": "code", "execution_count": 9, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "sam = Dog('Lab','Sam')" @@ -415,7 +434,9 @@ { "cell_type": "code", "execution_count": 14, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Animal:\n", @@ -702,7 +723,9 @@ { "cell_type": "code", "execution_count": 23, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Book:\n", @@ -784,7 +807,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/05-Object Oriented Programming/02-Object Oriented Programming Homework.ipynb b/05-Object Oriented Programming/02-Object Oriented Programming Homework.ipynb index 3824be7be..7e10ddb75 100644 --- a/05-Object Oriented Programming/02-Object Oriented Programming Homework.ipynb +++ b/05-Object Oriented Programming/02-Object Oriented Programming Homework.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -14,7 +25,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Line:\n", @@ -32,7 +45,9 @@ { "cell_type": "code", "execution_count": 2, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# EXAMPLE OUTPUT\n", @@ -101,7 +116,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Cylinder:\n", @@ -119,7 +136,9 @@ { "cell_type": "code", "execution_count": 6, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# EXAMPLE OUTPUT\n", @@ -183,7 +202,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/05-Object Oriented Programming/03-Object Oriented Programming Homework - Solution.ipynb b/05-Object Oriented Programming/03-Object Oriented Programming Homework - Solution.ipynb index 81328ce4f..e17ef322c 100644 --- a/05-Object Oriented Programming/03-Object Oriented Programming Homework - Solution.ipynb +++ b/05-Object Oriented Programming/03-Object Oriented Programming Homework - Solution.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -14,7 +25,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Line(object):\n", @@ -37,7 +50,9 @@ { "cell_type": "code", "execution_count": 2, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "coordinate1 = (3,2)\n", @@ -104,7 +119,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Cylinder:\n", @@ -124,7 +141,9 @@ { "cell_type": "code", "execution_count": 6, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "c = Cylinder(2,3)" @@ -187,7 +206,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/05-Object Oriented Programming/04-OOP Challenge.ipynb b/05-Object Oriented Programming/04-OOP Challenge.ipynb index fb60ffb74..614576f12 100644 --- a/05-Object Oriented Programming/04-OOP Challenge.ipynb +++ b/05-Object Oriented Programming/04-OOP Challenge.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -24,7 +35,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Account:\n", @@ -34,7 +47,9 @@ { "cell_type": "code", "execution_count": 2, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# 1. Instantiate the class\n", @@ -179,7 +194,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/05-Object Oriented Programming/05-OOP Challenge - Solution.ipynb b/05-Object Oriented Programming/05-OOP Challenge - Solution.ipynb index 8c71a3e91..01f274c30 100644 --- a/05-Object Oriented Programming/05-OOP Challenge - Solution.ipynb +++ b/05-Object Oriented Programming/05-OOP Challenge - Solution.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -24,7 +35,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Account:\n", @@ -50,7 +63,9 @@ { "cell_type": "code", "execution_count": 2, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# 1. Instantiate the class\n", @@ -195,7 +210,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/.ipynb_checkpoints/Useful_Info_Notebook-checkpoint.ipynb b/06-Modules and Packages/.ipynb_checkpoints/Useful_Info_Notebook-checkpoint.ipynb index f7317b76d..b22b6d95e 100644 --- a/06-Modules and Packages/.ipynb_checkpoints/Useful_Info_Notebook-checkpoint.ipynb +++ b/06-Modules and Packages/.ipynb_checkpoints/Useful_Info_Notebook-checkpoint.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)" + "\n", + "___\n", + "
Content Copyright by Pierian Data
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**file1.py** is going to be used as a module.\n", + "# Modules and Packages\n", "\n", - "Note that it doesn't print or return anything,\n", - "it just defines a function called *myfunc* and a variable called *list1*.\n", - "## Writing scripts" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Writing file2.py\n" - ] - } - ], - "source": [ - "%%writefile file2.py\n", - "import file1\n", - "file1.list1.append(12)\n", - "print(file1.list1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**file2.py** is a Python script.\n", + "There's no code here because it didn't really make sense for the section. Check out the video lectures for more info and the resources for this.\n", "\n", - "First, we import our **file1** module (note the lack of a .py extension)
\n", - "Next, we access the *list1* variable inside **file1**, and perform a list method on it.
\n", - "`.append(12)` proves we're working with a Python list object, and not just a string.
\n", - "Finally, we tell our script to print the modified list.\n", - "## Running scripts" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0, 2, 4, 6, 8, 10, 12]\n" - ] - } - ], - "source": [ - "! python file2.py" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here we run our script from the command line. The exclamation point is a Jupyter trick that lets you run command line statements from inside a jupyter cell." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0, 2, 4, 6, 8, 10]\n" - ] - } - ], - "source": [ - "import file1\n", - "print(file1.list1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The above cell proves that we never altered **file1.py**, we just appended a number to the list *after* it was brought into **file2**." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Passing command line arguments\n", - "Python's `sys` module gives you access to command line arguments when calling scripts." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Writing file3.py\n" - ] - } - ], - "source": [ - "%%writefile file3.py\n", - "import sys\n", - "import file1\n", - "num = int(sys.argv[1])\n", - "print(file1.myfunc(num))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Note that we selected the second item in the list of arguments with `sys.argv[1]`.
\n", - "This is because the list created with `sys.argv` always starts with the name of the file being used.
" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n" - ] - } - ], - "source": [ - "! python file3.py 21" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here we're passing 21 to be the upper range value used by the *myfunc* function in **list1.py**" + "Here is the best source the official docs!\n", + "https://docs.python.org/3/tutorial/modules.html#packages\n", + "\n", + "But I really like the info here: https://python4astronomers.github.io/installation/packages.html\n", + "\n", + "Here's some extra info to help:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Understanding modules\n", - "\n", "Modules in Python are simply Python files with the .py extension, which implement a set of functions. Modules are imported from other modules using the 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)" + "\n", + "___\n", + "
Content Copyright by Pierian Data
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "**file1.py** is going to be used as a module.\n", + "# Modules and Packages\n", "\n", - "Note that it doesn't print or return anything,\n", - "it just defines a function called *myfunc* and a variable called *list1*.\n", - "## Writing scripts" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Writing file2.py\n" - ] - } - ], - "source": [ - "%%writefile file2.py\n", - "import file1\n", - "file1.list1.append(12)\n", - "print(file1.list1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**file2.py** is a Python script.\n", + "There's no code here because it didn't really make sense for the section. Check out the video lectures for more info and the resources for this.\n", "\n", - "First, we import our **file1** module (note the lack of a .py extension)
\n", - "Next, we access the *list1* variable inside **file1**, and perform a list method on it.
\n", - "`.append(12)` proves we're working with a Python list object, and not just a string.
\n", - "Finally, we tell our script to print the modified list.\n", - "## Running scripts" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0, 2, 4, 6, 8, 10, 12]\n" - ] - } - ], - "source": [ - "! python file2.py" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here we run our script from the command line. The exclamation point is a Jupyter trick that lets you run command line statements from inside a jupyter cell." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0, 2, 4, 6, 8, 10]\n" - ] - } - ], - "source": [ - "import file1\n", - "print(file1.list1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The above cell proves that we never altered **file1.py**, we just appended a number to the list *after* it was brought into **file2**." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Passing command line arguments\n", - "Python's `sys` module gives you access to command line arguments when calling scripts." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Writing file3.py\n" - ] - } - ], - "source": [ - "%%writefile file3.py\n", - "import sys\n", - "import file1\n", - "num = int(sys.argv[1])\n", - "print(file1.myfunc(num))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Note that we selected the second item in the list of arguments with `sys.argv[1]`.
\n", - "This is because the list created with `sys.argv` always starts with the name of the file being used.
" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n" - ] - } - ], - "source": [ - "! python file3.py 21" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here we're passing 21 to be the upper range value used by the *myfunc* function in **list1.py**" + "Here is the best source the official docs!\n", + "https://docs.python.org/3/tutorial/modules.html#packages\n", + "\n", + "But I really like the info here: https://python4astronomers.github.io/installation/packages.html\n", + "\n", + "Here's some extra info to help:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Understanding modules\n", - "\n", "Modules in Python are simply Python files with the .py extension, which implement a set of functions. Modules are imported from other modules using the 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", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -201,7 +212,9 @@ { "cell_type": "code", "execution_count": 6, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def askint():\n", @@ -275,7 +288,9 @@ { "cell_type": "code", "execution_count": 9, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def askint():\n", @@ -335,7 +350,9 @@ { "cell_type": "code", "execution_count": 11, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def askint():\n", @@ -390,7 +407,9 @@ { "cell_type": "code", "execution_count": 13, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def askint():\n", @@ -455,7 +474,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/07-Errors and Exception Handling/.ipynb_checkpoints/02-Errors and Exceptions Homework-checkpoint.ipynb b/07-Errors and Exception Handling/.ipynb_checkpoints/02-Errors and Exceptions Homework-checkpoint.ipynb index f873f230e..2e634ac98 100644 --- a/07-Errors and Exception Handling/.ipynb_checkpoints/02-Errors and Exceptions Homework-checkpoint.ipynb +++ b/07-Errors and Exception Handling/.ipynb_checkpoints/02-Errors and Exceptions Homework-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -80,7 +91,9 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def ask():\n", @@ -131,7 +144,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/07-Errors and Exception Handling/.ipynb_checkpoints/03-Errors and Exceptions Homework - Solution-checkpoint.ipynb b/07-Errors and Exception Handling/.ipynb_checkpoints/03-Errors and Exceptions Homework - Solution-checkpoint.ipynb index b4f9c520c..9a6d5e282 100644 --- a/07-Errors and Exception Handling/.ipynb_checkpoints/03-Errors and Exceptions Homework - Solution-checkpoint.ipynb +++ b/07-Errors and Exception Handling/.ipynb_checkpoints/03-Errors and Exceptions Homework - Solution-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -80,7 +91,9 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def ask():\n", @@ -142,7 +155,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/07-Errors and Exception Handling/.ipynb_checkpoints/04-Unit Testing-checkpoint.ipynb b/07-Errors and Exception Handling/.ipynb_checkpoints/04-Unit Testing-checkpoint.ipynb index 84455477d..9fe096efd 100644 --- a/07-Errors and Exception Handling/.ipynb_checkpoints/04-Unit Testing-checkpoint.ipynb +++ b/07-Errors and Exception Handling/.ipynb_checkpoints/04-Unit Testing-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -50,7 +61,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "! pip install pylint" @@ -545,7 +558,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/07-Errors and Exception Handling/01-Errors and Exceptions Handling.ipynb b/07-Errors and Exception Handling/01-Errors and Exceptions Handling.ipynb index c135164e8..f22c75756 100644 --- a/07-Errors and Exception Handling/01-Errors and Exceptions Handling.ipynb +++ b/07-Errors and Exception Handling/01-Errors and Exceptions Handling.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -201,7 +212,9 @@ { "cell_type": "code", "execution_count": 6, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def askint():\n", @@ -275,7 +288,9 @@ { "cell_type": "code", "execution_count": 9, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def askint():\n", @@ -335,7 +350,9 @@ { "cell_type": "code", "execution_count": 11, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def askint():\n", @@ -390,7 +407,9 @@ { "cell_type": "code", "execution_count": 13, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def askint():\n", @@ -455,7 +474,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/07-Errors and Exception Handling/02-Errors and Exceptions Homework.ipynb b/07-Errors and Exception Handling/02-Errors and Exceptions Homework.ipynb index f873f230e..2e634ac98 100644 --- a/07-Errors and Exception Handling/02-Errors and Exceptions Homework.ipynb +++ b/07-Errors and Exception Handling/02-Errors and Exceptions Homework.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -80,7 +91,9 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def ask():\n", @@ -131,7 +144,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/07-Errors and Exception Handling/03-Errors and Exceptions Homework - Solution.ipynb b/07-Errors and Exception Handling/03-Errors and Exceptions Homework - Solution.ipynb index b4f9c520c..9a6d5e282 100644 --- a/07-Errors and Exception Handling/03-Errors and Exceptions Homework - Solution.ipynb +++ b/07-Errors and Exception Handling/03-Errors and Exceptions Homework - Solution.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -80,7 +91,9 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def ask():\n", @@ -142,7 +155,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/07-Errors and Exception Handling/04-Unit Testing.ipynb b/07-Errors and Exception Handling/04-Unit Testing.ipynb index 84455477d..9fe096efd 100644 --- a/07-Errors and Exception Handling/04-Unit Testing.ipynb +++ b/07-Errors and Exception Handling/04-Unit Testing.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -50,7 +61,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "! pip install pylint" @@ -545,7 +558,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/08-Milestone Project - 2/.ipynb_checkpoints/00-Milestone-2-Warmup-Project-checkpoint.ipynb b/08-Milestone Project - 2/.ipynb_checkpoints/00-Milestone-2-Warmup-Project-checkpoint.ipynb new file mode 100644 index 000000000..c50355da7 --- /dev/null +++ b/08-Milestone Project - 2/.ipynb_checkpoints/00-Milestone-2-Warmup-Project-checkpoint.ipynb @@ -0,0 +1,1508 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Warmup Project Exercise" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Simple War Game\n", + "\n", + "Before we launch in to the OOP Milestone 2 Project, let's walk through together on using OOP for a more robust and complex application, such as a game. We will use Python OOP to simulate a simplified version of the game war. Two players will each start off with half the deck, then they each remove a card, compare which card has the highest value, and the player with the higher card wins both cards. In the event of a time" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Single Card Class\n", + "\n", + "### Creating a Card Class with outside variables\n", + "\n", + "Here we will use some outside variables that we know don't change regardless of the situation, such as a deck of cards. Regardless of what round,match, or game we're playing, we'll still need the same deck of cards." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# We'll use this later\n", + "import random " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')\n", + "ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')\n", + "values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, \n", + " 'Nine':9, 'Ten':10, 'Jack':11, 'Queen':12, 'King':13, 'Ace':14}" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "class Card:\n", + " \n", + " def __init__(self,suit,rank):\n", + " self.suit = suit\n", + " self.rank = rank\n", + " self.value = values[rank]\n", + " \n", + " def __str__(self):\n", + " return self.rank + ' of ' + self.suit" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create an example card" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Hearts'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "suits[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Two'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ranks[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "two_hearts = Card(suits[0],ranks[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "<__main__.Card at 0x1dfaff6b898>" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "two_hearts" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Two of Hearts\n" + ] + } + ], + "source": [ + "print(two_hearts)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Two'" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "two_hearts.rank" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "two_hearts.value" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "values[two_hearts.rank]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Deck Class\n", + "\n", + "### Using a class within another class\n", + "\n", + "We just created a single card, but how can we create an entire Deck of cards? Let's explore doing this with a class that utilizes the Card class." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A Deck will be made up of multiple Cards. Which mean's we will actually use the Card class within the \\_\\_init__ of the Deck class." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "class Deck:\n", + " \n", + " def __init__(self):\n", + " # Note this only happens once upon creation of a new Deck\n", + " self.all_cards = [] \n", + " for suit in suits:\n", + " for rank in ranks:\n", + " # This assumes the Card class has already been defined!\n", + " self.all_cards.append(Card(suit,rank))\n", + " \n", + " def shuffle(self):\n", + " # Note this doesn't return anything\n", + " random.shuffle(self.all_cards)\n", + " \n", + " def deal_one(self):\n", + " # Note we remove one card from the list of all_cards\n", + " return self.all_cards.pop() " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Create a Deck" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "mydeck = Deck()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "52" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(mydeck.all_cards)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "<__main__.Card at 0x1dfaff269e8>" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mydeck.all_cards[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Two of Hearts\n" + ] + } + ], + "source": [ + "print(mydeck.all_cards[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "mydeck.shuffle()" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Five of Spades\n" + ] + } + ], + "source": [ + "print(mydeck.all_cards[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_card = mydeck.deal_one()" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "King of Clubs\n" + ] + } + ], + "source": [ + "print(my_card)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Player Class\n", + "\n", + "Let's create a Player Class, a player should be able to hold instances of Cards, they should also be able to remove and add them from their hand. We want the Player class to be flexible enough to add one card, or many cards so we'll use a simple if check to keep it all in the same method." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We'll keep this all in mind as we create the methods for the Player class.\n", + "\n", + "### Player Class" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "class Player:\n", + " \n", + " def __init__(self,name):\n", + " self.name = name\n", + " # A new player has no cards\n", + " self.all_cards = [] \n", + " \n", + " def remove_one(self):\n", + " # Note we remove one card from the list of all_cards\n", + " # We state 0 to remove from the \"top\" of the deck\n", + " # We'll imagine index -1 as the bottom of the deck\n", + " return self.all_cards.pop(0)\n", + " \n", + " def add_cards(self,new_cards):\n", + " if type(new_cards) == type([]):\n", + " self.all_cards.extend(new_cards)\n", + " else:\n", + " self.all_cards.append(new_cards)\n", + " \n", + " \n", + " def __str__(self):\n", + " return f'Player {self.name} has {len(self.all_cards)} cards.'" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "jose = Player(\"Jose\")" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "<__main__.Player at 0x1dfaff8b940>" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "jose" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Player Jose has 0 cards.\n" + ] + } + ], + "source": [ + "print(jose)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "<__main__.Card at 0x1dfaff6b898>" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "two_hearts" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "jose.add_cards(two_hearts)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Player Jose has 1 cards.\n" + ] + } + ], + "source": [ + "print(jose)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "jose.add_cards([two_hearts,two_hearts,two_hearts])" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Player Jose has 4 cards.\n" + ] + } + ], + "source": [ + "print(jose)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## War Game Logic" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "player_one = Player(\"One\")" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "player_two = Player(\"Two\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Setup New Game" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "new_deck = Deck()" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "new_deck.shuffle()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Split the Deck between players" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "26.0" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(new_deck.all_cards)/2" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "for x in range(26):\n", + " player_one.add_cards(new_deck.deal_one())\n", + " player_two.add_cards(new_deck.deal_one())" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(new_deck.all_cards)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "26" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(player_one.all_cards)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "26" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(player_two.all_cards)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Play the Game" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import pdb" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "game_on = True" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Round 1\n", + "Round 2\n", + "Round 3\n", + "Round 4\n", + "Round 5\n", + "Round 6\n", + "Round 7\n", + "Round 8\n", + "Round 9\n", + "Round 10\n", + "Round 11\n", + "Round 12\n", + "Round 13\n", + "Round 14\n", + "Round 15\n", + "Round 16\n", + "Round 17\n", + "Round 18\n", + "Round 19\n", + "Round 20\n", + "Round 21\n", + "Round 22\n", + "Round 23\n", + "Round 24\n", + "Round 25\n", + "Round 26\n", + "Round 27\n", + "Player One out of cards! Game Over\n" + ] + } + ], + "source": [ + "round_num = 0\n", + "while game_on:\n", + " \n", + " round_num += 1\n", + " print(f\"Round {round_num}\")\n", + " \n", + " # Check to see if a player is out of cards:\n", + " if len(player_one.all_cards) == 0:\n", + " print(\"Player One out of cards! Game Over\")\n", + " print(\"Player Two Wins!\")\n", + " game_on = False\n", + " break\n", + " \n", + " if len(player_two.all_cards) == 0:\n", + " print(\"Player Two out of cards! Game Over\")\n", + " print(\"Player One Wins!\")\n", + " game_on = False\n", + " break\n", + " \n", + " # Otherwise, the game is still on!\n", + " \n", + " # Start a new round and reset current cards \"on the table\"\n", + " player_one_cards = []\n", + " player_one_cards.append(player_one.remove_one())\n", + " \n", + " player_two_cards = []\n", + " player_two_cards.append(player_two.remove_one())\n", + " \n", + " at_war = True\n", + "\n", + " while at_war:\n", + "\n", + "\n", + " if player_one_cards[-1].value > player_two_cards[-1].value:\n", + "\n", + " # Player One gets the cards\n", + " player_one.add_cards(player_one_cards)\n", + " player_one.add_cards(player_two_cards)\n", + " \n", + " \n", + " # No Longer at \"war\" , time for next round\n", + " at_war = False\n", + " \n", + " # Player Two Has higher Card\n", + " elif player_one_cards[-1].value < player_two_cards[-1].value:\n", + "\n", + " # Player Two gets the cards\n", + " player_two.add_cards(player_one_cards)\n", + " player_two.add_cards(player_two_cards)\n", + " \n", + " # No Longer at \"war\" , time for next round\n", + " at_war = False\n", + "\n", + " else:\n", + " print('WAR!')\n", + " # This occurs when the cards are equal.\n", + " # We'll grab another card each and continue the current war.\n", + " \n", + " # First check to see if player has enough cards\n", + " \n", + " # Check to see if a player is out of cards:\n", + " if len(player_one.all_cards) < 5:\n", + " print(\"Player One unable to play war! Game Over at War\")\n", + " print(\"Player Two Wins! Player One Loses!\")\n", + " game_on = False\n", + " break\n", + "\n", + " elif len(player_two.all_cards) < 5:\n", + " print(\"Player Two unable to play war! Game Over at War\")\n", + " print(\"Player One Wins! Player One Loses!\")\n", + " game_on = False\n", + " break\n", + " # Otherwise, we're still at war, so we'll add the next cards\n", + " else:\n", + " for num in range(5):\n", + " player_one_cards.append(player_one.remove_one())\n", + " player_two_cards.append(player_two.remove_one())\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Game Setup in One Cell" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "player_one = Player(\"One\")\n", + "player_two = Player(\"Two\")\n", + "\n", + "new_deck = Deck()\n", + "new_deck.shuffle()\n", + "\n", + "for x in range(26):\n", + " player_one.add_cards(new_deck.deal_one())\n", + " player_two.add_cards(new_deck.deal_one())\n", + " \n", + "game_on = True" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Round 1\n", + "Round 2\n", + "WAR!\n", + "Round 3\n", + "WAR!\n", + "WAR!\n", + "Round 4\n", + "WAR!\n", + "Round 5\n", + "Round 6\n", + "Round 7\n", + "Round 8\n", + "Round 9\n", + "Round 10\n", + "Round 11\n", + "Round 12\n", + "Round 13\n", + "WAR!\n", + "Round 14\n", + "Round 15\n", + "WAR!\n", + "Round 16\n", + "Round 17\n", + "Round 18\n", + "Round 19\n", + "Round 20\n", + "Round 21\n", + "Round 22\n", + "Round 23\n", + "Round 24\n", + "Round 25\n", + "Round 26\n", + "Round 27\n", + "Round 28\n", + "Round 29\n", + "WAR!\n", + "Round 30\n", + "Round 31\n", + "Round 32\n", + "WAR!\n", + "Round 33\n", + "Round 34\n", + "Round 35\n", + "Round 36\n", + "Round 37\n", + "Round 38\n", + "Round 39\n", + "Round 40\n", + "Round 41\n", + "Round 42\n", + "WAR!\n", + "WAR!\n", + "Round 43\n", + "Round 44\n", + "Round 45\n", + "Round 46\n", + "Round 47\n", + "Round 48\n", + "Round 49\n", + "Round 50\n", + "Round 51\n", + "Round 52\n", + "Round 53\n", + "Round 54\n", + "Round 55\n", + "Round 56\n", + "Round 57\n", + "Round 58\n", + "Round 59\n", + "Round 60\n", + "Round 61\n", + "Round 62\n", + "Round 63\n", + "Round 64\n", + "Round 65\n", + "Round 66\n", + "Round 67\n", + "Round 68\n", + "Round 69\n", + "Round 70\n", + "Round 71\n", + "Round 72\n", + "Round 73\n", + "Round 74\n", + "Round 75\n", + "Round 76\n", + "Round 77\n", + "Round 78\n", + "Round 79\n", + "Round 80\n", + "Round 81\n", + "Round 82\n", + "Round 83\n", + "Round 84\n", + "Round 85\n", + "Round 86\n", + "Round 87\n", + "Round 88\n", + "Round 89\n", + "Round 90\n", + "Round 91\n", + "Round 92\n", + "Round 93\n", + "Round 94\n", + "Round 95\n", + "Round 96\n", + "Round 97\n", + "Round 98\n", + "Round 99\n", + "Round 100\n", + "Round 101\n", + "Round 102\n", + "Round 103\n", + "Round 104\n", + "Round 105\n", + "Round 106\n", + "Round 107\n", + "Round 108\n", + "WAR!\n", + "Round 109\n", + "Round 110\n", + "Round 111\n", + "Round 112\n", + "Round 113\n", + "Round 114\n", + "Round 115\n", + "Round 116\n", + "Round 117\n", + "Round 118\n", + "Round 119\n", + "Round 120\n", + "Round 121\n", + "Round 122\n", + "Round 123\n", + "Round 124\n", + "Round 125\n", + "Round 126\n", + "Round 127\n", + "Round 128\n", + "Round 129\n", + "Round 130\n", + "Round 131\n", + "Round 132\n", + "Round 133\n", + "Round 134\n", + "WAR!\n", + "Round 135\n", + "Round 136\n", + "WAR!\n", + "Round 137\n", + "WAR!\n", + "Round 138\n", + "Round 139\n", + "Round 140\n", + "Round 141\n", + "Round 142\n", + "Round 143\n", + "Round 144\n", + "Round 145\n", + "Round 146\n", + "Round 147\n", + "Round 148\n", + "Round 149\n", + "Round 150\n", + "Round 151\n", + "Round 152\n", + "Round 153\n", + "Round 154\n", + "Round 155\n", + "Round 156\n", + "Round 157\n", + "Round 158\n", + "Round 159\n", + "Round 160\n", + "Round 161\n", + "Round 162\n", + "Round 163\n", + "Round 164\n", + "Round 165\n", + "Round 166\n", + "Round 167\n", + "Round 168\n", + "Round 169\n", + "Round 170\n", + "Round 171\n", + "Round 172\n", + "Round 173\n", + "Round 174\n", + "Round 175\n", + "Round 176\n", + "Round 177\n", + "Round 178\n", + "Round 179\n", + "Round 180\n", + "Round 181\n", + "Round 182\n", + "Round 183\n", + "Round 184\n", + "Round 185\n", + "Round 186\n", + "Round 187\n", + "Round 188\n", + "Round 189\n", + "Round 190\n", + "Round 191\n", + "Round 192\n", + "Round 193\n", + "Round 194\n", + "Round 195\n", + "Round 196\n", + "Round 197\n", + "Round 198\n", + "Round 199\n", + "Round 200\n", + "Round 201\n", + "Round 202\n", + "Round 203\n", + "Round 204\n", + "Round 205\n", + "Round 206\n", + "Round 207\n", + "Round 208\n", + "Round 209\n", + "Round 210\n", + "Round 211\n", + "Round 212\n", + "Round 213\n", + "Round 214\n", + "Round 215\n", + "Round 216\n", + "Round 217\n", + "Round 218\n", + "Round 219\n", + "Round 220\n", + "Round 221\n", + "Round 222\n", + "Round 223\n", + "WAR!\n", + "Round 224\n", + "Round 225\n", + "Round 226\n", + "Round 227\n", + "Round 228\n", + "Round 229\n", + "Round 230\n", + "Round 231\n", + "Round 232\n", + "Round 233\n", + "Round 234\n", + "Round 235\n", + "Round 236\n", + "Round 237\n", + "Round 238\n", + "Round 239\n", + "Round 240\n", + "Round 241\n", + "Round 242\n", + "Round 243\n", + "Round 244\n", + "Round 245\n", + "Round 246\n", + "Round 247\n", + "Round 248\n", + "Round 249\n", + "Round 250\n", + "Round 251\n", + "Round 252\n", + "Round 253\n", + "Round 254\n", + "Round 255\n", + "Round 256\n", + "Round 257\n", + "WAR!\n", + "Round 258\n", + "Round 259\n", + "Round 260\n", + "Round 261\n", + "Round 262\n", + "Round 263\n", + "Round 264\n", + "Round 265\n", + "Round 266\n", + "Round 267\n", + "Round 268\n", + "Round 269\n", + "Round 270\n", + "Round 271\n", + "Round 272\n", + "Round 273\n", + "Round 274\n", + "Round 275\n", + "Round 276\n", + "Round 277\n", + "Round 278\n", + "Round 279\n", + "Round 280\n", + "Round 281\n", + "Round 282\n", + "Round 283\n", + "Round 284\n", + "Round 285\n", + "Round 286\n", + "Round 287\n", + "Round 288\n", + "Round 289\n", + "Round 290\n", + "Round 291\n", + "Round 292\n", + "Round 293\n", + "Round 294\n", + "Round 295\n", + "Round 296\n", + "Round 297\n", + "Round 298\n", + "Round 299\n", + "Round 300\n", + "Round 301\n", + "Round 302\n", + "Round 303\n", + "Round 304\n", + "Round 305\n", + "Round 306\n", + "Round 307\n", + "WAR!\n", + "Round 308\n", + "Round 309\n", + "Round 310\n", + "Round 311\n", + "Round 312\n", + "Round 313\n", + "Round 314\n", + "Round 315\n", + "WAR!\n", + "Round 316\n", + "Round 317\n", + "Round 318\n", + "Round 319\n", + "Round 320\n", + "Round 321\n", + "Round 322\n", + "Round 323\n", + "Round 324\n", + "Round 325\n", + "Round 326\n", + "Round 327\n", + "Round 328\n", + "Round 329\n", + "Round 330\n", + "Round 331\n", + "Round 332\n", + "Round 333\n", + "Round 334\n", + "Round 335\n", + "Round 336\n", + "Round 337\n", + "Round 338\n", + "Round 339\n", + "Round 340\n", + "Round 341\n", + "Round 342\n", + "Round 343\n", + "Round 344\n", + "Round 345\n", + "Round 346\n", + "Round 347\n", + "Round 348\n", + "Round 349\n", + "WAR!\n", + "Round 350\n", + "WAR!\n", + "Player Two unable to play war! Game Over at War\n", + "Player One Wins! Player One Loses!\n" + ] + } + ], + "source": [ + "round_num = 0\n", + "while game_on:\n", + " \n", + " round_num += 1\n", + " print(f\"Round {round_num}\")\n", + " \n", + " # Check to see if a player is out of cards:\n", + " if len(player_one.all_cards) == 0:\n", + " print(\"Player One out of cards! Game Over\")\n", + " print(\"Player Two Wins!\")\n", + " game_on = False\n", + " break\n", + " \n", + " if len(player_two.all_cards) == 0:\n", + " print(\"Player Two out of cards! Game Over\")\n", + " print(\"Player One Wins!\")\n", + " game_on = False\n", + " break\n", + " \n", + " # Otherwise, the game is still on!\n", + " \n", + " # Start a new round and reset current cards \"on the table\"\n", + " player_one_cards = []\n", + " player_one_cards.append(player_one.remove_one())\n", + " \n", + " player_two_cards = []\n", + " player_two_cards.append(player_two.remove_one())\n", + " \n", + " at_war = True\n", + "\n", + " while at_war:\n", + "\n", + "\n", + " if player_one_cards[-1].value > player_two_cards[-1].value:\n", + "\n", + " # Player One gets the cards\n", + " player_one.add_cards(player_one_cards)\n", + " player_one.add_cards(player_two_cards)\n", + " \n", + " \n", + " # No Longer at \"war\" , time for next round\n", + " at_war = False\n", + " \n", + " # Player Two Has higher Card\n", + " elif player_one_cards[-1].value < player_two_cards[-1].value:\n", + "\n", + " # Player Two gets the cards\n", + " player_two.add_cards(player_one_cards)\n", + " player_two.add_cards(player_two_cards)\n", + " \n", + " # No Longer at \"war\" , time for next round\n", + " at_war = False\n", + "\n", + " else:\n", + " print('WAR!')\n", + " # This occurs when the cards are equal.\n", + " # We'll grab another card each and continue the current war.\n", + " \n", + " # First check to see if player has enough cards\n", + " \n", + " # Check to see if a player is out of cards:\n", + " if len(player_one.all_cards) < 5:\n", + " print(\"Player One unable to play war! Game Over at War\")\n", + " print(\"Player Two Wins! Player One Loses!\")\n", + " game_on = False\n", + " break\n", + "\n", + " elif len(player_two.all_cards) < 5:\n", + " print(\"Player Two unable to play war! Game Over at War\")\n", + " print(\"Player One Wins! Player One Loses!\")\n", + " game_on = False\n", + " break\n", + " # Otherwise, we're still at war, so we'll add the next cards\n", + " else:\n", + " for num in range(5):\n", + " player_one_cards.append(player_one.remove_one())\n", + " player_two_cards.append(player_two.remove_one())\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "27" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(player_one.all_cards)" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "25" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(player_two.all_cards)" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ace of Diamonds\n" + ] + } + ], + "source": [ + "print(player_one_cards[-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Four of Hearts\n" + ] + } + ], + "source": [ + "print(player_two_cards[-1])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "## Great Work!\n", + "\n", + "Other links that may interest you:\n", + "* https://www.reddit.com/r/learnpython/comments/7ay83p/war_card_game/\n", + "* https://codereview.stackexchange.com/questions/131174/war-card-game-using-classes\n", + "* https://gist.github.com/damianesteban/6896120\n", + "* https://lethain.com/war-card-game-in-python/\n", + "* https://hectorpefo.github.io/2017-09-13-Card-Wars/\n", + "* https://www.wimpyprogrammer.com/the-statistics-of-war-the-card-game" + ] + } + ], + "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": 2 +} diff --git a/08-Milestone Project - 2/.ipynb_checkpoints/01-Milestone Project 2 - Assignment-checkpoint.ipynb b/08-Milestone Project - 2/.ipynb_checkpoints/01-Milestone Project 2 - Assignment-checkpoint.ipynb index 53b748698..2a105368b 100644 --- a/08-Milestone Project - 2/.ipynb_checkpoints/01-Milestone Project 2 - Assignment-checkpoint.ipynb +++ b/08-Milestone Project - 2/.ipynb_checkpoints/01-Milestone Project 2 - Assignment-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -43,7 +54,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/08-Milestone Project - 2/.ipynb_checkpoints/02-Milestone Project 2 - Walkthrough Steps Workbook-checkpoint.ipynb b/08-Milestone Project - 2/.ipynb_checkpoints/02-Milestone Project 2 - Walkthrough Steps Workbook-checkpoint.ipynb index 12a43cfb3..dac1f317b 100644 --- a/08-Milestone Project - 2/.ipynb_checkpoints/02-Milestone Project 2 - Walkthrough Steps Workbook-checkpoint.ipynb +++ b/08-Milestone Project - 2/.ipynb_checkpoints/02-Milestone Project 2 - Walkthrough Steps Workbook-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": { @@ -55,7 +66,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "import random\n", @@ -86,7 +99,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Card:\n", @@ -115,7 +130,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Deck:\n", @@ -146,7 +163,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "test_deck = Deck()\n", @@ -171,7 +190,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Hand:\n", @@ -198,7 +219,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Chips:\n", @@ -233,7 +256,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def take_bet():\n", @@ -252,7 +277,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def hit(deck,hand):\n", @@ -272,7 +299,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def hit_or_stand(deck,hand):\n", @@ -292,7 +321,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def show_some(player,dealer):\n", @@ -315,7 +346,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def player_busts():\n", @@ -344,7 +377,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "while True:\n", @@ -417,7 +452,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/08-Milestone Project - 2/.ipynb_checkpoints/03-Milestone Project 2 - Complete Walkthrough Solution-checkpoint.ipynb b/08-Milestone Project - 2/.ipynb_checkpoints/03-Milestone Project 2 - Complete Walkthrough Solution-checkpoint.ipynb index 24cb151fa..0293f36dd 100644 --- a/08-Milestone Project - 2/.ipynb_checkpoints/03-Milestone Project 2 - Complete Walkthrough Solution-checkpoint.ipynb +++ b/08-Milestone Project - 2/.ipynb_checkpoints/03-Milestone Project 2 - Complete Walkthrough Solution-checkpoint.ipynb @@ -1,5 +1,16 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, { "cell_type": "markdown", "metadata": { @@ -55,7 +66,9 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "import random\n", @@ -87,7 +100,9 @@ { "cell_type": "code", "execution_count": 2, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Card:\n", @@ -117,7 +132,9 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Deck:\n", @@ -237,7 +254,9 @@ { "cell_type": "code", "execution_count": 5, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Hand:\n", @@ -322,7 +341,9 @@ { "cell_type": "code", "execution_count": 8, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Hand:\n", @@ -362,7 +383,9 @@ { "cell_type": "code", "execution_count": 9, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class Chips:\n", @@ -411,7 +434,9 @@ { "cell_type": "code", "execution_count": 10, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def take_bet(chips):\n", @@ -468,7 +493,9 @@ { "cell_type": "code", "execution_count": 11, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def hit(deck,hand):\n", @@ -489,7 +516,9 @@ { "cell_type": "code", "execution_count": 12, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def hit_or_stand(deck,hand):\n", @@ -522,7 +551,9 @@ { "cell_type": "code", "execution_count": 13, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def show_some(player,dealer):\n", @@ -569,7 +600,9 @@ { "cell_type": "code", "execution_count": 14, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def player_busts(player,dealer,chips):\n", @@ -799,7 +832,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.6.6" } }, "nbformat": 4, diff --git a/08-Milestone Project - 2/.ipynb_checkpoints/04-Milestone Project 2 - Solution Code-checkpoint.ipynb b/08-Milestone Project - 2/.ipynb_checkpoints/04-Milestone Project 2 - Solution Code-checkpoint.ipynb deleted file mode 100644 index af44df772..000000000 --- a/08-Milestone Project - 2/.ipynb_checkpoints/04-Milestone Project 2 - Solution Code-checkpoint.ipynb +++ /dev/null @@ -1,321 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Milestone Project 2 - Solution Code\n", - "Below is an implementation of a simple game of Blackjack. Notice the use of OOP and classes for the cards and hands." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Welcome to BlackJack! Get as close to 21 as you can without going over!\n", - " Dealer hits until she reaches 17. Aces count as 1 or 11.\n", - "How many chips would you like to bet? 50\n", - "\n", - "Dealer's Hand:\n", - "