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 new file mode 100644 index 000000000..c869c3ab6 --- /dev/null +++ b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/01-Variable Assignment-checkpoint.ipynb @@ -0,0 +1,466 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Variable Assignment\n", + "\n", + "## Rules for variable names\n", + "* names can not start with a number\n", + "* names can not contain spaces, use _ intead\n", + "* names can not contain any of these symbols:\n", + "\n", + " :'\",<>/?|\\!@#%^&*~-+\n", + " \n", + "* it's considered best practice ([PEP8](https://www.python.org/dev/peps/pep-0008/#function-and-variable-names)) that names are lowercase with underscores\n", + "* avoid using Python built-in keywords like `list` and `str`\n", + "* avoid using the single characters `l` (lowercase letter el), `O` (uppercase letter oh) and `I` (uppercase letter eye) as they can be confused with `1` and `0`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Dynamic Typing\n", + "\n", + "Python uses *dynamic typing*, meaning you can reassign variables to different data types. This makes Python very flexible in assigning data types; it differs from other languages that are *statically typed*." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_dogs = 2" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_dogs" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_dogs = ['Sammy', 'Frankie']" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Sammy', 'Frankie']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_dogs" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Pros and Cons of Dynamic Typing\n", + "#### Pros of Dynamic Typing\n", + "* very easy to work with\n", + "* faster development time\n", + "\n", + "#### Cons of Dynamic Typing\n", + "* may result in unexpected bugs!\n", + "* you need to be aware of `type()`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Assigning Variables\n", + "Variable assignment follows `name = object`, where a single equals sign `=` is an *assignment operator*" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "a = 5" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here we assigned the integer object `5` to the variable name `a`.
Let's assign `a` to something else:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "a = 10" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can now use `a` in place of the number `10`:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a + a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Reassigning Variables\n", + "Python lets you reassign variables with a reference to the same object." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "a = a + 10" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "There's actually a shortcut for this. Python lets you add, subtract, multiply and divide numbers with reassignment using `+=`, `-=`, `*=`, and `/=`." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "a += 10" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "a *= 2" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "60" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Determining variable type with `type()`\n", + "You can check what type of object is assigned to a variable using Python's built-in `type()` function. Common data types include:\n", + "* **int** (for integer)\n", + "* **float**\n", + "* **str** (for string)\n", + "* **list**\n", + "* **tuple**\n", + "* **dict** (for dictionary)\n", + "* **set**\n", + "* **bool** (for Boolean True/False)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "a = (1,2)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Simple Exercise\n", + "This shows how variables make calculations more readable and easier to follow." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_income = 100\n", + "tax_rate = 0.1\n", + "my_taxes = my_income * tax_rate" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10.0" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_taxes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Great! You should now understand the basics of variable assignment and reassignment in Python.
Up next, we'll learn about strings!" + ] + } + ], + "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/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 new file mode 100644 index 000000000..28a42ec48 --- /dev/null +++ b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/02-Strings-checkpoint.ipynb @@ -0,0 +1,1020 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Strings" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Strings are used in Python to record text information, such as names. Strings in Python are actually a *sequence*, which basically means Python keeps track of every element in the string as a sequence. For example, Python understands the string \"hello' to be a sequence of letters in a specific order. This means we will be able to use indexing to grab particular letters (like the first letter, or the last letter).\n", + "\n", + "This idea of a sequence is an important one in Python and we will touch upon it later on in the future.\n", + "\n", + "In this lecture we'll learn about the following:\n", + "\n", + " 1.) Creating Strings\n", + " 2.) Printing Strings\n", + " 3.) String Indexing and Slicing\n", + " 4.) String Properties\n", + " 5.) String Methods\n", + " 6.) Print Formatting" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Creating a String\n", + "To create a string in Python you need to use either single quotes or double quotes. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'hello'" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Single word\n", + "'hello'" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'This is also a string'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Entire phrase \n", + "'This is also a string'" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'String built with double quotes'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# We can also use double quote\n", + "\"String built with double quotes\"" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (, line 2)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m ' I'm using single quotes, but this will create an error'\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "# Be careful with quotes!\n", + "' I'm using single quotes, but this will create an error'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The reason for the error above is because the single quote in I'm stopped the string. You can use combinations of double and single quotes to get the complete statement." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"Now I'm ready to use the single quotes inside a string!\"" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"Now I'm ready to use the single quotes inside a string!\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's learn about printing strings!" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Printing a String\n", + "\n", + "Using Jupyter notebook with just a string in a cell will automatically output strings, but the correct way to display strings in your output is by using a print function." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Hello World'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# We can simply declare a string\n", + "'Hello World'" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Hello World 2'" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Note that we can't output multiple strings this way\n", + "'Hello World 1'\n", + "'Hello World 2'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can use a print statement to print a string." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World 1\n", + "Hello World 2\n", + "Use \n", + " to print a new line\n", + "\n", + "\n", + "See what I mean?\n" + ] + } + ], + "source": [ + "print('Hello World 1')\n", + "print('Hello World 2')\n", + "print('Use \\n to print a new line')\n", + "print('\\n')\n", + "print('See what I mean?')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## String Basics" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can also use a function called len() to check the length of a string!" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "11" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len('Hello World')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Python's built-in len() function counts all of the characters in the string, including spaces and punctuation." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## String Indexing\n", + "We know strings are a sequence, which means Python can use indexes to call parts of the sequence. Let's learn how this works.\n", + "\n", + "In Python, we use brackets [] after an object to call its index. We should also note that indexing starts at 0 for Python. Let's create a new object called s and then walk through a few examples of indexing." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Assign s as a string\n", + "s = 'Hello World'" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Hello World'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Check\n", + "s" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n" + ] + } + ], + "source": [ + "# Print the object\n", + "print(s) " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's start indexing!" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'H'" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Show first element (in this case a letter)\n", + "s[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'e'" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s[1]" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'l'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can use a : to perform *slicing* which grabs everything up to a designated point. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'ello World'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Grab everything past the first term all the way to the length of s which is len(s)\n", + "s[1:]" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Hello World'" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Note that there is no change to the original s\n", + "s" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Hel'" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Grab everything UP TO the 3rd index\n", + "s[:3]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Note the above slicing. Here we're telling Python to grab everything from 0 up to 3. It doesn't include the 3rd index. You'll notice this a lot in Python, where statements and are usually in the context of \"up to, but not including\"." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Hello World'" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Everything\n", + "s[:]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can also use negative indexing to go backwards." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'d'" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Last letter (one index behind 0 so it loops back around)\n", + "s[-1]" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Hello Worl'" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Grab everything but the last letter\n", + "s[:-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can also use index and slice notation to grab elements of a sequence by a specified step size (the default is 1). For instance we can use two colons in a row and then a number specifying the frequency to grab elements. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Hello World'" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Grab everything, but go in steps size of 1\n", + "s[::1]" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'HloWrd'" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Grab everything, but go in step sizes of 2\n", + "s[::2]" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'dlroW olleH'" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# We can use this to print a string backwards\n", + "s[::-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "## String Properties\n", + "It's important to note that strings have an important property known as *immutability*. This means that once a string is created, the elements within it can not be changed or replaced. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Hello World'" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'str' 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[0;32m 1\u001b[0m \u001b[1;31m# Let's try to change the first letter to 'x'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0ms\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'x'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" + ] + } + ], + "source": [ + "# Let's try to change the first letter to 'x'\n", + "s[0] = 'x'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Notice how the error tells us directly what we can't do, change the item assignment!\n", + "\n", + "Something we *can* do is concatenate strings!" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Hello World'" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Hello World concatenate me!'" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Concatenate strings!\n", + "s + ' concatenate me!'" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# We can reassign s completely though!\n", + "s = s + ' concatenate me!'" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World concatenate me!\n" + ] + } + ], + "source": [ + "print(s)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Hello World concatenate me!'" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can use the multiplication symbol to create repetition!" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "letter = 'z'" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'zzzzzzzzzz'" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "letter*10" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Basic Built-in String methods\n", + "\n", + "Objects in Python usually have built-in methods. These methods are functions inside the object (we will learn about these in much more depth later) that can perform actions or commands on the object itself.\n", + "\n", + "We call methods with a period and then the method name. Methods are in the form:\n", + "\n", + "object.method(parameters)\n", + "\n", + "Where parameters are extra arguments we can pass into the method. Don't worry if the details don't make 100% sense right now. Later on we will be creating our own objects and functions!\n", + "\n", + "Here are some examples of built-in methods in strings:" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Hello World concatenate me!'" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'HELLO WORLD CONCATENATE ME!'" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Upper Case a string\n", + "s.upper()" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'hello world concatenate me!'" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Lower case\n", + "s.lower()" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Hello', 'World', 'concatenate', 'me!']" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Split a string by blank space (this is the default)\n", + "s.split()" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Hello ', 'orld concatenate me!']" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Split by a specific element (doesn't include the element that was split on)\n", + "s.split('W')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "There are many more methods than the ones covered here. Visit the Advanced String section to find out more!" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Print Formatting\n", + "\n", + "We can use the .format() method to add formatted objects to printed string statements. \n", + "\n", + "The easiest way to show this is through an example:" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Insert another string with curly brackets: The inserted string'" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'Insert another string with curly brackets: {}'.format('The inserted string')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We will revisit this string formatting topic in later sections when we are building our projects!" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Next up: Lists!" + ] + } + ], + "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/03-String Formatting-checkpoint.ipynb b/00-Python Object and Data Structure Basics/.ipynb_checkpoints/03-Print Formatting with Strings-checkpoint.ipynb similarity index 98% rename from 00-Python Object and Data Structure Basics/.ipynb_checkpoints/03-String Formatting-checkpoint.ipynb rename to 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-String Formatting-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 new file mode 100644 index 000000000..c869c3ab6 --- /dev/null +++ b/00-Python Object and Data Structure Basics/01-Variable Assignment.ipynb @@ -0,0 +1,466 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Variable Assignment\n", + "\n", + "## Rules for variable names\n", + "* names can not start with a number\n", + "* names can not contain spaces, use _ intead\n", + "* names can not contain any of these symbols:\n", + "\n", + " :'\",<>/?|\\!@#%^&*~-+\n", + " \n", + "* it's considered best practice ([PEP8](https://www.python.org/dev/peps/pep-0008/#function-and-variable-names)) that names are lowercase with underscores\n", + "* avoid using Python built-in keywords like `list` and `str`\n", + "* avoid using the single characters `l` (lowercase letter el), `O` (uppercase letter oh) and `I` (uppercase letter eye) as they can be confused with `1` and `0`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Dynamic Typing\n", + "\n", + "Python uses *dynamic typing*, meaning you can reassign variables to different data types. This makes Python very flexible in assigning data types; it differs from other languages that are *statically typed*." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_dogs = 2" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_dogs" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_dogs = ['Sammy', 'Frankie']" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Sammy', 'Frankie']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_dogs" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Pros and Cons of Dynamic Typing\n", + "#### Pros of Dynamic Typing\n", + "* very easy to work with\n", + "* faster development time\n", + "\n", + "#### Cons of Dynamic Typing\n", + "* may result in unexpected bugs!\n", + "* you need to be aware of `type()`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Assigning Variables\n", + "Variable assignment follows `name = object`, where a single equals sign `=` is an *assignment operator*" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "a = 5" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here we assigned the integer object `5` to the variable name `a`.
Let's assign `a` to something else:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "a = 10" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can now use `a` in place of the number `10`:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a + a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Reassigning Variables\n", + "Python lets you reassign variables with a reference to the same object." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "a = a + 10" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "There's actually a shortcut for this. Python lets you add, subtract, multiply and divide numbers with reassignment using `+=`, `-=`, `*=`, and `/=`." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "a += 10" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "a *= 2" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "60" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Determining variable type with `type()`\n", + "You can check what type of object is assigned to a variable using Python's built-in `type()` function. Common data types include:\n", + "* **int** (for integer)\n", + "* **float**\n", + "* **str** (for string)\n", + "* **list**\n", + "* **tuple**\n", + "* **dict** (for dictionary)\n", + "* **set**\n", + "* **bool** (for Boolean True/False)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "a = (1,2)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Simple Exercise\n", + "This shows how variables make calculations more readable and easier to follow." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "my_income = 100\n", + "tax_rate = 0.1\n", + "my_taxes = my_income * tax_rate" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10.0" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_taxes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Great! You should now understand the basics of variable assignment and reassignment in Python.
Up next, we'll learn about strings!" + ] + } + ], + "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/00-Python Object and Data Structure Basics/02-Strings.ipynb b/00-Python Object and Data Structure Basics/02-Strings.ipynb index 89d2e4e29..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": {}, @@ -11,7 +22,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Strings are used in Python to record text information, such as nameS. Strings in Python are actually a *sequence*, which basically means Python keeps track of every element in the string as a sequence. For example, Python understands the string \"hello' to be a sequence of letters in a specific order. This means we will be able to use indexing to grab particular letters (like the first letter, or the last letter).\n", + "Strings are used in Python to record text information, such as names. Strings in Python are actually a *sequence*, which basically means Python keeps track of every element in the string as a sequence. For example, Python understands the string \"hello' to be a sequence of letters in a specific order. This means we will be able to use indexing to grab particular letters (like the first letter, or the last letter).\n", "\n", "This idea of a sequence is an important one in Python and we will touch upon it later on in the future.\n", "\n", @@ -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-String Formatting.ipynb b/00-Python Object and Data Structure Basics/03-Print Formatting with Strings.ipynb similarity index 98% rename from 00-Python Object and Data Structure Basics/03-String Formatting.ipynb rename to 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-String Formatting.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 3f93a3369..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", @@ -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 1932220d1..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.1" + "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 e448be126..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": {}, @@ -345,7 +356,7 @@ }, "outputs": [], "source": [ - "def has_33(pattern,text):\n", + "def has_33(nums):\n", " pass" ] }, @@ -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 352fd3d12..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": {}, @@ -217,7 +228,7 @@ "source": [ "## lambda expression\n", "\n", - "One of Pythons most useful (and for recruits, confusing) tools is the lambda expression. lambda expressions allow us to create \"anonymous\" functions. This basically means we can quickly make ad-hoc functions without needing to properly define a function using def.\n", + "One of Pythons most useful (and for beginners, confusing) tools is the lambda expression. lambda expressions allow us to create \"anonymous\" functions. This basically means we can quickly make ad-hoc functions without needing to properly define a function using def.\n", "\n", "Function objects returned by running lambda expressions work exactly the same as those created and assigned by defs. There is key difference that makes lambda useful in specialized roles:\n", "\n", @@ -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 e448be126..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": {}, @@ -345,7 +356,7 @@ }, "outputs": [], "source": [ - "def has_33(pattern,text):\n", + "def has_33(nums):\n", " pass" ] }, @@ -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 352fd3d12..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": {}, @@ -217,7 +228,7 @@ "source": [ "## lambda expression\n", "\n", - "One of Pythons most useful (and for recruits, confusing) tools is the lambda expression. lambda expressions allow us to create \"anonymous\" functions. This basically means we can quickly make ad-hoc functions without needing to properly define a function using def.\n", + "One of Pythons most useful (and for beginners, confusing) tools is the lambda expression. lambda expressions allow us to create \"anonymous\" functions. This basically means we can quickly make ad-hoc functions without needing to properly define a function using def.\n", "\n", "Function objects returned by running lambda expressions work exactly the same as those created and assigned by defs. There is key difference that makes lambda useful in specialized roles:\n", "\n", @@ -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 8e84722d0..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": {}, @@ -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/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/08-Modules and Packages/.ipynb_checkpoints/01-Modules and Packages-checkpoint.ipynb b/06-Modules and Packages/.ipynb_checkpoints/01-Modules and Packages-checkpoint.ipynb similarity index 100% rename from 08-Modules and Packages/.ipynb_checkpoints/01-Modules and Packages-checkpoint.ipynb rename to 06-Modules and Packages/.ipynb_checkpoints/01-Modules and Packages-checkpoint.ipynb diff --git a/08-Modules and Packages/01-Modules and Packages.ipynb b/06-Modules and Packages/.ipynb_checkpoints/Useful_Info_Notebook-checkpoint.ipynb similarity index 94% rename from 08-Modules and Packages/01-Modules and Packages.ipynb rename to 06-Modules and Packages/.ipynb_checkpoints/Useful_Info_Notebook-checkpoint.ipynb index 786b0485a..b22b6d95e 100644 --- a/08-Modules and Packages/01-Modules and Packages.ipynb +++ b/06-Modules and Packages/.ipynb_checkpoints/Useful_Info_Notebook-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": [ "# import the library\n", @@ -151,7 +164,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "# OR could do it this way\n", @@ -197,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/00-Modules_and_Packages/MyMainPackage/SubPackage/__init__.py b/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/SubPackage/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/SubPackage/__pycache__/__init__.cpython-36.pyc b/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/SubPackage/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 000000000..d4fedf2ca Binary files /dev/null and b/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/SubPackage/__pycache__/__init__.cpython-36.pyc differ diff --git a/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/SubPackage/__pycache__/mysubscript.cpython-36.pyc b/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/SubPackage/__pycache__/mysubscript.cpython-36.pyc new file mode 100644 index 000000000..24a8ba483 Binary files /dev/null and b/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/SubPackage/__pycache__/mysubscript.cpython-36.pyc differ diff --git a/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/SubPackage/mysubscript.py b/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/SubPackage/mysubscript.py new file mode 100644 index 000000000..0c4217ffe --- /dev/null +++ b/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/SubPackage/mysubscript.py @@ -0,0 +1,2 @@ +def sub_report(): + print("Hey Im a function inside mysubscript") \ No newline at end of file diff --git a/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/__init__.py b/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/__pycache__/__init__.cpython-36.pyc b/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 000000000..36ec225ce Binary files /dev/null and b/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/__pycache__/__init__.cpython-36.pyc differ diff --git a/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/__pycache__/some_main_script.cpython-36.pyc b/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/__pycache__/some_main_script.cpython-36.pyc new file mode 100644 index 000000000..c0b08646a Binary files /dev/null and b/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/__pycache__/some_main_script.cpython-36.pyc differ diff --git a/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/some_main_script.py b/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/some_main_script.py new file mode 100644 index 000000000..b1c7287ac --- /dev/null +++ b/06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/some_main_script.py @@ -0,0 +1,2 @@ +def report_main(): + print("Hey I am in some_main_script in main package.") \ No newline at end of file diff --git a/06-Modules and Packages/00-Modules_and_Packages/mymodule.py b/06-Modules and Packages/00-Modules_and_Packages/mymodule.py new file mode 100644 index 000000000..5970453b8 --- /dev/null +++ b/06-Modules and Packages/00-Modules_and_Packages/mymodule.py @@ -0,0 +1,2 @@ +def my_func(): + print("Hey I am in mymodule.py") \ No newline at end of file diff --git a/06-Modules and Packages/00-Modules_and_Packages/myprogram.py b/06-Modules and Packages/00-Modules_and_Packages/myprogram.py new file mode 100644 index 000000000..e08a2cb0d --- /dev/null +++ b/06-Modules and Packages/00-Modules_and_Packages/myprogram.py @@ -0,0 +1,6 @@ +from MyMainPackage.some_main_script import report_main +from MyMainPackage.SubPackage import mysubscript + +report_main() + +mysubscript.sub_report() diff --git a/06-Modules and Packages/01-Name_and_Main/Explanation.txt b/06-Modules and Packages/01-Name_and_Main/Explanation.txt new file mode 100644 index 000000000..19eb661e7 --- /dev/null +++ b/06-Modules and Packages/01-Name_and_Main/Explanation.txt @@ -0,0 +1,76 @@ +Sometimes when you are importing from a module, you would like to know whether +a modules function is being used as an import, or if you are using the original +.py file of that module. In this case we can use the: + + if __name__ == "__main__": + +line to determine this. For example: + +When your script is run by passing it as a command to the Python interpreter: + + python myscript.py + +all of the code that is at indentation level 0 gets executed. Functions and +classes that are defined are, well, defined, but none of their code gets ran. +Unlike other languages, there's no main() function that gets run automatically +- the main() function is implicitly all the code at the top level. + +In this case, the top-level code is an if block. __name__ is a built-in variable + which evaluate to the name of the current module. However, if a module is being + run directly (as in myscript.py above), then __name__ instead is set to the + string "__main__". Thus, you can test whether your script is being run directly + or being imported by something else by testing + + if __name__ == "__main__": + ... + +If that code is being imported into another module, the various function and +class definitions will be imported, but the main() code won't get run. As a +basic example, consider the following two scripts: + + # file one.py + def func(): + print("func() in one.py") + + print("top-level in one.py") + + if __name__ == "__main__": + print("one.py is being run directly") + else: + print("one.py is being imported into another module") + +and then: + + # file two.py + import one + + print("top-level in two.py") + one.func() + + if __name__ == "__main__": + print("two.py is being run directly") + else: + print("two.py is being imported into another module") + +Now, if you invoke the interpreter as + + python one.py + +The output will be + + top-level in one.py + +one.py is being run directly +If you run two.py instead: + + python two.py + +You get + + top-level in one.py + one.py is being imported into another module + top-level in two.py + func() in one.py + two.py is being run directly + +Thus, when module one gets loaded, its __name__ equals "one" instead of __main__. diff --git a/06-Modules and Packages/01-Name_and_Main/one.py b/06-Modules and Packages/01-Name_and_Main/one.py new file mode 100644 index 000000000..41818a638 --- /dev/null +++ b/06-Modules and Packages/01-Name_and_Main/one.py @@ -0,0 +1,9 @@ +def func(): + print("func() ran in one.py") + +print("top-level print inside of one.py") + +if __name__ == "__main__": + print("one.py is being run directly") +else: + print("one.py is being imported into another module") diff --git a/06-Modules and Packages/01-Name_and_Main/two.py b/06-Modules and Packages/01-Name_and_Main/two.py new file mode 100644 index 000000000..ec630497d --- /dev/null +++ b/06-Modules and Packages/01-Name_and_Main/two.py @@ -0,0 +1,10 @@ +import one + +print("top-level in two.py") + +one.func() + +if __name__ == "__main__": + print("two.py is being run directly") +else: + print("two.py is being imported into another module") diff --git a/06-Modules and Packages/Useful_Info_Notebook.ipynb b/06-Modules and Packages/Useful_Info_Notebook.ipynb new file mode 100644 index 000000000..b22b6d95e --- /dev/null +++ b/06-Modules and Packages/Useful_Info_Notebook.ipynb @@ -0,0 +1,220 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "___\n", + "\n", + "\n", + "___\n", + "
Content Copyright by Pierian Data
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Modules and Packages\n", + "\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", + "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": [ + "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", + "\n", + "The first time a module is loaded into a running Python script, it is initialized by executing the code in the module once. If another module in your code imports the same module again, it will not be loaded twice but once only - so local variables inside the module act as a \"singleton\" - they are initialized only once.\n", + "\n", + "If we want to import the math module, we simply import the name of the module:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# import the library\n", + "import math" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# use it (ceiling rounding)\n", + "math.ceil(2.4)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exploring built-in modules\n", + "Two very important functions come in handy when exploring modules in Python - the dir and help functions.\n", + "\n", + "We can look for which functions are implemented in each module by using the dir function:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']\n" + ] + } + ], + "source": [ + "print(dir(math))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "When we find the function in the module we want to use, we can read about it more using the help function, inside the Python interpreter:\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on built-in function ceil in module math:\n", + "\n", + "ceil(...)\n", + " ceil(x)\n", + " \n", + " Return the ceiling of x as an Integral.\n", + " This is the smallest integer >= x.\n", + "\n" + ] + } + ], + "source": [ + "help(math.ceil)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Writing modules\n", + "Writing Python modules is very simple. To create a module of your own, simply create a new .py file with the module name, and then import it using the Python file name (without the .py extension) using the import command.\n", + "\n", + "## Writing packages\n", + "Packages are name-spaces which contain multiple packages and modules themselves. They are simply directories, but with a twist.\n", + "\n", + "Each package in Python is a directory which MUST contain a special file called **\\__init\\__.py**. This file can be empty, and it indicates that the directory it contains is a Python package, so it can be imported the same way a module can be imported.\n", + "\n", + "If we create a directory called foo, which marks the package name, we can then create a module inside that package called bar. We also must not forget to add the **\\__init\\__.py** file inside the foo directory.\n", + "\n", + "To use the module bar, we can import it in two ways:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Just an example, this won't work\n", + "import foo.bar" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# OR could do it this way\n", + "from foo import bar" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the first method, we must use the foo prefix whenever we access the module bar. In the second method, we don't, because we import the module to our module's name-space.\n", + "\n", + "The **\\__init\\__.py** file can also decide which modules the package exports as the API, while keeping other modules internal, by overriding the **\\__all\\__** variable, like so:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "__init__.py:\n", + "\n", + "__all__ = [\"bar\"]" + ] + } + ], + "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/06-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 similarity index 97% rename from 06-Errors and Exception Handling/.ipynb_checkpoints/01-Errors and Exceptions Handling-checkpoint.ipynb rename to 07-Errors and Exception Handling/.ipynb_checkpoints/01-Errors and Exceptions Handling-checkpoint.ipynb index c135164e8..f22c75756 100644 --- a/06-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/06-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 similarity index 92% rename from 06-Errors and Exception Handling/.ipynb_checkpoints/02-Errors and Exceptions Homework-checkpoint.ipynb rename to 07-Errors and Exception Handling/.ipynb_checkpoints/02-Errors and Exceptions Homework-checkpoint.ipynb index f873f230e..2e634ac98 100644 --- a/06-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/06-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 similarity index 90% rename from 06-Errors and Exception Handling/.ipynb_checkpoints/03-Errors and Exceptions Homework - Solution-checkpoint.ipynb rename to 07-Errors and Exception Handling/.ipynb_checkpoints/03-Errors and Exceptions Homework - Solution-checkpoint.ipynb index b4f9c520c..9a6d5e282 100644 --- a/06-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/06-Errors and Exception Handling/.ipynb_checkpoints/04-Unit Testing-checkpoint.ipynb b/07-Errors and Exception Handling/.ipynb_checkpoints/04-Unit Testing-checkpoint.ipynb similarity index 97% rename from 06-Errors and Exception Handling/.ipynb_checkpoints/04-Unit Testing-checkpoint.ipynb rename to 07-Errors and Exception Handling/.ipynb_checkpoints/04-Unit Testing-checkpoint.ipynb index 84455477d..9fe096efd 100644 --- a/06-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/06-Errors and Exception Handling/01-Errors and Exceptions Handling.ipynb b/07-Errors and Exception Handling/01-Errors and Exceptions Handling.ipynb similarity index 97% rename from 06-Errors and Exception Handling/01-Errors and Exceptions Handling.ipynb rename to 07-Errors and Exception Handling/01-Errors and Exceptions Handling.ipynb index c135164e8..f22c75756 100644 --- a/06-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/06-Errors and Exception Handling/02-Errors and Exceptions Homework.ipynb b/07-Errors and Exception Handling/02-Errors and Exceptions Homework.ipynb similarity index 92% rename from 06-Errors and Exception Handling/02-Errors and Exceptions Homework.ipynb rename to 07-Errors and Exception Handling/02-Errors and Exceptions Homework.ipynb index f873f230e..2e634ac98 100644 --- a/06-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/06-Errors and Exception Handling/03-Errors and Exceptions Homework - Solution.ipynb b/07-Errors and Exception Handling/03-Errors and Exceptions Homework - Solution.ipynb similarity index 90% rename from 06-Errors and Exception Handling/03-Errors and Exceptions Homework - Solution.ipynb rename to 07-Errors and Exception Handling/03-Errors and Exceptions Homework - Solution.ipynb index b4f9c520c..9a6d5e282 100644 --- a/06-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/06-Errors and Exception Handling/04-Unit Testing.ipynb b/07-Errors and Exception Handling/04-Unit Testing.ipynb similarity index 97% rename from 06-Errors and Exception Handling/04-Unit Testing.ipynb rename to 07-Errors and Exception Handling/04-Unit Testing.ipynb index 84455477d..9fe096efd 100644 --- a/06-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/06-Errors and Exception Handling/__pycache__/cap.cpython-36.pyc b/07-Errors and Exception Handling/__pycache__/cap.cpython-36.pyc similarity index 100% rename from 06-Errors and Exception Handling/__pycache__/cap.cpython-36.pyc rename to 07-Errors and Exception Handling/__pycache__/cap.cpython-36.pyc diff --git a/06-Errors and Exception Handling/cap.py b/07-Errors and Exception Handling/cap.py similarity index 100% rename from 06-Errors and Exception Handling/cap.py rename to 07-Errors and Exception Handling/cap.py diff --git a/06-Errors and Exception Handling/simple1.py b/07-Errors and Exception Handling/simple1.py similarity index 100% rename from 06-Errors and Exception Handling/simple1.py rename to 07-Errors and Exception Handling/simple1.py diff --git a/06-Errors and Exception Handling/simple2.py b/07-Errors and Exception Handling/simple2.py similarity index 100% rename from 06-Errors and Exception Handling/simple2.py rename to 07-Errors and Exception Handling/simple2.py diff --git a/06-Errors and Exception Handling/test_cap.py b/07-Errors and Exception Handling/test_cap.py similarity index 100% rename from 06-Errors and Exception Handling/test_cap.py rename to 07-Errors and Exception Handling/test_cap.py diff --git a/06-Errors and Exception Handling/testfile b/07-Errors and Exception Handling/testfile similarity index 100% rename from 06-Errors and Exception Handling/testfile rename to 07-Errors and Exception Handling/testfile diff --git a/07-Milestone Project - 2/.ipynb_checkpoints/04-Milestone Project 2 - Solution Code-checkpoint.ipynb b/07-Milestone Project - 2/.ipynb_checkpoints/04-Milestone Project 2 - Solution Code-checkpoint.ipynb deleted file mode 100644 index af44df772..000000000 --- a/07-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", - "