├── 00-Python Object and Data Structure Basics ├── .ipynb_checkpoints │ ├── 01-Numbers-checkpoint.ipynb │ ├── 01-Variable Assignment-checkpoint.ipynb │ ├── 02-Strings-checkpoint.ipynb │ ├── 03-Print Formatting with Strings-checkpoint.ipynb │ ├── 04-Lists-checkpoint.ipynb │ ├── 05-Dictionaries-checkpoint.ipynb │ ├── 06-Tuples-checkpoint.ipynb │ ├── 07-Sets and Booleans-checkpoint.ipynb │ ├── 08-Files-checkpoint.ipynb │ ├── 09-Objects and Data Structures Assessment Test-checkpoint.ipynb │ ├── 10-Objects and Data Structures Assessment Test-Solution-checkpoint.ipynb │ └── Untitled-checkpoint.ipynb ├── 01-Numbers.ipynb ├── 01-Variable Assignment.ipynb ├── 02-Strings.ipynb ├── 03-Print Formatting with Strings.ipynb ├── 04-Lists.ipynb ├── 05-Dictionaries.ipynb ├── 06-Tuples.ipynb ├── 07-Sets and Booleans.ipynb ├── 08-Files.ipynb ├── Assignment │ ├── 09-Objects and Data Structures Assessment Test.ipynb │ └── 10-Objects and Data Structures Assessment Test-Solution.ipynb └── test.txt ├── 01-Python Comparison Operators ├── .ipynb_checkpoints │ ├── 01-Comparison Operators-checkpoint.ipynb │ └── 02-Chained Comparison Operators-checkpoint.ipynb ├── 01-Comparison Operators.ipynb └── 02-Chained Comparison Operators.ipynb ├── 02-Python Statements ├── .ipynb_checkpoints │ ├── 01-Introduction to Python Statements-checkpoint.ipynb │ ├── 02-if, elif, and else Statements-checkpoint.ipynb │ ├── 03-for Loops-checkpoint.ipynb │ ├── 04-while Loops-checkpoint.ipynb │ ├── 05-Useful-Operators-checkpoint.ipynb │ ├── 05-range()-checkpoint.ipynb │ ├── 06-List Comprehensions-checkpoint.ipynb │ ├── 07-Statements Assessment Test-checkpoint.ipynb │ ├── 08-Statements Assessment Test - Solutions-checkpoint.ipynb │ ├── 09-Guessing Game Challenge-checkpoint.ipynb │ └── 10-Guessing Game Challenge - Solution-checkpoint.ipynb ├── 01-Introduction to Python Statements.ipynb ├── 02-if, elif, and else Statements.ipynb ├── 03-for Loops.ipynb ├── 04-while Loops.ipynb ├── 05-Useful-Operators.ipynb ├── 06-List Comprehensions.ipynb ├── 07-Statements Assessment Test.ipynb ├── 09-Guessing Game Challenge.ipynb └── Assignment │ ├── 07-Statements Assessment Test.ipynb │ ├── 08-Statements Assessment Test - Solutions.ipynb │ ├── 09-Guessing Game Challenge.ipynb │ └── 10-Guessing Game Challenge - Solution.ipynb ├── 03-Methods and Functions ├── .ipynb_checkpoints │ ├── 01-Methods-checkpoint.ipynb │ ├── 02-Functions-checkpoint.ipynb │ ├── 03-Function Practice Exercises-checkpoint.ipynb │ ├── 03-Lambda expressions-checkpoint.ipynb │ ├── 04-Function Practice Exercises - Solutions-checkpoint.ipynb │ ├── 04-Nested Statements and Scope-checkpoint.ipynb │ ├── 05-Functions and Methods Homework-checkpoint.ipynb │ ├── 05-Lambda-Expressions-Map-and-Filter-checkpoint.ipynb │ ├── 06-Functions and Methods Homework - Solutions-checkpoint.ipynb │ ├── 07-args and kwargs-checkpoint.ipynb │ ├── 08-Function Practice Exercises-checkpoint.ipynb │ ├── 08-Functions and Methods Homework-checkpoint.ipynb │ └── 09-Function Practice Exercises - Solutions-checkpoint.ipynb ├── 01-Methods.ipynb ├── 02-Functions.ipynb ├── 05-Lambda-Expressions-Map-and-Filter.ipynb ├── 06-Nested Statements and Scope.ipynb ├── 07-args and kwargs.ipynb └── Assignment │ ├── 03-Function Practice Exercises.ipynb │ └── 08-Functions and Methods Homework.ipynb ├── 04-Milestone Project - 1 ├── .ipynb_checkpoints │ ├── 01-Milestone Project 1 - Assignment-checkpoint.ipynb │ ├── 02-Milestone Project 1 - Walkthrough Steps Workbook-checkpoint.ipynb │ ├── 03-Milestone Project 1 - Complete Walkthrough Solution-checkpoint.ipynb │ └── 04-OPTIONAL -Milestone Project 1 - Advanced Solution-checkpoint.ipynb ├── 01-Milestone Project 1 - Assignment.ipynb └── 02-Milestone Project 1 - Walkthrough Steps Workbook.ipynb ├── 05-Object Oriented Programming ├── .ipynb_checkpoints │ ├── 01-Object Oriented Programming-Copy1-checkpoint.ipynb │ ├── 01-Object Oriented Programming-checkpoint.ipynb │ ├── 02-Object Oriented Programming Homework-checkpoint.ipynb │ ├── 03-Object Oriented Programming Homework - Solution-checkpoint.ipynb │ ├── 04-OOP Challenge-checkpoint.ipynb │ ├── 05-OOP Challenge - Solution-checkpoint.ipynb │ └── OOP_Introduction-checkpoint.ipynb ├── 01-Object Oriented Programming.ipynb ├── Assignment │ ├── 02-Object Oriented Programming Homework.ipynb │ └── 04-OOP Challenge.ipynb └── OOP_Introduction.ipynb └── README.md /00-Python Object and Data Structure Basics/.ipynb_checkpoints/06-Tuples-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Tuples\n", 8 | "\n", 9 | "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", 10 | "\n", 11 | "In this section, we will get a brief overview of the following:\n", 12 | "\n", 13 | " 1.) Constructing Tuples\n", 14 | " 2.) Basic Tuple Methods\n", 15 | " 3.) Immutability\n", 16 | " 4.) When to Use Tuples\n", 17 | "\n", 18 | "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", 19 | "\n", 20 | "## Constructing Tuples\n", 21 | "\n", 22 | "The construction of a tuples use () with elements separated by commas. For example:" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 1, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "# Create a tuple\n", 32 | "t = (1,2,3)" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 2, 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "data": { 42 | "text/plain": [ 43 | "3" 44 | ] 45 | }, 46 | "execution_count": 2, 47 | "metadata": {}, 48 | "output_type": "execute_result" 49 | } 50 | ], 51 | "source": [ 52 | "# Check len just like a list\n", 53 | "len(t)" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 3, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "data": { 63 | "text/plain": [ 64 | "('one', 2)" 65 | ] 66 | }, 67 | "execution_count": 3, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "# Can also mix object types\n", 74 | "t = ('one',2)\n", 75 | "\n", 76 | "# Show\n", 77 | "t" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 4, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "'one'" 89 | ] 90 | }, 91 | "execution_count": 4, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "source": [ 97 | "# Use indexing just like we did in lists\n", 98 | "t[0]" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 5, 104 | "metadata": {}, 105 | "outputs": [ 106 | { 107 | "data": { 108 | "text/plain": [ 109 | "2" 110 | ] 111 | }, 112 | "execution_count": 5, 113 | "metadata": {}, 114 | "output_type": "execute_result" 115 | } 116 | ], 117 | "source": [ 118 | "# Slicing just like a list\n", 119 | "t[-1]" 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": {}, 125 | "source": [ 126 | "## Basic Tuple Methods\n", 127 | "\n", 128 | "Tuples have built-in methods, but not as many as lists do. Let's look at two of them:" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 6, 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "data": { 138 | "text/plain": [ 139 | "0" 140 | ] 141 | }, 142 | "execution_count": 6, 143 | "metadata": {}, 144 | "output_type": "execute_result" 145 | } 146 | ], 147 | "source": [ 148 | "# Use .index to enter a value and return the index\n", 149 | "t.index('one')" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 7, 155 | "metadata": {}, 156 | "outputs": [ 157 | { 158 | "data": { 159 | "text/plain": [ 160 | "1" 161 | ] 162 | }, 163 | "execution_count": 7, 164 | "metadata": {}, 165 | "output_type": "execute_result" 166 | } 167 | ], 168 | "source": [ 169 | "# Use .count to count the number of times a value appears\n", 170 | "t.count('one')" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": {}, 176 | "source": [ 177 | "## Immutability\n", 178 | "\n", 179 | "It can't be stressed enough that tuples are immutable. To drive that point home:" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 8, 185 | "metadata": {}, 186 | "outputs": [ 187 | { 188 | "ename": "TypeError", 189 | "evalue": "'tuple' object does not support item assignment", 190 | "output_type": "error", 191 | "traceback": [ 192 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 193 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 194 | "\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", 195 | "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" 196 | ] 197 | } 198 | ], 199 | "source": [ 200 | "t[0]= 'change'" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "Because of this immutability, tuples can't grow. Once a tuple is made we can not add to it." 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 9, 213 | "metadata": {}, 214 | "outputs": [ 215 | { 216 | "ename": "AttributeError", 217 | "evalue": "'tuple' object has no attribute 'append'", 218 | "output_type": "error", 219 | "traceback": [ 220 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 221 | "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", 222 | "\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", 223 | "\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" 224 | ] 225 | } 226 | ], 227 | "source": [ 228 | "t.append('nope')" 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "metadata": {}, 234 | "source": [ 235 | "## When to use Tuples\n", 236 | "\n", 237 | "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", 238 | "\n", 239 | "You should now be able to create and use tuples in your programming as well as have an understanding of their immutability.\n", 240 | "\n", 241 | "Up next Sets and Booleans!!" 242 | ] 243 | } 244 | ], 245 | "metadata": { 246 | "kernelspec": { 247 | "display_name": "Python 3", 248 | "language": "python", 249 | "name": "python3" 250 | }, 251 | "language_info": { 252 | "codemirror_mode": { 253 | "name": "ipython", 254 | "version": 3 255 | }, 256 | "file_extension": ".py", 257 | "mimetype": "text/x-python", 258 | "name": "python", 259 | "nbconvert_exporter": "python", 260 | "pygments_lexer": "ipython3", 261 | "version": "3.6.2" 262 | } 263 | }, 264 | "nbformat": 4, 265 | "nbformat_minor": 1 266 | } 267 | -------------------------------------------------------------------------------- /00-Python Object and Data Structure Basics/.ipynb_checkpoints/07-Sets and Booleans-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Set and Booleans\n", 8 | "\n", 9 | "There are two other object types in Python that we should quickly cover: Sets and Booleans. \n", 10 | "\n", 11 | "## Sets\n", 12 | "\n", 13 | "Sets are an unordered collection of *unique* elements. We can construct them by using the set() function. Let's go ahead and make a set to see how it works" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": { 20 | "collapsed": true 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "x = set()" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 2, 30 | "metadata": { 31 | "collapsed": true 32 | }, 33 | "outputs": [], 34 | "source": [ 35 | "# We add to sets with the add() method\n", 36 | "x.add(1)" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 3, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "{1}" 48 | ] 49 | }, 50 | "execution_count": 3, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "#Show\n", 57 | "x" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "Note the curly brackets. This does not indicate a dictionary! Although you can draw analogies as a set being a dictionary with only keys.\n", 65 | "\n", 66 | "We know that a set has only unique entries. So what happens when we try to add something that is already in a set?" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 4, 72 | "metadata": { 73 | "collapsed": true 74 | }, 75 | "outputs": [], 76 | "source": [ 77 | "# Add a different element\n", 78 | "x.add(2)" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 5, 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "data": { 88 | "text/plain": [ 89 | "{1, 2}" 90 | ] 91 | }, 92 | "execution_count": 5, 93 | "metadata": {}, 94 | "output_type": "execute_result" 95 | } 96 | ], 97 | "source": [ 98 | "#Show\n", 99 | "x" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 6, 105 | "metadata": { 106 | "collapsed": true 107 | }, 108 | "outputs": [], 109 | "source": [ 110 | "# Try to add the same element\n", 111 | "x.add(1)" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 7, 117 | "metadata": {}, 118 | "outputs": [ 119 | { 120 | "data": { 121 | "text/plain": [ 122 | "{1, 2}" 123 | ] 124 | }, 125 | "execution_count": 7, 126 | "metadata": {}, 127 | "output_type": "execute_result" 128 | } 129 | ], 130 | "source": [ 131 | "#Show\n", 132 | "x" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "Notice how it won't place another 1 there. That's because a set is only concerned with unique elements! We can cast a list with multiple repeat elements to a set to get the unique elements. For example:" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 8, 145 | "metadata": { 146 | "collapsed": true 147 | }, 148 | "outputs": [], 149 | "source": [ 150 | "# Create a list with repeats\n", 151 | "list1 = [1,1,2,2,3,4,5,6,1,1]" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 9, 157 | "metadata": {}, 158 | "outputs": [ 159 | { 160 | "data": { 161 | "text/plain": [ 162 | "{1, 2, 3, 4, 5, 6}" 163 | ] 164 | }, 165 | "execution_count": 9, 166 | "metadata": {}, 167 | "output_type": "execute_result" 168 | } 169 | ], 170 | "source": [ 171 | "# Cast as set to get unique values\n", 172 | "set(list1)" 173 | ] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "## Booleans\n", 180 | "\n", 181 | "Python comes with Booleans (with predefined True and False displays that are basically just the integers 1 and 0). It also has a placeholder object called None. Let's walk through a few quick examples of Booleans (we will dive deeper into them later in this course)." 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 10, 187 | "metadata": { 188 | "collapsed": true 189 | }, 190 | "outputs": [], 191 | "source": [ 192 | "# Set object to be a boolean\n", 193 | "a = True" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 11, 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "data": { 203 | "text/plain": [ 204 | "True" 205 | ] 206 | }, 207 | "execution_count": 11, 208 | "metadata": {}, 209 | "output_type": "execute_result" 210 | } 211 | ], 212 | "source": [ 213 | "#Show\n", 214 | "a" 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "We can also use comparison operators to create booleans. We will go over all the comparison operators later on in the course." 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 12, 227 | "metadata": {}, 228 | "outputs": [ 229 | { 230 | "data": { 231 | "text/plain": [ 232 | "False" 233 | ] 234 | }, 235 | "execution_count": 12, 236 | "metadata": {}, 237 | "output_type": "execute_result" 238 | } 239 | ], 240 | "source": [ 241 | "# Output is boolean\n", 242 | "1 > 2" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "We can use None as a placeholder for an object that we don't want to reassign yet:" 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": 13, 255 | "metadata": { 256 | "collapsed": true 257 | }, 258 | "outputs": [], 259 | "source": [ 260 | "# None placeholder\n", 261 | "b = None" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": 14, 267 | "metadata": {}, 268 | "outputs": [ 269 | { 270 | "name": "stdout", 271 | "output_type": "stream", 272 | "text": [ 273 | "None\n" 274 | ] 275 | } 276 | ], 277 | "source": [ 278 | "# Show\n", 279 | "print(b)" 280 | ] 281 | }, 282 | { 283 | "cell_type": "markdown", 284 | "metadata": {}, 285 | "source": [ 286 | "Thats it! You should now have a basic understanding of Python objects and data structure types. Next, go ahead and do the assessment test!" 287 | ] 288 | } 289 | ], 290 | "metadata": { 291 | "kernelspec": { 292 | "display_name": "Python 3", 293 | "language": "python", 294 | "name": "python3" 295 | }, 296 | "language_info": { 297 | "codemirror_mode": { 298 | "name": "ipython", 299 | "version": 3 300 | }, 301 | "file_extension": ".py", 302 | "mimetype": "text/x-python", 303 | "name": "python", 304 | "nbconvert_exporter": "python", 305 | "pygments_lexer": "ipython3", 306 | "version": "3.6.1" 307 | } 308 | }, 309 | "nbformat": 4, 310 | "nbformat_minor": 1 311 | } 312 | -------------------------------------------------------------------------------- /00-Python Object and Data Structure Basics/.ipynb_checkpoints/Untitled-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /00-Python Object and Data Structure Basics/06-Tuples.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Tuples\n", 8 | "\n", 9 | "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", 10 | "\n", 11 | "In this section, we will get a brief overview of the following:\n", 12 | "\n", 13 | " 1.) Constructing Tuples\n", 14 | " 2.) Basic Tuple Methods\n", 15 | " 3.) Immutability\n", 16 | " 4.) When to Use Tuples\n", 17 | "\n", 18 | "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", 19 | "\n", 20 | "## Constructing Tuples\n", 21 | "\n", 22 | "The construction of a tuples use () with elements separated by commas. For example:" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 1, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "# Create a tuple\n", 32 | "t = (1,2,3)" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 2, 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "data": { 42 | "text/plain": [ 43 | "3" 44 | ] 45 | }, 46 | "execution_count": 2, 47 | "metadata": {}, 48 | "output_type": "execute_result" 49 | } 50 | ], 51 | "source": [ 52 | "# Check len just like a list\n", 53 | "len(t)" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 3, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "data": { 63 | "text/plain": [ 64 | "('one', 2)" 65 | ] 66 | }, 67 | "execution_count": 3, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "# Can also mix object types\n", 74 | "t = ('one',2)\n", 75 | "\n", 76 | "# Show\n", 77 | "t" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 4, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "'one'" 89 | ] 90 | }, 91 | "execution_count": 4, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "source": [ 97 | "# Use indexing just like we did in lists\n", 98 | "t[0]" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 5, 104 | "metadata": {}, 105 | "outputs": [ 106 | { 107 | "data": { 108 | "text/plain": [ 109 | "2" 110 | ] 111 | }, 112 | "execution_count": 5, 113 | "metadata": {}, 114 | "output_type": "execute_result" 115 | } 116 | ], 117 | "source": [ 118 | "# Slicing just like a list\n", 119 | "t[-1]" 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": {}, 125 | "source": [ 126 | "## Basic Tuple Methods\n", 127 | "\n", 128 | "Tuples have built-in methods, but not as many as lists do. Let's look at two of them:" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 6, 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "data": { 138 | "text/plain": [ 139 | "0" 140 | ] 141 | }, 142 | "execution_count": 6, 143 | "metadata": {}, 144 | "output_type": "execute_result" 145 | } 146 | ], 147 | "source": [ 148 | "# Use .index to enter a value and return the index\n", 149 | "t.index('one')" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 7, 155 | "metadata": {}, 156 | "outputs": [ 157 | { 158 | "data": { 159 | "text/plain": [ 160 | "1" 161 | ] 162 | }, 163 | "execution_count": 7, 164 | "metadata": {}, 165 | "output_type": "execute_result" 166 | } 167 | ], 168 | "source": [ 169 | "# Use .count to count the number of times a value appears\n", 170 | "t.count('one')" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": {}, 176 | "source": [ 177 | "## Immutability\n", 178 | "\n", 179 | "It can't be stressed enough that tuples are immutable. To drive that point home:" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 8, 185 | "metadata": {}, 186 | "outputs": [ 187 | { 188 | "ename": "TypeError", 189 | "evalue": "'tuple' object does not support item assignment", 190 | "output_type": "error", 191 | "traceback": [ 192 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 193 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 194 | "\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", 195 | "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" 196 | ] 197 | } 198 | ], 199 | "source": [ 200 | "t[0]= 'change'" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "Because of this immutability, tuples can't grow. Once a tuple is made we can not add to it." 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 9, 213 | "metadata": {}, 214 | "outputs": [ 215 | { 216 | "ename": "AttributeError", 217 | "evalue": "'tuple' object has no attribute 'append'", 218 | "output_type": "error", 219 | "traceback": [ 220 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 221 | "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", 222 | "\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", 223 | "\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" 224 | ] 225 | } 226 | ], 227 | "source": [ 228 | "t.append('nope')" 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "metadata": {}, 234 | "source": [ 235 | "## When to use Tuples\n", 236 | "\n", 237 | "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", 238 | "\n", 239 | "You should now be able to create and use tuples in your programming as well as have an understanding of their immutability.\n", 240 | "\n", 241 | "Up next Sets and Booleans!!" 242 | ] 243 | } 244 | ], 245 | "metadata": { 246 | "kernelspec": { 247 | "display_name": "Python 3", 248 | "language": "python", 249 | "name": "python3" 250 | }, 251 | "language_info": { 252 | "codemirror_mode": { 253 | "name": "ipython", 254 | "version": 2 255 | }, 256 | "file_extension": ".py", 257 | "mimetype": "text/x-python", 258 | "name": "python", 259 | "nbconvert_exporter": "python", 260 | "pygments_lexer": "ipython2", 261 | "version": "2.7.12" 262 | } 263 | }, 264 | "nbformat": 4, 265 | "nbformat_minor": 2 266 | } 267 | -------------------------------------------------------------------------------- /00-Python Object and Data Structure Basics/07-Sets and Booleans.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Set and Booleans\n", 8 | "\n", 9 | "There are two other object types in Python that we should quickly cover: Sets and Booleans. \n", 10 | "\n", 11 | "## Sets\n", 12 | "\n", 13 | "Sets are an unordered collection of *unique* elements. We can construct them by using the set() function. Let's go ahead and make a set to see how it works" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": { 20 | "collapsed": true 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "x = set()" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 2, 30 | "metadata": { 31 | "collapsed": true 32 | }, 33 | "outputs": [], 34 | "source": [ 35 | "# We add to sets with the add() method\n", 36 | "x.add(1)" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 3, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "{1}" 48 | ] 49 | }, 50 | "execution_count": 3, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "#Show\n", 57 | "x" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "Note the curly brackets. This does not indicate a dictionary! Although you can draw analogies as a set being a dictionary with only keys.\n", 65 | "\n", 66 | "We know that a set has only unique entries. So what happens when we try to add something that is already in a set?" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 4, 72 | "metadata": { 73 | "collapsed": true 74 | }, 75 | "outputs": [], 76 | "source": [ 77 | "# Add a different element\n", 78 | "x.add(2)" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 5, 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "data": { 88 | "text/plain": [ 89 | "{1, 2}" 90 | ] 91 | }, 92 | "execution_count": 5, 93 | "metadata": {}, 94 | "output_type": "execute_result" 95 | } 96 | ], 97 | "source": [ 98 | "#Show\n", 99 | "x" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 6, 105 | "metadata": { 106 | "collapsed": true 107 | }, 108 | "outputs": [], 109 | "source": [ 110 | "# Try to add the same element\n", 111 | "x.add(1)" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 7, 117 | "metadata": {}, 118 | "outputs": [ 119 | { 120 | "data": { 121 | "text/plain": [ 122 | "{1, 2}" 123 | ] 124 | }, 125 | "execution_count": 7, 126 | "metadata": {}, 127 | "output_type": "execute_result" 128 | } 129 | ], 130 | "source": [ 131 | "#Show\n", 132 | "x" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "Notice how it won't place another 1 there. That's because a set is only concerned with unique elements! We can cast a list with multiple repeat elements to a set to get the unique elements. For example:" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 8, 145 | "metadata": { 146 | "collapsed": true 147 | }, 148 | "outputs": [], 149 | "source": [ 150 | "# Create a list with repeats\n", 151 | "list1 = [1,1,2,2,3,4,5,6,1,1]" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 9, 157 | "metadata": {}, 158 | "outputs": [ 159 | { 160 | "data": { 161 | "text/plain": [ 162 | "{1, 2, 3, 4, 5, 6}" 163 | ] 164 | }, 165 | "execution_count": 9, 166 | "metadata": {}, 167 | "output_type": "execute_result" 168 | } 169 | ], 170 | "source": [ 171 | "# Cast as set to get unique values\n", 172 | "set(list1)" 173 | ] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "## Booleans\n", 180 | "\n", 181 | "Python comes with Booleans (with predefined True and False displays that are basically just the integers 1 and 0). It also has a placeholder object called None. Let's walk through a few quick examples of Booleans (we will dive deeper into them later in this course)." 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 10, 187 | "metadata": { 188 | "collapsed": true 189 | }, 190 | "outputs": [], 191 | "source": [ 192 | "# Set object to be a boolean\n", 193 | "a = True" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 11, 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "data": { 203 | "text/plain": [ 204 | "True" 205 | ] 206 | }, 207 | "execution_count": 11, 208 | "metadata": {}, 209 | "output_type": "execute_result" 210 | } 211 | ], 212 | "source": [ 213 | "#Show\n", 214 | "a" 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "We can also use comparison operators to create booleans. We will go over all the comparison operators later on in the course." 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 12, 227 | "metadata": {}, 228 | "outputs": [ 229 | { 230 | "data": { 231 | "text/plain": [ 232 | "False" 233 | ] 234 | }, 235 | "execution_count": 12, 236 | "metadata": {}, 237 | "output_type": "execute_result" 238 | } 239 | ], 240 | "source": [ 241 | "# Output is boolean\n", 242 | "1 > 2" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "We can use None as a placeholder for an object that we don't want to reassign yet:" 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": 13, 255 | "metadata": { 256 | "collapsed": true 257 | }, 258 | "outputs": [], 259 | "source": [ 260 | "# None placeholder\n", 261 | "b = None" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": 14, 267 | "metadata": {}, 268 | "outputs": [ 269 | { 270 | "name": "stdout", 271 | "output_type": "stream", 272 | "text": [ 273 | "None\n" 274 | ] 275 | } 276 | ], 277 | "source": [ 278 | "# Show\n", 279 | "print(b)" 280 | ] 281 | }, 282 | { 283 | "cell_type": "markdown", 284 | "metadata": {}, 285 | "source": [ 286 | "Thats it! You should now have a basic understanding of Python objects and data structure types. Next, go ahead and do the assessment test!" 287 | ] 288 | } 289 | ], 290 | "metadata": { 291 | "kernelspec": { 292 | "display_name": "Python 3", 293 | "language": "python", 294 | "name": "python3" 295 | }, 296 | "language_info": { 297 | "codemirror_mode": { 298 | "name": "ipython", 299 | "version": 2 300 | }, 301 | "file_extension": ".py", 302 | "mimetype": "text/x-python", 303 | "name": "python", 304 | "nbconvert_exporter": "python", 305 | "pygments_lexer": "ipython2", 306 | "version": "2.7.12" 307 | } 308 | }, 309 | "nbformat": 4, 310 | "nbformat_minor": 2 311 | } 312 | -------------------------------------------------------------------------------- /00-Python Object and Data Structure Basics/test.txt: -------------------------------------------------------------------------------- 1 | First Line 2 | Second Line -------------------------------------------------------------------------------- /01-Python Comparison Operators/.ipynb_checkpoints/01-Comparison Operators-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Comparison Operators \n", 8 | "\n", 9 | "In this lecture we will be learning about Comparison Operators in Python. These operators will allow us to compare variables and output a Boolean value (True or False). \n", 10 | "\n", 11 | "If you have any sort of background in Math, these operators should be very straight forward.\n", 12 | "\n", 13 | "First we'll present a table of the comparison operators and then work through some examples:\n", 14 | "\n", 15 | "

Table of Comparison Operators

In the table below, a=3 and b=4.

\n", 16 | "\n", 17 | "\n", 18 | "\n", 19 | "\n", 20 | "\n", 21 | "\n", 22 | "\n", 23 | "\n", 24 | "\n", 25 | "\n", 26 | "\n", 27 | "\n", 28 | "\n", 29 | "\n", 30 | "\n", 31 | "\n", 32 | "\n", 33 | "\n", 34 | "\n", 35 | "\n", 36 | "\n", 37 | "\n", 38 | "\n", 39 | "\n", 40 | "\n", 41 | "\n", 42 | "\n", 43 | "\n", 44 | "\n", 45 | "\n", 46 | "\n", 47 | "\n", 48 | "\n", 49 | "\n", 50 | "\n", 51 | "
OperatorDescriptionExample
==If the values of two operands are equal, then the condition becomes true. (a == b) is not true.
!=If values of two operands are not equal, then condition becomes true.(a != b) is true
>If the value of left operand is greater than the value of right operand, then condition becomes true. (a > b) is not true.
<If the value of left operand is less than the value of right operand, then condition becomes true. (a < b) is true.
>=If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. (a >= b) is not true.
<=If the value of left operand is less than or equal to the value of right operand, then condition becomes true. (a <= b) is true.
" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "Let's now work through quick examples of each of these.\n", 59 | "\n", 60 | "#### Equal" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 1, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "data": { 70 | "text/plain": [ 71 | "True" 72 | ] 73 | }, 74 | "execution_count": 1, 75 | "metadata": {}, 76 | "output_type": "execute_result" 77 | } 78 | ], 79 | "source": [ 80 | "2 == 2" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 2, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "data": { 90 | "text/plain": [ 91 | "False" 92 | ] 93 | }, 94 | "execution_count": 2, 95 | "metadata": {}, 96 | "output_type": "execute_result" 97 | } 98 | ], 99 | "source": [ 100 | "1 == 0" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "Note that == is a comparison operator, while = is an assignment operator." 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "#### Not Equal" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 3, 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "data": { 124 | "text/plain": [ 125 | "True" 126 | ] 127 | }, 128 | "execution_count": 3, 129 | "metadata": {}, 130 | "output_type": "execute_result" 131 | } 132 | ], 133 | "source": [ 134 | "2 != 1" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 4, 140 | "metadata": {}, 141 | "outputs": [ 142 | { 143 | "data": { 144 | "text/plain": [ 145 | "False" 146 | ] 147 | }, 148 | "execution_count": 4, 149 | "metadata": {}, 150 | "output_type": "execute_result" 151 | } 152 | ], 153 | "source": [ 154 | "2 != 2" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "#### Greater Than" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 5, 167 | "metadata": {}, 168 | "outputs": [ 169 | { 170 | "data": { 171 | "text/plain": [ 172 | "True" 173 | ] 174 | }, 175 | "execution_count": 5, 176 | "metadata": {}, 177 | "output_type": "execute_result" 178 | } 179 | ], 180 | "source": [ 181 | "2 > 1" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 6, 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "data": { 191 | "text/plain": [ 192 | "False" 193 | ] 194 | }, 195 | "execution_count": 6, 196 | "metadata": {}, 197 | "output_type": "execute_result" 198 | } 199 | ], 200 | "source": [ 201 | "2 > 4" 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": {}, 207 | "source": [ 208 | "#### Less Than" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 7, 214 | "metadata": {}, 215 | "outputs": [ 216 | { 217 | "data": { 218 | "text/plain": [ 219 | "True" 220 | ] 221 | }, 222 | "execution_count": 7, 223 | "metadata": {}, 224 | "output_type": "execute_result" 225 | } 226 | ], 227 | "source": [ 228 | "2 < 4" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 8, 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "data": { 238 | "text/plain": [ 239 | "False" 240 | ] 241 | }, 242 | "execution_count": 8, 243 | "metadata": {}, 244 | "output_type": "execute_result" 245 | } 246 | ], 247 | "source": [ 248 | "2 < 1" 249 | ] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "metadata": {}, 254 | "source": [ 255 | "#### Greater Than or Equal to" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 9, 261 | "metadata": {}, 262 | "outputs": [ 263 | { 264 | "data": { 265 | "text/plain": [ 266 | "True" 267 | ] 268 | }, 269 | "execution_count": 9, 270 | "metadata": {}, 271 | "output_type": "execute_result" 272 | } 273 | ], 274 | "source": [ 275 | "2 >= 2" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": 10, 281 | "metadata": {}, 282 | "outputs": [ 283 | { 284 | "data": { 285 | "text/plain": [ 286 | "True" 287 | ] 288 | }, 289 | "execution_count": 10, 290 | "metadata": {}, 291 | "output_type": "execute_result" 292 | } 293 | ], 294 | "source": [ 295 | "2 >= 1" 296 | ] 297 | }, 298 | { 299 | "cell_type": "markdown", 300 | "metadata": {}, 301 | "source": [ 302 | "#### Less than or Equal to" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": 11, 308 | "metadata": {}, 309 | "outputs": [ 310 | { 311 | "data": { 312 | "text/plain": [ 313 | "True" 314 | ] 315 | }, 316 | "execution_count": 11, 317 | "metadata": {}, 318 | "output_type": "execute_result" 319 | } 320 | ], 321 | "source": [ 322 | "2 <= 2" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": 12, 328 | "metadata": {}, 329 | "outputs": [ 330 | { 331 | "data": { 332 | "text/plain": [ 333 | "True" 334 | ] 335 | }, 336 | "execution_count": 12, 337 | "metadata": {}, 338 | "output_type": "execute_result" 339 | } 340 | ], 341 | "source": [ 342 | "2 <= 4" 343 | ] 344 | }, 345 | { 346 | "cell_type": "markdown", 347 | "metadata": {}, 348 | "source": [ 349 | "**Great! Go over each comparison operator to make sure you understand what each one is saying. But hopefully this was straightforward for you.**\n", 350 | "\n", 351 | "Next we will cover chained comparison operators" 352 | ] 353 | } 354 | ], 355 | "metadata": { 356 | "kernelspec": { 357 | "display_name": "Python 3", 358 | "language": "python", 359 | "name": "python3" 360 | }, 361 | "language_info": { 362 | "codemirror_mode": { 363 | "name": "ipython", 364 | "version": 3 365 | }, 366 | "file_extension": ".py", 367 | "mimetype": "text/x-python", 368 | "name": "python", 369 | "nbconvert_exporter": "python", 370 | "pygments_lexer": "ipython3", 371 | "version": "3.6.2" 372 | } 373 | }, 374 | "nbformat": 4, 375 | "nbformat_minor": 1 376 | } 377 | -------------------------------------------------------------------------------- /01-Python Comparison Operators/.ipynb_checkpoints/02-Chained Comparison Operators-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Chained Comparison Operators\n", 8 | "\n", 9 | "An interesting feature of Python is the ability to *chain* multiple comparisons to perform a more complex test. You can use these chained comparisons as shorthand for larger Boolean Expressions.\n", 10 | "\n", 11 | "In this lecture we will learn how to chain comparison operators and we will also introduce two other important statements in Python: **and** and **or**.\n", 12 | "\n", 13 | "Let's look at a few examples of using chains:" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "data": { 23 | "text/plain": [ 24 | "True" 25 | ] 26 | }, 27 | "execution_count": 1, 28 | "metadata": {}, 29 | "output_type": "execute_result" 30 | } 31 | ], 32 | "source": [ 33 | "1 < 2 < 3" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "The above statement checks if 1 was less than 2 **and** if 2 was less than 3. We could have written this using an **and** statement in Python:" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 2, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "data": { 50 | "text/plain": [ 51 | "True" 52 | ] 53 | }, 54 | "execution_count": 2, 55 | "metadata": {}, 56 | "output_type": "execute_result" 57 | } 58 | ], 59 | "source": [ 60 | "1<2 and 2<3" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "The **and** is used to make sure two checks have to be true in order for the total check to be true. Let's see another example:" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 3, 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "data": { 77 | "text/plain": [ 78 | "True" 79 | ] 80 | }, 81 | "execution_count": 3, 82 | "metadata": {}, 83 | "output_type": "execute_result" 84 | } 85 | ], 86 | "source": [ 87 | "1 < 3 > 2" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "The above checks if 3 is larger than both of the other numbers, so you could use **and** to rewrite it as:" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 4, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "True" 106 | ] 107 | }, 108 | "execution_count": 4, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "1<3 and 3>2" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "It's important to note that Python is checking both instances of the comparisons. We can also use **or** to write comparisons in Python. For example:" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 5, 127 | "metadata": {}, 128 | "outputs": [ 129 | { 130 | "data": { 131 | "text/plain": [ 132 | "True" 133 | ] 134 | }, 135 | "execution_count": 5, 136 | "metadata": {}, 137 | "output_type": "execute_result" 138 | } 139 | ], 140 | "source": [ 141 | "1==2 or 2<3" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "Note how it was true; this is because with the **or** operator, we only need one *or* the other to be true. Let's see one more example to drive this home:" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 6, 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "data": { 158 | "text/plain": [ 159 | "True" 160 | ] 161 | }, 162 | "execution_count": 6, 163 | "metadata": {}, 164 | "output_type": "execute_result" 165 | } 166 | ], 167 | "source": [ 168 | "1==1 or 100==1" 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": {}, 174 | "source": [ 175 | "Great! For an overview of this quick lesson: You should have a comfortable understanding of using **and** and **or** statements as well as reading chained comparison code.\n", 176 | "\n", 177 | "Go ahead and go to the quiz for this section to check your understanding!" 178 | ] 179 | } 180 | ], 181 | "metadata": { 182 | "kernelspec": { 183 | "display_name": "Python 3", 184 | "language": "python", 185 | "name": "python3" 186 | }, 187 | "language_info": { 188 | "codemirror_mode": { 189 | "name": "ipython", 190 | "version": 3 191 | }, 192 | "file_extension": ".py", 193 | "mimetype": "text/x-python", 194 | "name": "python", 195 | "nbconvert_exporter": "python", 196 | "pygments_lexer": "ipython3", 197 | "version": "3.6.2" 198 | } 199 | }, 200 | "nbformat": 4, 201 | "nbformat_minor": 1 202 | } 203 | -------------------------------------------------------------------------------- /01-Python Comparison Operators/01-Comparison Operators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Comparison Operators \n", 8 | "\n", 9 | "In this lecture we will be learning about Comparison Operators in Python. These operators will allow us to compare variables and output a Boolean value (True or False). \n", 10 | "\n", 11 | "If you have any sort of background in Math, these operators should be very straight forward.\n", 12 | "\n", 13 | "First we'll present a table of the comparison operators and then work through some examples:\n", 14 | "\n", 15 | "

Table of Comparison Operators

In the table below, a=3 and b=4.

\n", 16 | "\n", 17 | "\n", 18 | "\n", 19 | "\n", 20 | "\n", 21 | "\n", 22 | "\n", 23 | "\n", 24 | "\n", 25 | "\n", 26 | "\n", 27 | "\n", 28 | "\n", 29 | "\n", 30 | "\n", 31 | "\n", 32 | "\n", 33 | "\n", 34 | "\n", 35 | "\n", 36 | "\n", 37 | "\n", 38 | "\n", 39 | "\n", 40 | "\n", 41 | "\n", 42 | "\n", 43 | "\n", 44 | "\n", 45 | "\n", 46 | "\n", 47 | "\n", 48 | "\n", 49 | "\n", 50 | "\n", 51 | "
OperatorDescriptionExample
==If the values of two operands are equal, then the condition becomes true. (a == b) is not true.
!=If values of two operands are not equal, then condition becomes true.(a != b) is true
>If the value of left operand is greater than the value of right operand, then condition becomes true. (a > b) is not true.
<If the value of left operand is less than the value of right operand, then condition becomes true. (a < b) is true.
>=If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. (a >= b) is not true.
<=If the value of left operand is less than or equal to the value of right operand, then condition becomes true. (a <= b) is true.
" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "Let's now work through quick examples of each of these.\n", 59 | "\n", 60 | "#### Equal" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 1, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "data": { 70 | "text/plain": [ 71 | "True" 72 | ] 73 | }, 74 | "execution_count": 1, 75 | "metadata": {}, 76 | "output_type": "execute_result" 77 | } 78 | ], 79 | "source": [ 80 | "2 == 2" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 2, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "data": { 90 | "text/plain": [ 91 | "False" 92 | ] 93 | }, 94 | "execution_count": 2, 95 | "metadata": {}, 96 | "output_type": "execute_result" 97 | } 98 | ], 99 | "source": [ 100 | "1 == 0" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "Note that == is a comparison operator, while = is an assignment operator." 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "#### Not Equal" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 3, 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "data": { 124 | "text/plain": [ 125 | "True" 126 | ] 127 | }, 128 | "execution_count": 3, 129 | "metadata": {}, 130 | "output_type": "execute_result" 131 | } 132 | ], 133 | "source": [ 134 | "2 != 1" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 4, 140 | "metadata": {}, 141 | "outputs": [ 142 | { 143 | "data": { 144 | "text/plain": [ 145 | "False" 146 | ] 147 | }, 148 | "execution_count": 4, 149 | "metadata": {}, 150 | "output_type": "execute_result" 151 | } 152 | ], 153 | "source": [ 154 | "2 != 2" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "#### Greater Than" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 5, 167 | "metadata": {}, 168 | "outputs": [ 169 | { 170 | "data": { 171 | "text/plain": [ 172 | "True" 173 | ] 174 | }, 175 | "execution_count": 5, 176 | "metadata": {}, 177 | "output_type": "execute_result" 178 | } 179 | ], 180 | "source": [ 181 | "2 > 1" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 6, 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "data": { 191 | "text/plain": [ 192 | "False" 193 | ] 194 | }, 195 | "execution_count": 6, 196 | "metadata": {}, 197 | "output_type": "execute_result" 198 | } 199 | ], 200 | "source": [ 201 | "2 > 4" 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": {}, 207 | "source": [ 208 | "#### Less Than" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 7, 214 | "metadata": {}, 215 | "outputs": [ 216 | { 217 | "data": { 218 | "text/plain": [ 219 | "True" 220 | ] 221 | }, 222 | "execution_count": 7, 223 | "metadata": {}, 224 | "output_type": "execute_result" 225 | } 226 | ], 227 | "source": [ 228 | "2 < 4" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 8, 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "data": { 238 | "text/plain": [ 239 | "False" 240 | ] 241 | }, 242 | "execution_count": 8, 243 | "metadata": {}, 244 | "output_type": "execute_result" 245 | } 246 | ], 247 | "source": [ 248 | "2 < 1" 249 | ] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "metadata": {}, 254 | "source": [ 255 | "#### Greater Than or Equal to" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 9, 261 | "metadata": {}, 262 | "outputs": [ 263 | { 264 | "data": { 265 | "text/plain": [ 266 | "True" 267 | ] 268 | }, 269 | "execution_count": 9, 270 | "metadata": {}, 271 | "output_type": "execute_result" 272 | } 273 | ], 274 | "source": [ 275 | "2 >= 2" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": 10, 281 | "metadata": {}, 282 | "outputs": [ 283 | { 284 | "data": { 285 | "text/plain": [ 286 | "True" 287 | ] 288 | }, 289 | "execution_count": 10, 290 | "metadata": {}, 291 | "output_type": "execute_result" 292 | } 293 | ], 294 | "source": [ 295 | "2 >= 1" 296 | ] 297 | }, 298 | { 299 | "cell_type": "markdown", 300 | "metadata": {}, 301 | "source": [ 302 | "#### Less than or Equal to" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": 11, 308 | "metadata": {}, 309 | "outputs": [ 310 | { 311 | "data": { 312 | "text/plain": [ 313 | "True" 314 | ] 315 | }, 316 | "execution_count": 11, 317 | "metadata": {}, 318 | "output_type": "execute_result" 319 | } 320 | ], 321 | "source": [ 322 | "2 <= 2" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": 12, 328 | "metadata": {}, 329 | "outputs": [ 330 | { 331 | "data": { 332 | "text/plain": [ 333 | "True" 334 | ] 335 | }, 336 | "execution_count": 12, 337 | "metadata": {}, 338 | "output_type": "execute_result" 339 | } 340 | ], 341 | "source": [ 342 | "2 <= 4" 343 | ] 344 | }, 345 | { 346 | "cell_type": "markdown", 347 | "metadata": {}, 348 | "source": [ 349 | "**Great! Go over each comparison operator to make sure you understand what each one is saying. But hopefully this was straightforward for you.**\n", 350 | "\n", 351 | "Next we will cover chained comparison operators" 352 | ] 353 | } 354 | ], 355 | "metadata": { 356 | "kernelspec": { 357 | "display_name": "Python 3", 358 | "language": "python", 359 | "name": "python3" 360 | }, 361 | "language_info": { 362 | "codemirror_mode": { 363 | "name": "ipython", 364 | "version": 2 365 | }, 366 | "file_extension": ".py", 367 | "mimetype": "text/x-python", 368 | "name": "python", 369 | "nbconvert_exporter": "python", 370 | "pygments_lexer": "ipython2", 371 | "version": "2.7.12" 372 | } 373 | }, 374 | "nbformat": 4, 375 | "nbformat_minor": 2 376 | } 377 | -------------------------------------------------------------------------------- /01-Python Comparison Operators/02-Chained Comparison Operators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Chained Comparison Operators\n", 8 | "\n", 9 | "An interesting feature of Python is the ability to *chain* multiple comparisons to perform a more complex test. You can use these chained comparisons as shorthand for larger Boolean Expressions.\n", 10 | "\n", 11 | "In this lecture we will learn how to chain comparison operators and we will also introduce two other important statements in Python: **and** and **or**.\n", 12 | "\n", 13 | "Let's look at a few examples of using chains:" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "data": { 23 | "text/plain": [ 24 | "True" 25 | ] 26 | }, 27 | "execution_count": 1, 28 | "metadata": {}, 29 | "output_type": "execute_result" 30 | } 31 | ], 32 | "source": [ 33 | "1 < 2 < 3" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "The above statement checks if 1 was less than 2 **and** if 2 was less than 3. We could have written this using an **and** statement in Python:" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 2, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "data": { 50 | "text/plain": [ 51 | "True" 52 | ] 53 | }, 54 | "execution_count": 2, 55 | "metadata": {}, 56 | "output_type": "execute_result" 57 | } 58 | ], 59 | "source": [ 60 | "1<2 and 2<3" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "The **and** is used to make sure two checks have to be true in order for the total check to be true. Let's see another example:" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 3, 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "data": { 77 | "text/plain": [ 78 | "True" 79 | ] 80 | }, 81 | "execution_count": 3, 82 | "metadata": {}, 83 | "output_type": "execute_result" 84 | } 85 | ], 86 | "source": [ 87 | "1 < 3 > 2" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "The above checks if 3 is larger than both of the other numbers, so you could use **and** to rewrite it as:" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 4, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "True" 106 | ] 107 | }, 108 | "execution_count": 4, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "1<3 and 3>2" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "It's important to note that Python is checking both instances of the comparisons. We can also use **or** to write comparisons in Python. For example:" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 5, 127 | "metadata": {}, 128 | "outputs": [ 129 | { 130 | "data": { 131 | "text/plain": [ 132 | "True" 133 | ] 134 | }, 135 | "execution_count": 5, 136 | "metadata": {}, 137 | "output_type": "execute_result" 138 | } 139 | ], 140 | "source": [ 141 | "1==2 or 2<3" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "Note how it was true; this is because with the **or** operator, we only need one *or* the other to be true. Let's see one more example to drive this home:" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 6, 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "data": { 158 | "text/plain": [ 159 | "True" 160 | ] 161 | }, 162 | "execution_count": 6, 163 | "metadata": {}, 164 | "output_type": "execute_result" 165 | } 166 | ], 167 | "source": [ 168 | "1==1 or 100==1" 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": {}, 174 | "source": [ 175 | "Great! For an overview of this quick lesson: You should have a comfortable understanding of using **and** and **or** statements as well as reading chained comparison code.\n", 176 | "\n", 177 | "Go ahead and go to the quiz for this section to check your understanding!" 178 | ] 179 | } 180 | ], 181 | "metadata": { 182 | "kernelspec": { 183 | "display_name": "Python 3", 184 | "language": "python", 185 | "name": "python3" 186 | }, 187 | "language_info": { 188 | "codemirror_mode": { 189 | "name": "ipython", 190 | "version": 2 191 | }, 192 | "file_extension": ".py", 193 | "mimetype": "text/x-python", 194 | "name": "python", 195 | "nbconvert_exporter": "python", 196 | "pygments_lexer": "ipython2", 197 | "version": "2.7.12" 198 | } 199 | }, 200 | "nbformat": 4, 201 | "nbformat_minor": 2 202 | } 203 | -------------------------------------------------------------------------------- /02-Python Statements/.ipynb_checkpoints/01-Introduction to Python Statements-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "slideshow": { 7 | "slide_type": "slide" 8 | } 9 | }, 10 | "source": [ 11 | "# Introduction to Python Statements\n", 12 | "\n", 13 | "In this lecture we will be doing a quick overview of Python Statements. This lecture will emphasize differences between Python and other languages such as C++. \n", 14 | "\n", 15 | "There are two reasons we take this approach for learning the context of Python Statements:\n", 16 | "\n", 17 | " 1.) If you are coming from a different language this will rapidly accelerate your understanding of Python.\n", 18 | " 2.) Learning about statements will allow you to be able to read other languages more easily in the future." 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "slideshow": { 25 | "slide_type": "slide" 26 | } 27 | }, 28 | "source": [ 29 | "## Python vs Other Languages\n", 30 | "\n", 31 | "Let's create a simple statement that says:\n", 32 | "\"If a is greater than b, assign 2 to a and 4 to b\"\n", 33 | "\n", 34 | "Take a look at these two if statements (we will learn about building out if statements soon).\n", 35 | "\n", 36 | "**Version 1 (Other Languages)**\n", 37 | "\n", 38 | " if (a>b){\n", 39 | " a = 2;\n", 40 | " b = 4;\n", 41 | " }\n", 42 | " \n", 43 | "**Version 2 (Python)** \n", 44 | "\n", 45 | " if a>b:\n", 46 | " a = 2\n", 47 | " b = 4" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "You'll notice that Python is less cluttered and much more readable than the first version. How does Python manage this?\n", 55 | "\n", 56 | "Let's walk through the main differences:\n", 57 | "\n", 58 | "Python gets rid of () and {} by incorporating two main factors: a *colon* and *whitespace*. The statement is ended with a colon, and whitespace is used (indentation) to describe what takes place in case of the statement.\n", 59 | "\n", 60 | "Another major difference is the lack of semicolons in Python. Semicolons are used to denote statement endings in many other languages, but in Python, the end of a line is the same as the end of a statement.\n", 61 | "\n", 62 | "Lastly, to end this brief overview of differences, let's take a closer look at indentation syntax in Python vs other languages:\n", 63 | "\n", 64 | "## Indentation\n", 65 | "\n", 66 | "Here is some pseudo-code to indicate the use of whitespace and indentation in Python:\n", 67 | "\n", 68 | "**Other Languages**\n", 69 | "\n", 70 | " if (x)\n", 71 | " if(y)\n", 72 | " code-statement;\n", 73 | " else\n", 74 | " another-code-statement;\n", 75 | " \n", 76 | "**Python**\n", 77 | " \n", 78 | " if x:\n", 79 | " if y:\n", 80 | " code-statement\n", 81 | " else:\n", 82 | " another-code-statement" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "Note how Python is so heavily driven by code indentation and whitespace. This means that code readability is a core part of the design of the Python language.\n", 90 | "\n", 91 | "Now let's start diving deeper by coding these sort of statements in Python!" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": { 97 | "collapsed": true 98 | }, 99 | "source": [ 100 | "## Time to code!" 101 | ] 102 | } 103 | ], 104 | "metadata": { 105 | "kernelspec": { 106 | "display_name": "Python 3", 107 | "language": "python", 108 | "name": "python3" 109 | }, 110 | "language_info": { 111 | "codemirror_mode": { 112 | "name": "ipython", 113 | "version": 3 114 | }, 115 | "file_extension": ".py", 116 | "mimetype": "text/x-python", 117 | "name": "python", 118 | "nbconvert_exporter": "python", 119 | "pygments_lexer": "ipython3", 120 | "version": "3.6.2" 121 | } 122 | }, 123 | "nbformat": 4, 124 | "nbformat_minor": 1 125 | } 126 | -------------------------------------------------------------------------------- /02-Python Statements/.ipynb_checkpoints/02-if, elif, and else Statements-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# if, elif, else Statements\n", 8 | "\n", 9 | "if Statements in Python allows us to tell the computer to perform alternative actions based on a certain set of results.\n", 10 | "\n", 11 | "Verbally, we can imagine we are telling the computer:\n", 12 | "\n", 13 | "\"Hey if this case happens, perform some action\"\n", 14 | "\n", 15 | "We can then expand the idea further with elif and else statements, which allow us to tell the computer:\n", 16 | "\n", 17 | "\"Hey if this case happens, perform some action. Else, if another case happens, perform some other action. Else, if *none* of the above cases happened, perform this action.\"\n", 18 | "\n", 19 | "Let's go ahead and look at the syntax format for if statements to get a better idea of this:\n", 20 | "\n", 21 | " if case1:\n", 22 | " perform action1\n", 23 | " elif case2:\n", 24 | " perform action2\n", 25 | " else: \n", 26 | " perform action3" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": {}, 32 | "source": [ 33 | "## First Example\n", 34 | "\n", 35 | "Let's see a quick example of this:" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 1, 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "It was true!\n" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "if True:\n", 53 | " print('It was true!')" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "Let's add in some else logic:" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 2, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "name": "stdout", 70 | "output_type": "stream", 71 | "text": [ 72 | "I will be printed in any case where x is not true\n" 73 | ] 74 | } 75 | ], 76 | "source": [ 77 | "x = False\n", 78 | "\n", 79 | "if x:\n", 80 | " print('x was True!')\n", 81 | "else:\n", 82 | " print('I will be printed in any case where x is not true')" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "### Multiple Branches\n", 90 | "\n", 91 | "Let's get a fuller picture of how far if, elif, and else can take us!\n", 92 | "\n", 93 | "We write this out in a nested structure. Take note of how the if, elif, and else line up in the code. This can help you see what if is related to what elif or else statements.\n", 94 | "\n", 95 | "We'll reintroduce a comparison syntax for Python." 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 3, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "name": "stdout", 105 | "output_type": "stream", 106 | "text": [ 107 | "Welcome to the bank!\n" 108 | ] 109 | } 110 | ], 111 | "source": [ 112 | "loc = 'Bank'\n", 113 | "\n", 114 | "if loc == 'Auto Shop':\n", 115 | " print('Welcome to the Auto Shop!')\n", 116 | "elif loc == 'Bank':\n", 117 | " print('Welcome to the bank!')\n", 118 | "else:\n", 119 | " print('Where are you?')" 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": {}, 125 | "source": [ 126 | "Note how the nested if statements are each checked until a True boolean causes the nested code below it to run. You should also note that you can put in as many elif statements as you want before you close off with an else.\n", 127 | "\n", 128 | "Let's create two more simple examples for the if, elif, and else statements:" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 4, 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "name": "stdout", 138 | "output_type": "stream", 139 | "text": [ 140 | "Welcome Sammy!\n" 141 | ] 142 | } 143 | ], 144 | "source": [ 145 | "person = 'Sammy'\n", 146 | "\n", 147 | "if person == 'Sammy':\n", 148 | " print('Welcome Sammy!')\n", 149 | "else:\n", 150 | " print(\"Welcome, what's your name?\")" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 5, 156 | "metadata": {}, 157 | "outputs": [ 158 | { 159 | "name": "stdout", 160 | "output_type": "stream", 161 | "text": [ 162 | "Welcome George!\n" 163 | ] 164 | } 165 | ], 166 | "source": [ 167 | "person = 'George'\n", 168 | "\n", 169 | "if person == 'Sammy':\n", 170 | " print('Welcome Sammy!')\n", 171 | "elif person =='George':\n", 172 | " print('Welcome George!')\n", 173 | "else:\n", 174 | " print(\"Welcome, what's your name?\")" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "## Indentation\n", 182 | "\n", 183 | "It is important to keep a good understanding of how indentation works in Python to maintain the structure and order of your code. We will touch on this topic again when we start building out functions!" 184 | ] 185 | } 186 | ], 187 | "metadata": { 188 | "kernelspec": { 189 | "display_name": "Python 3", 190 | "language": "python", 191 | "name": "python3" 192 | }, 193 | "language_info": { 194 | "codemirror_mode": { 195 | "name": "ipython", 196 | "version": 3 197 | }, 198 | "file_extension": ".py", 199 | "mimetype": "text/x-python", 200 | "name": "python", 201 | "nbconvert_exporter": "python", 202 | "pygments_lexer": "ipython3", 203 | "version": "3.6.2" 204 | } 205 | }, 206 | "nbformat": 4, 207 | "nbformat_minor": 1 208 | } 209 | -------------------------------------------------------------------------------- /02-Python Statements/.ipynb_checkpoints/05-range()-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# range()\n", 8 | "\n", 9 | "In this short lecture we will be discussing the range function. We haven't developed a very deep level of knowledge of functions yet, but we can understand the basics of this simple (but extremely useful!) function.\n", 10 | "\n", 11 | "range() allows us to generate a list of numbers ranging from a starting point *up to but not including* an ending point. We can also specify step size. Let's walk through a few examples:" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "data": { 21 | "text/plain": [ 22 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" 23 | ] 24 | }, 25 | "execution_count": 1, 26 | "metadata": {}, 27 | "output_type": "execute_result" 28 | } 29 | ], 30 | "source": [ 31 | "list(range(0,10))" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "To see the output of range() as a list, we *cast* it as a list as shown above. This is rarely done, as normally range is used in for loops." 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 6, 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "data": { 48 | "text/plain": [ 49 | "range" 50 | ] 51 | }, 52 | "execution_count": 6, 53 | "metadata": {}, 54 | "output_type": "execute_result" 55 | } 56 | ], 57 | "source": [ 58 | "# Range objects\n", 59 | "x =range(0,10)\n", 60 | "type(x)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "range objects behave like *generators* - they don't produce every value all at once, but deliver them one at a time as needed. Behind the scenes this saves on overhead since you're not storing every value, and it improves the performance of your code! We will learn more about generators later on in the course." 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 3, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "start = 0 #Default\n", 77 | "stop = 20 \n", 78 | "x = range(start,stop)" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 4, 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "data": { 88 | "text/plain": [ 89 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]" 90 | ] 91 | }, 92 | "execution_count": 4, 93 | "metadata": {}, 94 | "output_type": "execute_result" 95 | } 96 | ], 97 | "source": [ 98 | "list(x)" 99 | ] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "metadata": {}, 104 | "source": [ 105 | "Great! Notice how it went *up to* 20, but doesn't actually produce 20. Just like in indexing. What about step size? We can specify that as a third argument:" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 5, 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "data": { 115 | "text/plain": [ 116 | "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]" 117 | ] 118 | }, 119 | "execution_count": 5, 120 | "metadata": {}, 121 | "output_type": "execute_result" 122 | } 123 | ], 124 | "source": [ 125 | "x = range(start,stop,2)\n", 126 | "\n", 127 | "#Show\n", 128 | "list(x)" 129 | ] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "metadata": { 134 | "collapsed": true 135 | }, 136 | "source": [ 137 | "You should now have a good understanding of how to use range() in Python." 138 | ] 139 | } 140 | ], 141 | "metadata": { 142 | "kernelspec": { 143 | "display_name": "Python 3", 144 | "language": "python", 145 | "name": "python3" 146 | }, 147 | "language_info": { 148 | "codemirror_mode": { 149 | "name": "ipython", 150 | "version": 3 151 | }, 152 | "file_extension": ".py", 153 | "mimetype": "text/x-python", 154 | "name": "python", 155 | "nbconvert_exporter": "python", 156 | "pygments_lexer": "ipython3", 157 | "version": "3.6.2" 158 | } 159 | }, 160 | "nbformat": 4, 161 | "nbformat_minor": 1 162 | } 163 | -------------------------------------------------------------------------------- /02-Python Statements/.ipynb_checkpoints/06-List Comprehensions-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "# List Comprehensions\n", 10 | "\n", 11 | "In addition to sequence operations and list methods, Python includes a more advanced operation called a list comprehension.\n", 12 | "\n", 13 | "List comprehensions allow us to build out lists using a different notation. You can think of it as essentially a one line for loop built inside of brackets. For a simple example:\n", 14 | "## Example 1" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "# Grab every letter in string\n", 24 | "lst = [x for x in 'word']" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 2, 30 | "metadata": {}, 31 | "outputs": [ 32 | { 33 | "data": { 34 | "text/plain": [ 35 | "['w', 'o', 'r', 'd']" 36 | ] 37 | }, 38 | "execution_count": 2, 39 | "metadata": {}, 40 | "output_type": "execute_result" 41 | } 42 | ], 43 | "source": [ 44 | "# Check\n", 45 | "lst" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "This is the basic idea of a list comprehension. If you're familiar with mathematical notation this format should feel familiar for example: x^2 : x in { 0,1,2...10 } \n", 53 | "\n", 54 | "Let's see a few more examples of list comprehensions in Python:\n", 55 | "## Example 2" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 3, 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [ 64 | "# Square numbers in range and turn into list\n", 65 | "lst = [x**2 for x in range(0,11)]" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 4, 71 | "metadata": {}, 72 | "outputs": [ 73 | { 74 | "data": { 75 | "text/plain": [ 76 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]" 77 | ] 78 | }, 79 | "execution_count": 4, 80 | "metadata": {}, 81 | "output_type": "execute_result" 82 | } 83 | ], 84 | "source": [ 85 | "lst" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "## Example 3\n", 93 | "Let's see how to add in if statements:" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 5, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "# Check for even numbers in a range\n", 103 | "lst = [x for x in range(11) if x % 2 == 0]" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 6, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "data": { 113 | "text/plain": [ 114 | "[0, 2, 4, 6, 8, 10]" 115 | ] 116 | }, 117 | "execution_count": 6, 118 | "metadata": {}, 119 | "output_type": "execute_result" 120 | } 121 | ], 122 | "source": [ 123 | "lst" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "## Example 4\n", 131 | "Can also do more complicated arithmetic:" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 7, 137 | "metadata": {}, 138 | "outputs": [ 139 | { 140 | "data": { 141 | "text/plain": [ 142 | "[32.0, 50.0, 68.18, 94.1]" 143 | ] 144 | }, 145 | "execution_count": 7, 146 | "metadata": {}, 147 | "output_type": "execute_result" 148 | } 149 | ], 150 | "source": [ 151 | "# Convert Celsius to Fahrenheit\n", 152 | "celsius = [0,10,20.1,34.5]\n", 153 | "\n", 154 | "fahrenheit = [((9/5)*temp + 32) for temp in celsius ]\n", 155 | "\n", 156 | "fahrenheit" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "## Example 5\n", 164 | "We can also perform nested list comprehensions, for example:" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 8, 170 | "metadata": {}, 171 | "outputs": [ 172 | { 173 | "data": { 174 | "text/plain": [ 175 | "[0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]" 176 | ] 177 | }, 178 | "execution_count": 8, 179 | "metadata": {}, 180 | "output_type": "execute_result" 181 | } 182 | ], 183 | "source": [ 184 | "lst = [ x**2 for x in [x**2 for x in range(11)]]\n", 185 | "lst" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "Later on in the course we will learn about generator comprehensions. After this lecture you should feel comfortable reading and writing basic list comprehensions." 193 | ] 194 | } 195 | ], 196 | "metadata": { 197 | "kernelspec": { 198 | "display_name": "Python 3", 199 | "language": "python", 200 | "name": "python3" 201 | }, 202 | "language_info": { 203 | "codemirror_mode": { 204 | "name": "ipython", 205 | "version": 3 206 | }, 207 | "file_extension": ".py", 208 | "mimetype": "text/x-python", 209 | "name": "python", 210 | "nbconvert_exporter": "python", 211 | "pygments_lexer": "ipython3", 212 | "version": "3.6.2" 213 | } 214 | }, 215 | "nbformat": 4, 216 | "nbformat_minor": 1 217 | } 218 | -------------------------------------------------------------------------------- /02-Python Statements/.ipynb_checkpoints/07-Statements Assessment Test-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "# Statements Assessment Test\n", 10 | "Let's test your knowledge!" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "_____\n", 18 | "**Use for, .split(), and if to create a Statement that will print out words that start with 's':**" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 3, 24 | "metadata": { 25 | "collapsed": true 26 | }, 27 | "outputs": [], 28 | "source": [ 29 | "st = 'Print only the words that start with s in this sentence'" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 1, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "#Code here" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "______\n", 46 | "**Use range() to print all the even numbers from 0 to 10.**" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 2, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [ 55 | "#Code Here" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "___\n", 63 | "**Use a List Comprehension to create a list of all numbers between 1 and 50 that are divisible by 3.**" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 3, 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "data": { 73 | "text/plain": [ 74 | "[]" 75 | ] 76 | }, 77 | "execution_count": 3, 78 | "metadata": {}, 79 | "output_type": "execute_result" 80 | } 81 | ], 82 | "source": [ 83 | "#Code in this cell\n", 84 | "[]" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "_____\n", 92 | "**Go through the string below and if the length of a word is even print \"even!\"**" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 6, 98 | "metadata": { 99 | "collapsed": true 100 | }, 101 | "outputs": [], 102 | "source": [ 103 | "st = 'Print every word in this sentence that has an even number of letters'" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 4, 109 | "metadata": { 110 | "collapsed": true 111 | }, 112 | "outputs": [], 113 | "source": [ 114 | "#Code in this cell" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "____\n", 122 | "**Write a program that prints the integers from 1 to 100. But for multiples of three print \"Fizz\" instead of the number, and for the multiples of five print \"Buzz\". For numbers which are multiples of both three and five print \"FizzBuzz\".**" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": null, 128 | "metadata": {}, 129 | "outputs": [], 130 | "source": [ 131 | "#Code in this cell" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "____\n", 139 | "**Use List Comprehension to create a list of the first letters of every word in the string below:**" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 8, 145 | "metadata": { 146 | "collapsed": true 147 | }, 148 | "outputs": [], 149 | "source": [ 150 | "st = 'Create a list of the first letters of every word in this string'" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 5, 156 | "metadata": { 157 | "collapsed": true 158 | }, 159 | "outputs": [], 160 | "source": [ 161 | "#Code in this cell" 162 | ] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": {}, 167 | "source": [ 168 | "### Great Job!" 169 | ] 170 | } 171 | ], 172 | "metadata": { 173 | "kernelspec": { 174 | "display_name": "Python 3", 175 | "language": "python", 176 | "name": "python3" 177 | }, 178 | "language_info": { 179 | "codemirror_mode": { 180 | "name": "ipython", 181 | "version": 3 182 | }, 183 | "file_extension": ".py", 184 | "mimetype": "text/x-python", 185 | "name": "python", 186 | "nbconvert_exporter": "python", 187 | "pygments_lexer": "ipython3", 188 | "version": "3.6.2" 189 | } 190 | }, 191 | "nbformat": 4, 192 | "nbformat_minor": 1 193 | } 194 | -------------------------------------------------------------------------------- /02-Python Statements/.ipynb_checkpoints/08-Statements Assessment Test - Solutions-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "# Statements Assessment Solutions" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "_____\n", 17 | "**Use for, .split(), and if to create a Statement that will print out words that start with 's':**" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 1, 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "st = 'Print only the words that start with s in this sentence'" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 2, 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "name": "stdout", 36 | "output_type": "stream", 37 | "text": [ 38 | "start\n", 39 | "s\n", 40 | "sentence\n" 41 | ] 42 | } 43 | ], 44 | "source": [ 45 | "for word in st.split():\n", 46 | " if word[0] == 's':\n", 47 | " print(word)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "______\n", 55 | "**Use range() to print all the even numbers from 0 to 10.**" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 3, 61 | "metadata": {}, 62 | "outputs": [ 63 | { 64 | "data": { 65 | "text/plain": [ 66 | "[0, 2, 4, 6, 8, 10]" 67 | ] 68 | }, 69 | "execution_count": 3, 70 | "metadata": {}, 71 | "output_type": "execute_result" 72 | } 73 | ], 74 | "source": [ 75 | "list(range(0,11,2))" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "___\n", 83 | "**Use List comprehension to create a list of all numbers between 1 and 50 that are divisible by 3.**" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 4, 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "data": { 93 | "text/plain": [ 94 | "[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48]" 95 | ] 96 | }, 97 | "execution_count": 4, 98 | "metadata": {}, 99 | "output_type": "execute_result" 100 | } 101 | ], 102 | "source": [ 103 | "[x for x in range(1,51) if x%3 == 0]" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "_____\n", 111 | "**Go through the string below and if the length of a word is even print \"even!\"**" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 5, 117 | "metadata": {}, 118 | "outputs": [], 119 | "source": [ 120 | "st = 'Print every word in this sentence that has an even number of letters'" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 6, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "name": "stdout", 130 | "output_type": "stream", 131 | "text": [ 132 | "word <-- has an even length!\n", 133 | "in <-- has an even length!\n", 134 | "this <-- has an even length!\n", 135 | "sentence <-- has an even length!\n", 136 | "that <-- has an even length!\n", 137 | "an <-- has an even length!\n", 138 | "even <-- has an even length!\n", 139 | "number <-- has an even length!\n", 140 | "of <-- has an even length!\n" 141 | ] 142 | } 143 | ], 144 | "source": [ 145 | "for word in st.split():\n", 146 | " if len(word)%2 == 0:\n", 147 | " print(word+\" <-- has an even length!\")" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "____\n", 155 | "**Write a program that prints the integers from 1 to 100. But for multiples of three print \"Fizz\" instead of the number, and for the multiples of five print \"Buzz\". For numbers which are multiples of both three and five print \"FizzBuzz\".**" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": null, 161 | "metadata": {}, 162 | "outputs": [], 163 | "source": [ 164 | "for num in range(1,101):\n", 165 | " if num % 3 == 0 and num % 5 == 0:\n", 166 | " print(\"FizzBuzz\")\n", 167 | " elif num % 3 == 0:\n", 168 | " print(\"Fizz\")\n", 169 | " elif num % 5 == 0:\n", 170 | " print(\"Buzz\")\n", 171 | " else:\n", 172 | " print(num)" 173 | ] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "____\n", 180 | "**Use a List Comprehension to create a list of the first letters of every word in the string below:**" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 7, 186 | "metadata": {}, 187 | "outputs": [], 188 | "source": [ 189 | "st = 'Create a list of the first letters of every word in this string'" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 8, 195 | "metadata": {}, 196 | "outputs": [ 197 | { 198 | "data": { 199 | "text/plain": [ 200 | "['C', 'a', 'l', 'o', 't', 'f', 'l', 'o', 'e', 'w', 'i', 't', 's']" 201 | ] 202 | }, 203 | "execution_count": 8, 204 | "metadata": {}, 205 | "output_type": "execute_result" 206 | } 207 | ], 208 | "source": [ 209 | "[word[0] for word in st.split()]" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": {}, 215 | "source": [ 216 | "### Great Job!" 217 | ] 218 | } 219 | ], 220 | "metadata": { 221 | "kernelspec": { 222 | "display_name": "Python 3", 223 | "language": "python", 224 | "name": "python3" 225 | }, 226 | "language_info": { 227 | "codemirror_mode": { 228 | "name": "ipython", 229 | "version": 3 230 | }, 231 | "file_extension": ".py", 232 | "mimetype": "text/x-python", 233 | "name": "python", 234 | "nbconvert_exporter": "python", 235 | "pygments_lexer": "ipython3", 236 | "version": "3.6.2" 237 | } 238 | }, 239 | "nbformat": 4, 240 | "nbformat_minor": 1 241 | } 242 | -------------------------------------------------------------------------------- /02-Python Statements/.ipynb_checkpoints/09-Guessing Game Challenge-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Guessing Game Challenge\n", 8 | "\n", 9 | "Let's use `while` loops to create a guessing game.\n", 10 | "\n", 11 | "The Challenge:\n", 12 | "\n", 13 | "Write a program that picks a random integer from 1 to 100, and has players guess the number. The rules are:\n", 14 | "\n", 15 | "1. If a player's guess is less than 1 or greater than 100, say \"OUT OF BOUNDS\"\n", 16 | "2. On a player's first turn, if their guess is\n", 17 | " * within 10 of the number, return \"WARM!\"\n", 18 | " * further than 10 away from the number, return \"COLD!\"\n", 19 | "3. On all subsequent turns, if a guess is \n", 20 | " * closer to the number than the previous guess return \"WARMER!\"\n", 21 | " * farther from the number than the previous guess, return \"COLDER!\"\n", 22 | "4. When the player's guess equals the number, tell them they've guessed correctly *and* how many guesses it took!\n", 23 | "\n", 24 | "You can try this from scratch, or follow the steps outlined below. A separate Solution notebook has been provided. Good luck!\n" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "#### First, pick a random integer from 1 to 100 using the random module and assign it to a variable\n", 32 | "\n", 33 | "Note: `random.randint(a,b)` returns a random integer in range `[a, b]`, including both end points." 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "#### Next, print an introduction to the game and explain the rules" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "#### Create a list to store guesses\n", 62 | "\n", 63 | "Hint: zero is a good placeholder value. It's useful because it evaluates to \"False\"" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "metadata": {}, 76 | "source": [ 77 | "#### Write a `while` loop that asks for a valid guess. Test it a few times to make sure it works." 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "while True:\n", 87 | " \n", 88 | " pass" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "#### Write a `while` loop that compares the player's guess to our number. If the player guesses correctly, break from the loop. Otherwise, tell the player if they're warmer or colder, and continue asking for guesses.\n", 96 | "\n", 97 | "Some hints:\n", 98 | "* it may help to sketch out all possible combinations on paper first!\n", 99 | "* you can use the `abs()` function to find the positive difference between two numbers\n", 100 | "* if you append all new guesses to the list, then the previous guess is given as `guesses[-2]`" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": null, 106 | "metadata": {}, 107 | "outputs": [], 108 | "source": [ 109 | "while True:\n", 110 | "\n", 111 | " # we can copy the code from above to take an input\n", 112 | "\n", 113 | " pass" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "That's it! You've just programmed your first game!\n", 121 | "\n", 122 | "In the next section we'll learn how to turn some of these repetitive actions into *functions* that can be called whenever we need them." 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "### Good Job!" 130 | ] 131 | } 132 | ], 133 | "metadata": { 134 | "kernelspec": { 135 | "display_name": "Python 3", 136 | "language": "python", 137 | "name": "python3" 138 | }, 139 | "language_info": { 140 | "codemirror_mode": { 141 | "name": "ipython", 142 | "version": 3 143 | }, 144 | "file_extension": ".py", 145 | "mimetype": "text/x-python", 146 | "name": "python", 147 | "nbconvert_exporter": "python", 148 | "pygments_lexer": "ipython3", 149 | "version": "3.6.2" 150 | } 151 | }, 152 | "nbformat": 4, 153 | "nbformat_minor": 2 154 | } 155 | -------------------------------------------------------------------------------- /02-Python Statements/01-Introduction to Python Statements.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "slideshow": { 7 | "slide_type": "slide" 8 | } 9 | }, 10 | "source": [ 11 | "# Introduction to Python Statements\n", 12 | "\n", 13 | "In this lecture we will be doing a quick overview of Python Statements. This lecture will emphasize differences between Python and other languages such as C++. \n", 14 | "\n", 15 | "There are two reasons we take this approach for learning the context of Python Statements:\n", 16 | "\n", 17 | " 1.) If you are coming from a different language this will rapidly accelerate your understanding of Python.\n", 18 | " 2.) Learning about statements will allow you to be able to read other languages more easily in the future." 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "slideshow": { 25 | "slide_type": "slide" 26 | } 27 | }, 28 | "source": [ 29 | "## Python vs Other Languages\n", 30 | "\n", 31 | "Let's create a simple statement that says:\n", 32 | "\"If a is greater than b, assign 2 to a and 4 to b\"\n", 33 | "\n", 34 | "Take a look at these two if statements (we will learn about building out if statements soon).\n", 35 | "\n", 36 | "**Version 1 (Other Languages)**\n", 37 | "\n", 38 | " if (a>b){\n", 39 | " a = 2;\n", 40 | " b = 4;\n", 41 | " }\n", 42 | " \n", 43 | "**Version 2 (Python)** \n", 44 | "\n", 45 | " if a>b:\n", 46 | " a = 2\n", 47 | " b = 4" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "You'll notice that Python is less cluttered and much more readable than the first version. How does Python manage this?\n", 55 | "\n", 56 | "Let's walk through the main differences:\n", 57 | "\n", 58 | "Python gets rid of () and {} by incorporating two main factors: a *colon* and *whitespace*. The statement is ended with a colon, and whitespace is used (indentation) to describe what takes place in case of the statement.\n", 59 | "\n", 60 | "Another major difference is the lack of semicolons in Python. Semicolons are used to denote statement endings in many other languages, but in Python, the end of a line is the same as the end of a statement.\n", 61 | "\n", 62 | "Lastly, to end this brief overview of differences, let's take a closer look at indentation syntax in Python vs other languages:\n", 63 | "\n", 64 | "## Indentation\n", 65 | "\n", 66 | "Here is some pseudo-code to indicate the use of whitespace and indentation in Python:\n", 67 | "\n", 68 | "**Other Languages**\n", 69 | "\n", 70 | " if (x)\n", 71 | " if(y)\n", 72 | " code-statement;\n", 73 | " else\n", 74 | " another-code-statement;\n", 75 | " \n", 76 | "**Python**\n", 77 | " \n", 78 | " if x:\n", 79 | " if y:\n", 80 | " code-statement\n", 81 | " else:\n", 82 | " another-code-statement" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "Note how Python is so heavily driven by code indentation and whitespace. This means that code readability is a core part of the design of the Python language.\n", 90 | "\n", 91 | "Now let's start diving deeper by coding these sort of statements in Python!" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "## Time to code!" 99 | ] 100 | } 101 | ], 102 | "metadata": { 103 | "kernelspec": { 104 | "display_name": "Python 3", 105 | "language": "python", 106 | "name": "python3" 107 | }, 108 | "language_info": { 109 | "codemirror_mode": { 110 | "name": "ipython", 111 | "version": 2 112 | }, 113 | "file_extension": ".py", 114 | "mimetype": "text/x-python", 115 | "name": "python", 116 | "nbconvert_exporter": "python", 117 | "pygments_lexer": "ipython2", 118 | "version": "2.7.12" 119 | } 120 | }, 121 | "nbformat": 4, 122 | "nbformat_minor": 2 123 | } 124 | -------------------------------------------------------------------------------- /02-Python Statements/02-if, elif, and else Statements.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# if, elif, else Statements\n", 8 | "\n", 9 | "if Statements in Python allows us to tell the computer to perform alternative actions based on a certain set of results.\n", 10 | "\n", 11 | "Verbally, we can imagine we are telling the computer:\n", 12 | "\n", 13 | "\"Hey if this case happens, perform some action\"\n", 14 | "\n", 15 | "We can then expand the idea further with elif and else statements, which allow us to tell the computer:\n", 16 | "\n", 17 | "\"Hey if this case happens, perform some action. Else, if another case happens, perform some other action. Else, if *none* of the above cases happened, perform this action.\"\n", 18 | "\n", 19 | "Let's go ahead and look at the syntax format for if statements to get a better idea of this:\n", 20 | "\n", 21 | " if case1:\n", 22 | " perform action1\n", 23 | " elif case2:\n", 24 | " perform action2\n", 25 | " else: \n", 26 | " perform action3" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": {}, 32 | "source": [ 33 | "## First Example\n", 34 | "\n", 35 | "Let's see a quick example of this:" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 3, 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "Bye\n" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "if False:\n", 53 | " print('Hi')\n", 54 | "else :\n", 55 | " print('Bye')\n", 56 | " " 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 1, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "name": "stdout", 66 | "output_type": "stream", 67 | "text": [ 68 | "It was true!\n" 69 | ] 70 | } 71 | ], 72 | "source": [ 73 | "if True:\n", 74 | " print('It was true!')" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "Let's add in some else logic:" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 2, 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "name": "stdout", 91 | "output_type": "stream", 92 | "text": [ 93 | "I will be printed in any case where x is not true\n" 94 | ] 95 | } 96 | ], 97 | "source": [ 98 | "x = False\n", 99 | "\n", 100 | "if x:\n", 101 | " print('x was True!')\n", 102 | "else:\n", 103 | " print('I will be printed in any case where x is not true')" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "### Multiple Branches\n", 111 | "\n", 112 | "Let's get a fuller picture of how far if, elif, and else can take us!\n", 113 | "\n", 114 | "We write this out in a nested structure. Take note of how the if, elif, and else line up in the code. This can help you see what if is related to what elif or else statements.\n", 115 | "\n", 116 | "We'll reintroduce a comparison syntax for Python." 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 3, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "name": "stdout", 126 | "output_type": "stream", 127 | "text": [ 128 | "Welcome to the bank!\n" 129 | ] 130 | } 131 | ], 132 | "source": [ 133 | "loc = 'Bank'\n", 134 | "\n", 135 | "if loc == 'Auto Shop':\n", 136 | " print('Welcome to the Auto Shop!')\n", 137 | "elif loc == 'Bank':\n", 138 | " print('Welcome to the bank!')\n", 139 | "else:\n", 140 | " print('Where are you?')" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": {}, 146 | "source": [ 147 | "Note how the nested if statements are each checked until a True boolean causes the nested code below it to run. You should also note that you can put in as many elif statements as you want before you close off with an else.\n", 148 | "\n", 149 | "Let's create two more simple examples for the if, elif, and else statements:" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 4, 155 | "metadata": {}, 156 | "outputs": [ 157 | { 158 | "name": "stdout", 159 | "output_type": "stream", 160 | "text": [ 161 | "Welcome Sammy!\n" 162 | ] 163 | } 164 | ], 165 | "source": [ 166 | "person = 'Sammy'\n", 167 | "\n", 168 | "if person == 'Sammy':\n", 169 | " print('Welcome Sammy!')\n", 170 | "else:\n", 171 | " print(\"Welcome, what's your name?\")" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 5, 177 | "metadata": {}, 178 | "outputs": [ 179 | { 180 | "name": "stdout", 181 | "output_type": "stream", 182 | "text": [ 183 | "Welcome George!\n" 184 | ] 185 | } 186 | ], 187 | "source": [ 188 | "person = 'George'\n", 189 | "\n", 190 | "if person == 'Sammy':\n", 191 | " print('Welcome Sammy!')\n", 192 | "elif person =='George':\n", 193 | " print('Welcome George!')\n", 194 | "else:\n", 195 | " print(\"Welcome, what's your name?\")" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 5, 201 | "metadata": {}, 202 | "outputs": [ 203 | { 204 | "name": "stdout", 205 | "output_type": "stream", 206 | "text": [ 207 | "Person1 is test1 and Person 2 is test2\n" 208 | ] 209 | } 210 | ], 211 | "source": [ 212 | "person1 = 'test1'\n", 213 | "person2 = 'test2'\n", 214 | "\n", 215 | "if person1 == 'test1' and person2 == 'test2':\n", 216 | " print('Person1 is {} and Person 2 is {}'.format(person1,person2))" 217 | ] 218 | }, 219 | { 220 | "cell_type": "markdown", 221 | "metadata": {}, 222 | "source": [ 223 | "## Indentation\n", 224 | "\n", 225 | "It is important to keep a good understanding of how indentation works in Python to maintain the structure and order of your code. We will touch on this topic again when we start building out functions!" 226 | ] 227 | } 228 | ], 229 | "metadata": { 230 | "kernelspec": { 231 | "display_name": "Python 3", 232 | "language": "python", 233 | "name": "python3" 234 | }, 235 | "language_info": { 236 | "codemirror_mode": { 237 | "name": "ipython", 238 | "version": 2 239 | }, 240 | "file_extension": ".py", 241 | "mimetype": "text/x-python", 242 | "name": "python", 243 | "nbconvert_exporter": "python", 244 | "pygments_lexer": "ipython2", 245 | "version": "2.7.12" 246 | } 247 | }, 248 | "nbformat": 4, 249 | "nbformat_minor": 2 250 | } 251 | -------------------------------------------------------------------------------- /02-Python Statements/06-List Comprehensions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "# List Comprehensions\n", 10 | "\n", 11 | "In addition to sequence operations and list methods, Python includes a more advanced operation called a list comprehension.\n", 12 | "\n", 13 | "List comprehensions allow us to build out lists using a different notation. You can think of it as essentially a one line for loop built inside of brackets. For a simple example:\n", 14 | "## Example 1" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "# Grab every letter in string\n", 24 | "lst = [x for x in 'word']" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 2, 30 | "metadata": {}, 31 | "outputs": [ 32 | { 33 | "data": { 34 | "text/plain": [ 35 | "['w', 'o', 'r', 'd']" 36 | ] 37 | }, 38 | "execution_count": 2, 39 | "metadata": {}, 40 | "output_type": "execute_result" 41 | } 42 | ], 43 | "source": [ 44 | "# Check\n", 45 | "lst" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "This is the basic idea of a list comprehension. If you're familiar with mathematical notation this format should feel familiar for example: x^2 : x in { 0,1,2...10 } \n", 53 | "\n", 54 | "Let's see a few more examples of list comprehensions in Python:\n", 55 | "## Example 2" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 3, 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [ 64 | "# Square numbers in range and turn into list\n", 65 | "lst = [x**2 for x in range(0,11)]" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 4, 71 | "metadata": {}, 72 | "outputs": [ 73 | { 74 | "data": { 75 | "text/plain": [ 76 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]" 77 | ] 78 | }, 79 | "execution_count": 4, 80 | "metadata": {}, 81 | "output_type": "execute_result" 82 | } 83 | ], 84 | "source": [ 85 | "lst" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "## Example 3\n", 93 | "Let's see how to add in if statements:" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 5, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "# Check for even numbers in a range\n", 103 | "lst = [x for x in range(11) if x % 2 == 0]" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 6, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "data": { 113 | "text/plain": [ 114 | "[0, 2, 4, 6, 8, 10]" 115 | ] 116 | }, 117 | "execution_count": 6, 118 | "metadata": {}, 119 | "output_type": "execute_result" 120 | } 121 | ], 122 | "source": [ 123 | "lst" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "## Example 4\n", 131 | "Can also do more complicated arithmetic:" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 7, 137 | "metadata": {}, 138 | "outputs": [ 139 | { 140 | "data": { 141 | "text/plain": [ 142 | "[32.0, 50.0, 68.18, 94.1]" 143 | ] 144 | }, 145 | "execution_count": 7, 146 | "metadata": {}, 147 | "output_type": "execute_result" 148 | } 149 | ], 150 | "source": [ 151 | "# Convert Celsius to Fahrenheit\n", 152 | "celsius = [0,10,20.1,34.5]\n", 153 | "\n", 154 | "fahrenheit = [((9/5)*temp + 32) for temp in celsius ]\n", 155 | "\n", 156 | "fahrenheit" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "## Example 5\n", 164 | "We can also perform nested list comprehensions, for example:" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 8, 170 | "metadata": {}, 171 | "outputs": [ 172 | { 173 | "data": { 174 | "text/plain": [ 175 | "[0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]" 176 | ] 177 | }, 178 | "execution_count": 8, 179 | "metadata": {}, 180 | "output_type": "execute_result" 181 | } 182 | ], 183 | "source": [ 184 | "lst = [ x**2 for x in [x**2 for x in range(11)]]\n", 185 | "lst" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "Later on in the course we will learn about generator comprehensions. After this lecture you should feel comfortable reading and writing basic list comprehensions." 193 | ] 194 | } 195 | ], 196 | "metadata": { 197 | "kernelspec": { 198 | "display_name": "Python 3", 199 | "language": "python", 200 | "name": "python3" 201 | }, 202 | "language_info": { 203 | "codemirror_mode": { 204 | "name": "ipython", 205 | "version": 3 206 | }, 207 | "file_extension": ".py", 208 | "mimetype": "text/x-python", 209 | "name": "python", 210 | "nbconvert_exporter": "python", 211 | "pygments_lexer": "ipython3", 212 | "version": "3.6.2" 213 | } 214 | }, 215 | "nbformat": 4, 216 | "nbformat_minor": 1 217 | } 218 | -------------------------------------------------------------------------------- /02-Python Statements/07-Statements Assessment Test.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Statements Assessment Test\n", 8 | "Let's test your knowledge!" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "_____\n", 16 | "**Use for, .split(), and if to create a Statement that will print out words that start with 's':**" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "st = 'Print only the words that start with s in this sentence'" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "#Code here" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "______\n", 42 | "**Use range() to print all the even numbers from 0 to 10.**" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "#Code Here" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "___\n", 59 | "**Use a List Comprehension to create a list of all numbers between 1 and 50 that are divisible by 3.**" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "#Code in this cell\n", 69 | "[]" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "_____\n", 77 | "**Go through the string below and if the length of a word is even print \"even!\"**" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "st = 'Print every word in this sentence that has an even number of letters'" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "#Code in this cell" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "____\n", 103 | "**Write a program that prints the integers from 1 to 100. But for multiples of three print \"Fizz\" instead of the number, and for the multiples of five print \"Buzz\". For numbers which are multiples of both three and five print \"FizzBuzz\".**" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [ 112 | "#Code in this cell" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "____\n", 120 | "**Use List Comprehension to create a list of the first letters of every word in the string below:**" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [ 129 | "st = 'Create a list of the first letters of every word in this string'" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": null, 135 | "metadata": {}, 136 | "outputs": [], 137 | "source": [ 138 | "#Code in this cell" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": {}, 144 | "source": [ 145 | "### Great Job!" 146 | ] 147 | } 148 | ], 149 | "metadata": { 150 | "kernelspec": { 151 | "display_name": "Python 3", 152 | "language": "python", 153 | "name": "python3" 154 | }, 155 | "language_info": { 156 | "codemirror_mode": { 157 | "name": "ipython", 158 | "version": 2 159 | }, 160 | "file_extension": ".py", 161 | "mimetype": "text/x-python", 162 | "name": "python", 163 | "nbconvert_exporter": "python", 164 | "pygments_lexer": "ipython2", 165 | "version": "2.7.12" 166 | } 167 | }, 168 | "nbformat": 4, 169 | "nbformat_minor": 2 170 | } 171 | -------------------------------------------------------------------------------- /02-Python Statements/09-Guessing Game Challenge.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Guessing Game Challenge\n", 8 | "\n", 9 | "Let's use `while` loops to create a guessing game.\n", 10 | "\n", 11 | "The Challenge:\n", 12 | "\n", 13 | "Write a program that picks a random integer from 1 to 100, and has players guess the number. The rules are:\n", 14 | "\n", 15 | "1. If a player's guess is less than 1 or greater than 100, say \"OUT OF BOUNDS\"\n", 16 | "2. On a player's first turn, if their guess is\n", 17 | " * within 10 of the number, return \"WARM!\"\n", 18 | " * further than 10 away from the number, return \"COLD!\"\n", 19 | "3. On all subsequent turns, if a guess is \n", 20 | " * closer to the number than the previous guess return \"WARMER!\"\n", 21 | " * farther from the number than the previous guess, return \"COLDER!\"\n", 22 | "4. When the player's guess equals the number, tell them they've guessed correctly *and* how many guesses it took!\n", 23 | "\n", 24 | "You can try this from scratch, or follow the steps outlined below. A separate Solution notebook has been provided. Good luck!\n" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "#### First, pick a random integer from 1 to 100 using the random module and assign it to a variable\n", 32 | "\n", 33 | "Note: `random.randint(a,b)` returns a random integer in range `[a, b]`, including both end points." 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "#### Next, print an introduction to the game and explain the rules" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "#### Create a list to store guesses\n", 62 | "\n", 63 | "Hint: zero is a good placeholder value. It's useful because it evaluates to \"False\"" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "metadata": {}, 76 | "source": [ 77 | "#### Write a `while` loop that asks for a valid guess. Test it a few times to make sure it works." 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "while True:\n", 87 | " \n", 88 | " pass" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "#### Write a `while` loop that compares the player's guess to our number. If the player guesses correctly, break from the loop. Otherwise, tell the player if they're warmer or colder, and continue asking for guesses.\n", 96 | "\n", 97 | "Some hints:\n", 98 | "* it may help to sketch out all possible combinations on paper first!\n", 99 | "* you can use the `abs()` function to find the positive difference between two numbers\n", 100 | "* if you append all new guesses to the list, then the previous guess is given as `guesses[-2]`" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": null, 106 | "metadata": {}, 107 | "outputs": [], 108 | "source": [ 109 | "while True:\n", 110 | "\n", 111 | " # we can copy the code from above to take an input\n", 112 | "\n", 113 | " pass" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "That's it! You've just programmed your first game!\n", 121 | "\n", 122 | "In the next section we'll learn how to turn some of these repetitive actions into *functions* that can be called whenever we need them." 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "### Good Job!" 130 | ] 131 | } 132 | ], 133 | "metadata": { 134 | "kernelspec": { 135 | "display_name": "Python 3", 136 | "language": "python", 137 | "name": "python3" 138 | }, 139 | "language_info": { 140 | "codemirror_mode": { 141 | "name": "ipython", 142 | "version": 2 143 | }, 144 | "file_extension": ".py", 145 | "mimetype": "text/x-python", 146 | "name": "python", 147 | "nbconvert_exporter": "python", 148 | "pygments_lexer": "ipython2", 149 | "version": "2.7.12" 150 | } 151 | }, 152 | "nbformat": 4, 153 | "nbformat_minor": 2 154 | } 155 | -------------------------------------------------------------------------------- /02-Python Statements/Assignment/07-Statements Assessment Test.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Statements Assessment Test\n", 8 | "Let's test your knowledge!" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "_____\n", 16 | "**Use for, .split(), and if to create a Statement that will print out words that start with 's':**" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "st = 'Print only the words that start with s in this sentence'" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "#Code here" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "______\n", 42 | "**Use range() to print all the even numbers from 0 to 10.**" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "#Code Here" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "___\n", 59 | "**Use a List Comprehension to create a list of all numbers between 1 and 50 that are divisible by 3.**" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "#Code in this cell\n", 69 | "[]" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "_____\n", 77 | "**Go through the string below and if the length of a word is even print \"even!\"**" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "st = 'Print every word in this sentence that has an even number of letters'" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "#Code in this cell" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "____\n", 103 | "**Write a program that prints the integers from 1 to 100. But for multiples of three print \"Fizz\" instead of the number, and for the multiples of five print \"Buzz\". For numbers which are multiples of both three and five print \"FizzBuzz\".**" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [ 112 | "#Code in this cell" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "____\n", 120 | "**Use List Comprehension to create a list of the first letters of every word in the string below:**" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [ 129 | "st = 'Create a list of the first letters of every word in this string'" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": null, 135 | "metadata": {}, 136 | "outputs": [], 137 | "source": [ 138 | "#Code in this cell" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": {}, 144 | "source": [ 145 | "### Great Job!" 146 | ] 147 | } 148 | ], 149 | "metadata": { 150 | "kernelspec": { 151 | "display_name": "Python 3", 152 | "language": "python", 153 | "name": "python3" 154 | }, 155 | "language_info": { 156 | "codemirror_mode": { 157 | "name": "ipython", 158 | "version": 2 159 | }, 160 | "file_extension": ".py", 161 | "mimetype": "text/x-python", 162 | "name": "python", 163 | "nbconvert_exporter": "python", 164 | "pygments_lexer": "ipython2", 165 | "version": "2.7.12" 166 | } 167 | }, 168 | "nbformat": 4, 169 | "nbformat_minor": 2 170 | } 171 | -------------------------------------------------------------------------------- /02-Python Statements/Assignment/08-Statements Assessment Test - Solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Statements Assessment Solutions" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "_____\n", 15 | "**Use for, .split(), and if to create a Statement that will print out words that start with 's':**" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 1, 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "st = 'Print only the words that start with s in this sentence'" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 2, 30 | "metadata": {}, 31 | "outputs": [ 32 | { 33 | "data": { 34 | "text/plain": [ 35 | "['Print',\n", 36 | " 'only',\n", 37 | " 'the',\n", 38 | " 'words',\n", 39 | " 'that',\n", 40 | " 'start',\n", 41 | " 'with',\n", 42 | " 's',\n", 43 | " 'in',\n", 44 | " 'this',\n", 45 | " 'sentence']" 46 | ] 47 | }, 48 | "execution_count": 2, 49 | "metadata": {}, 50 | "output_type": "execute_result" 51 | } 52 | ], 53 | "source": [ 54 | "st.split()" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 2, 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "start\n", 67 | "s\n", 68 | "sentence\n" 69 | ] 70 | } 71 | ], 72 | "source": [ 73 | "for word in st.split():\n", 74 | " if word[0] == 's':\n", 75 | " print(word)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "______\n", 83 | "**Use range() to print all the even numbers from 0 to 10.**" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 3, 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "data": { 93 | "text/plain": [ 94 | "[0, 2, 4, 6, 8, 10]" 95 | ] 96 | }, 97 | "execution_count": 3, 98 | "metadata": {}, 99 | "output_type": "execute_result" 100 | } 101 | ], 102 | "source": [ 103 | "list(range(0,11,2))" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "___\n", 111 | "**Use List comprehension to create a list of all numbers between 1 and 50 that are divisible by 3.**" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 3, 117 | "metadata": {}, 118 | "outputs": [ 119 | { 120 | "name": "stdout", 121 | "output_type": "stream", 122 | "text": [ 123 | "3\n", 124 | "6\n", 125 | "9\n", 126 | "12\n", 127 | "15\n", 128 | "18\n", 129 | "21\n", 130 | "24\n", 131 | "27\n", 132 | "30\n", 133 | "33\n", 134 | "36\n", 135 | "39\n", 136 | "42\n", 137 | "45\n", 138 | "48\n" 139 | ] 140 | } 141 | ], 142 | "source": [ 143 | "for x in range(1,51):\n", 144 | " if x%3 == 0:\n", 145 | " print(x)" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 4, 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "data": { 155 | "text/plain": [ 156 | "[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48]" 157 | ] 158 | }, 159 | "execution_count": 4, 160 | "metadata": {}, 161 | "output_type": "execute_result" 162 | } 163 | ], 164 | "source": [ 165 | "[x for x in range(1,51) if x%3 == 0]" 166 | ] 167 | }, 168 | { 169 | "cell_type": "markdown", 170 | "metadata": {}, 171 | "source": [ 172 | "_____\n", 173 | "**Go through the string below and if the length of a word is even print \"even!\"**" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 5, 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [ 182 | "st = 'Print every word in this sentence that has an even number of letters'" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 6, 188 | "metadata": {}, 189 | "outputs": [ 190 | { 191 | "name": "stdout", 192 | "output_type": "stream", 193 | "text": [ 194 | "word <-- has an even length!\n", 195 | "in <-- has an even length!\n", 196 | "this <-- has an even length!\n", 197 | "sentence <-- has an even length!\n", 198 | "that <-- has an even length!\n", 199 | "an <-- has an even length!\n", 200 | "even <-- has an even length!\n", 201 | "number <-- has an even length!\n", 202 | "of <-- has an even length!\n" 203 | ] 204 | } 205 | ], 206 | "source": [ 207 | "for word in st.split():\n", 208 | " if len(word)%2 == 0:\n", 209 | " print(word+\" <-- has an even length!\")" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": {}, 215 | "source": [ 216 | "____\n", 217 | "**Write a program that prints the integers from 1 to 100. But for multiples of three print \"Fizz\" instead of the number, and for the multiples of five print \"Buzz\". For numbers which are multiples of both three and five print \"FizzBuzz\".**" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": null, 223 | "metadata": {}, 224 | "outputs": [], 225 | "source": [ 226 | "for num in range(1,101):\n", 227 | " if num % 3 == 0 and num % 5 == 0:\n", 228 | " print(\"FizzBuzz\")\n", 229 | " elif num % 3 == 0:\n", 230 | " print(\"Fizz\")\n", 231 | " elif num % 5 == 0:\n", 232 | " print(\"Buzz\")\n", 233 | " else:\n", 234 | " print(num)" 235 | ] 236 | }, 237 | { 238 | "cell_type": "markdown", 239 | "metadata": {}, 240 | "source": [ 241 | "____\n", 242 | "**Use a List Comprehension to create a list of the first letters of every word in the string below:**" 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "execution_count": 7, 248 | "metadata": {}, 249 | "outputs": [], 250 | "source": [ 251 | "st = 'Create a list of the first letters of every word in this string'" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": 8, 257 | "metadata": {}, 258 | "outputs": [ 259 | { 260 | "data": { 261 | "text/plain": [ 262 | "['C', 'a', 'l', 'o', 't', 'f', 'l', 'o', 'e', 'w', 'i', 't', 's']" 263 | ] 264 | }, 265 | "execution_count": 8, 266 | "metadata": {}, 267 | "output_type": "execute_result" 268 | } 269 | ], 270 | "source": [ 271 | "[word[0] for word in st.split()]" 272 | ] 273 | }, 274 | { 275 | "cell_type": "markdown", 276 | "metadata": {}, 277 | "source": [ 278 | "### Great Job!" 279 | ] 280 | } 281 | ], 282 | "metadata": { 283 | "kernelspec": { 284 | "display_name": "Python 3", 285 | "language": "python", 286 | "name": "python3" 287 | }, 288 | "language_info": { 289 | "codemirror_mode": { 290 | "name": "ipython", 291 | "version": 2 292 | }, 293 | "file_extension": ".py", 294 | "mimetype": "text/x-python", 295 | "name": "python", 296 | "nbconvert_exporter": "python", 297 | "pygments_lexer": "ipython2", 298 | "version": "2.7.12" 299 | } 300 | }, 301 | "nbformat": 4, 302 | "nbformat_minor": 2 303 | } 304 | -------------------------------------------------------------------------------- /02-Python Statements/Assignment/09-Guessing Game Challenge.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Guessing Game Challenge\n", 8 | "\n", 9 | "Let's use `while` loops to create a guessing game.\n", 10 | "\n", 11 | "The Challenge:\n", 12 | "\n", 13 | "Write a program that picks a random integer from 1 to 100, and has players guess the number. The rules are:\n", 14 | "\n", 15 | "1. If a player's guess is less than 1 or greater than 100, say \"OUT OF BOUNDS\"\n", 16 | "2. On a player's first turn, if their guess is\n", 17 | " * within 10 of the number, return \"WARM!\"\n", 18 | " * further than 10 away from the number, return \"COLD!\"\n", 19 | "3. On all subsequent turns, if a guess is \n", 20 | " * closer to the number than the previous guess return \"WARMER!\"\n", 21 | " * farther from the number than the previous guess, return \"COLDER!\"\n", 22 | "4. When the player's guess equals the number, tell them they've guessed correctly *and* how many guesses it took!\n", 23 | "\n", 24 | "You can try this from scratch, or follow the steps outlined below. A separate Solution notebook has been provided. Good luck!\n" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "#### First, pick a random integer from 1 to 100 using the random module and assign it to a variable\n", 32 | "\n", 33 | "Note: `random.randint(a,b)` returns a random integer in range `[a, b]`, including both end points." 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "#### Next, print an introduction to the game and explain the rules" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "#### Create a list to store guesses\n", 62 | "\n", 63 | "Hint: zero is a good placeholder value. It's useful because it evaluates to \"False\"" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "metadata": {}, 76 | "source": [ 77 | "#### Write a `while` loop that asks for a valid guess. Test it a few times to make sure it works." 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "while True:\n", 87 | " \n", 88 | " pass" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "#### Write a `while` loop that compares the player's guess to our number. If the player guesses correctly, break from the loop. Otherwise, tell the player if they're warmer or colder, and continue asking for guesses.\n", 96 | "\n", 97 | "Some hints:\n", 98 | "* it may help to sketch out all possible combinations on paper first!\n", 99 | "* you can use the `abs()` function to find the positive difference between two numbers\n", 100 | "* if you append all new guesses to the list, then the previous guess is given as `guesses[-2]`" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": null, 106 | "metadata": {}, 107 | "outputs": [], 108 | "source": [ 109 | "while True:\n", 110 | "\n", 111 | " # we can copy the code from above to take an input\n", 112 | "\n", 113 | " pass" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "That's it! You've just programmed your first game!\n", 121 | "\n", 122 | "In the next section we'll learn how to turn some of these repetitive actions into *functions* that can be called whenever we need them." 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "### Good Job!" 130 | ] 131 | } 132 | ], 133 | "metadata": { 134 | "kernelspec": { 135 | "display_name": "Python 3", 136 | "language": "python", 137 | "name": "python3" 138 | }, 139 | "language_info": { 140 | "codemirror_mode": { 141 | "name": "ipython", 142 | "version": 2 143 | }, 144 | "file_extension": ".py", 145 | "mimetype": "text/x-python", 146 | "name": "python", 147 | "nbconvert_exporter": "python", 148 | "pygments_lexer": "ipython2", 149 | "version": "2.7.12" 150 | } 151 | }, 152 | "nbformat": 4, 153 | "nbformat_minor": 2 154 | } 155 | -------------------------------------------------------------------------------- /02-Python Statements/Assignment/10-Guessing Game Challenge - Solution.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Guessing Game Challenge - Solution\n", 8 | "\n", 9 | "Let's use `while` loops to create a guessing game.\n", 10 | "\n", 11 | "The Challenge:\n", 12 | "\n", 13 | "Write a program that picks a random integer from 1 to 100, and has players guess the number. The rules are:\n", 14 | "\n", 15 | "1. If a player's guess is less than 1 or greater than 100, say \"OUT OF BOUNDS\"\n", 16 | "2. On a player's first turn, if their guess is\n", 17 | " * within 10 of the number, return \"WARM!\"\n", 18 | " * further than 10 away from the number, return \"COLD!\"\n", 19 | "3. On all subsequent turns, if a guess is \n", 20 | " * closer to the number than the previous guess return \"WARMER!\"\n", 21 | " * farther from the number than the previous guess, return \"COLDER!\"\n", 22 | "4. When the player's guess equals the number, tell them they've guessed correctly *and* how many guesses it took!\n" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "#### First, pick a random integer from 1 to 100 using the random module and assign it to a variable\n", 30 | "\n", 31 | "Note: `random.randint(a,b)` returns a random integer in range `[a, b]`, including both end points." 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 1, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "import random\n", 41 | "\n", 42 | "num = random.randint(1,100)" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "#### Next, print an introduction to the game and explain the rules" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 2, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "name": "stdout", 59 | "output_type": "stream", 60 | "text": [ 61 | "WELCOME TO GUESS ME!\n", 62 | "I'm thinking of a number between 1 and 100\n", 63 | "If your guess is more than 10 away from my number, I'll tell you you're COLD\n", 64 | "If your guess is within 10 of my number, I'll tell you you're WARM\n", 65 | "If your guess is farther than your most recent guess, I'll say you're getting COLDER\n", 66 | "If your guess is closer than your most recent guess, I'll say you're getting WARMER\n", 67 | "LET'S PLAY!\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "print(\"WELCOME TO GUESS ME!\")\n", 73 | "print(\"I'm thinking of a number between 1 and 100\")\n", 74 | "print(\"If your guess is more than 10 away from my number, I'll tell you you're COLD\")\n", 75 | "print(\"If your guess is within 10 of my number, I'll tell you you're WARM\")\n", 76 | "print(\"If your guess is farther than your most recent guess, I'll say you're getting COLDER\")\n", 77 | "print(\"If your guess is closer than your most recent guess, I'll say you're getting WARMER\")\n", 78 | "print(\"LET'S PLAY!\")" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "#### Create a list to store guesses\n", 86 | "\n", 87 | "Hint: zero is a good placeholder value. It's useful because it evaluates to \"False\"" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 3, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "guesses = [0]" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | "#### Write a `while` loop that asks for a valid guess. Test it a few times to make sure it works." 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 4, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "name": "stdout", 113 | "output_type": "stream", 114 | "text": [ 115 | "I'm thinking of a number between 1 and 100.\n", 116 | " What is your guess? 500\n", 117 | "OUT OF BOUNDS! Please try again: \n", 118 | "I'm thinking of a number between 1 and 100.\n", 119 | " What is your guess? 50\n" 120 | ] 121 | } 122 | ], 123 | "source": [ 124 | "while True:\n", 125 | " \n", 126 | " guess = int(input(\"I'm thinking of a number between 1 and 100.\\n What is your guess? \"))\n", 127 | " \n", 128 | " if guess < 1 or guess > 100:\n", 129 | " print('OUT OF BOUNDS! Please try again: ')\n", 130 | " continue\n", 131 | " \n", 132 | " break" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "#### Write a `while` loop that compares the player's guess to our number. If the player guesses correctly, break from the loop. Otherwise, tell the player if they're warmer or colder, and continue asking for guesses.\n", 140 | "\n", 141 | "Some hints:\n", 142 | "* it may help to sketch out all possible combinations on paper first!\n", 143 | "* you can use the `abs()` function to find the positive difference between two numbers\n", 144 | "* if you append all new guesses to the list, then the previous guess is given as `guesses[-2]`" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 5, 150 | "metadata": {}, 151 | "outputs": [ 152 | { 153 | "name": "stdout", 154 | "output_type": "stream", 155 | "text": [ 156 | "I'm thinking of a number between 1 and 100.\n", 157 | " What is your guess? 50\n", 158 | "COLD!\n", 159 | "I'm thinking of a number between 1 and 100.\n", 160 | " What is your guess? 75\n", 161 | "WARMER!\n", 162 | "I'm thinking of a number between 1 and 100.\n", 163 | " What is your guess? 85\n", 164 | "WARMER!\n", 165 | "I'm thinking of a number between 1 and 100.\n", 166 | " What is your guess? 92\n", 167 | "COLDER!\n", 168 | "I'm thinking of a number between 1 and 100.\n", 169 | " What is your guess? 80\n", 170 | "WARMER!\n", 171 | "I'm thinking of a number between 1 and 100.\n", 172 | " What is your guess? 78\n", 173 | "COLDER!\n", 174 | "I'm thinking of a number between 1 and 100.\n", 175 | " What is your guess? 82\n", 176 | "WARMER!\n", 177 | "I'm thinking of a number between 1 and 100.\n", 178 | " What is your guess? 83\n", 179 | "COLDER!\n", 180 | "I'm thinking of a number between 1 and 100.\n", 181 | " What is your guess? 81\n", 182 | "CONGRATULATIONS, YOU GUESSED IT IN ONLY 9 GUESSES!!\n" 183 | ] 184 | } 185 | ], 186 | "source": [ 187 | "while True:\n", 188 | "\n", 189 | " # we can copy the code from above to take an input\n", 190 | " guess = int(input(\"I'm thinking of a number between 1 and 100.\\n What is your guess? \"))\n", 191 | " \n", 192 | " if guess < 1 or guess > 100:\n", 193 | " print('OUT OF BOUNDS! Please try again: ')\n", 194 | " continue\n", 195 | " \n", 196 | " # here we compare the player's guess to our number\n", 197 | " if guess == num:\n", 198 | " print(f'CONGRATULATIONS, YOU GUESSED IT IN ONLY {len(guesses)} GUESSES!!')\n", 199 | " break\n", 200 | " \n", 201 | " # if guess is incorrect, add guess to the list\n", 202 | " guesses.append(guess)\n", 203 | " \n", 204 | " # when testing the first guess, guesses[-2]==0, which evaluates to False\n", 205 | " # and brings us down to the second section\n", 206 | " \n", 207 | " if guesses[-2]: \n", 208 | " if abs(num-guess) < abs(num-guesses[-2]):\n", 209 | " print('WARMER!')\n", 210 | " else:\n", 211 | " print('COLDER!')\n", 212 | " \n", 213 | " else:\n", 214 | " if abs(num-guess) <= 10:\n", 215 | " print('WARM!')\n", 216 | " else:\n", 217 | " print('COLD!')" 218 | ] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": {}, 223 | "source": [ 224 | "That's it! You've just programmed your first game!\n", 225 | "\n", 226 | "In the next section we'll learn how to turn some of these repetitive actions into *functions* that can be called whenever we need them." 227 | ] 228 | }, 229 | { 230 | "cell_type": "markdown", 231 | "metadata": {}, 232 | "source": [ 233 | "### Good Job!" 234 | ] 235 | } 236 | ], 237 | "metadata": { 238 | "kernelspec": { 239 | "display_name": "Python 3", 240 | "language": "python", 241 | "name": "python3" 242 | }, 243 | "language_info": { 244 | "codemirror_mode": { 245 | "name": "ipython", 246 | "version": 2 247 | }, 248 | "file_extension": ".py", 249 | "mimetype": "text/x-python", 250 | "name": "python", 251 | "nbconvert_exporter": "python", 252 | "pygments_lexer": "ipython2", 253 | "version": "2.7.12" 254 | } 255 | }, 256 | "nbformat": 4, 257 | "nbformat_minor": 2 258 | } 259 | -------------------------------------------------------------------------------- /03-Methods and Functions/.ipynb_checkpoints/01-Methods-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Methods\n", 8 | "\n", 9 | "We've already seen a few example of methods when learning about Object and Data Structure Types in Python. Methods are essentially functions built into objects. Later on in the course we will learn about how to create our own objects and methods using Object Oriented Programming (OOP) and classes.\n", 10 | "\n", 11 | "Methods perform specific actions on an object and can also take arguments, just like a function. This lecture will serve as just a brief introduction to methods and get you thinking about overall design methods that we will touch back upon when we reach OOP in the course.\n", 12 | "\n", 13 | "Methods are in the form:\n", 14 | "\n", 15 | " object.method(arg1,arg2,etc...)\n", 16 | " \n", 17 | "You'll later see that we can think of methods as having an argument 'self' referring to the object itself. You can't see this argument but we will be using it later on in the course during the OOP lectures.\n", 18 | "\n", 19 | "Let's take a quick look at what an example of the various methods a list has:" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 1, 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "# Create a simple list\n", 29 | "lst = [1,2,3,4,5]" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "Fortunately, with iPython and the Jupyter Notebook we can quickly see all the possible methods using the tab key. The methods for a list are:\n", 37 | "\n", 38 | "* append\n", 39 | "* count\n", 40 | "* extend\n", 41 | "* insert\n", 42 | "* pop\n", 43 | "* remove\n", 44 | "* reverse\n", 45 | "* sort\n", 46 | "\n", 47 | "Let's try out a few of them:" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "append() allows us to add elements to the end of a list:" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 2, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "lst.append(6)" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 3, 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "data": { 73 | "text/plain": [ 74 | "[1, 2, 3, 4, 5, 6]" 75 | ] 76 | }, 77 | "execution_count": 3, 78 | "metadata": {}, 79 | "output_type": "execute_result" 80 | } 81 | ], 82 | "source": [ 83 | "lst" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "Great! Now how about count()? The count() method will count the number of occurrences of an element in a list." 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 4, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "1" 102 | ] 103 | }, 104 | "execution_count": 4, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "# Check how many times 2 shows up in the list\n", 111 | "lst.count(2)" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "You can always use Shift+Tab in the Jupyter Notebook to get more help about the method. In general Python you can use the help() function: " 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 5, 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "name": "stdout", 128 | "output_type": "stream", 129 | "text": [ 130 | "Help on built-in function count:\n", 131 | "\n", 132 | "count(...) method of builtins.list instance\n", 133 | " L.count(value) -> integer -- return number of occurrences of value\n", 134 | "\n" 135 | ] 136 | } 137 | ], 138 | "source": [ 139 | "help(lst.count)" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "Feel free to play around with the rest of the methods for a list. Later on in this section your quiz will involve using help and Google searching for methods of different types of objects!" 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "metadata": {}, 152 | "source": [ 153 | "Great! By this lecture you should feel comfortable calling methods of objects in Python!" 154 | ] 155 | } 156 | ], 157 | "metadata": { 158 | "kernelspec": { 159 | "display_name": "Python 3", 160 | "language": "python", 161 | "name": "python3" 162 | }, 163 | "language_info": { 164 | "codemirror_mode": { 165 | "name": "ipython", 166 | "version": 3 167 | }, 168 | "file_extension": ".py", 169 | "mimetype": "text/x-python", 170 | "name": "python", 171 | "nbconvert_exporter": "python", 172 | "pygments_lexer": "ipython3", 173 | "version": "3.6.2" 174 | } 175 | }, 176 | "nbformat": 4, 177 | "nbformat_minor": 1 178 | } 179 | -------------------------------------------------------------------------------- /03-Methods and Functions/.ipynb_checkpoints/07-args and kwargs-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# `*args` and `**kwargs`\n", 8 | "\n", 9 | "Work with Python long enough, and eventually you will encounter `*args` and `**kwargs`. These strange terms show up as parameters in function definitions. What do they do? Let's review a simple function:" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "5.0" 21 | ] 22 | }, 23 | "execution_count": 1, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "def myfunc(a,b):\n", 30 | " return sum((a,b))*.05\n", 31 | "\n", 32 | "myfunc(40,60)" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "This function returns 5% of the sum of **a** and **b**. In this example, **a** and **b** are *positional* arguments; that is, 40 is assigned to **a** because it is the first argument, and 60 to **b**. Notice also that to work with multiple positional arguments in the `sum()` function we had to pass them in as a tuple.\n", 40 | "\n", 41 | "What if we want to work with more than two numbers? One way would be to assign a *lot* of parameters, and give each one a default value." 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 2, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "data": { 51 | "text/plain": [ 52 | "6.0" 53 | ] 54 | }, 55 | "execution_count": 2, 56 | "metadata": {}, 57 | "output_type": "execute_result" 58 | } 59 | ], 60 | "source": [ 61 | "def myfunc(a=0,b=0,c=0,d=0,e=0):\n", 62 | " return sum((a,b,c,d,e))*.05\n", 63 | "\n", 64 | "myfunc(40,60,20)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "Obviously this is not a very efficient solution, and that's where `*args` comes in.\n", 72 | "\n", 73 | "## `*args`\n", 74 | "\n", 75 | "When a function parameter starts with an asterisk, it allows for an *arbitrary number* of arguments, and the function takes them in as a tuple of values. Rewriting the above function:" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 3, 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "data": { 85 | "text/plain": [ 86 | "6.0" 87 | ] 88 | }, 89 | "execution_count": 3, 90 | "metadata": {}, 91 | "output_type": "execute_result" 92 | } 93 | ], 94 | "source": [ 95 | "def myfunc(*args):\n", 96 | " return sum(args)*.05\n", 97 | "\n", 98 | "myfunc(40,60,20)" 99 | ] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "metadata": {}, 104 | "source": [ 105 | "Notice how passing the keyword \"args\" into the `sum()` function did the same thing as a tuple of arguments.\n", 106 | "\n", 107 | "It is worth noting that the word \"args\" is itself arbitrary - any word will do so long as it's preceded by an asterisk. To demonstrate this:" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 4, 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "data": { 117 | "text/plain": [ 118 | "6.0" 119 | ] 120 | }, 121 | "execution_count": 4, 122 | "metadata": {}, 123 | "output_type": "execute_result" 124 | } 125 | ], 126 | "source": [ 127 | "def myfunc(*spam):\n", 128 | " return sum(spam)*.05\n", 129 | "\n", 130 | "myfunc(40,60,20)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "## `**kwargs`\n", 138 | "\n", 139 | "Similarly, Python offers a way to handle arbitrary numbers of *keyworded* arguments. Instead of creating a tuple of values, `**kwargs` builds a dictionary of key/value pairs. For example:" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 5, 145 | "metadata": {}, 146 | "outputs": [ 147 | { 148 | "name": "stdout", 149 | "output_type": "stream", 150 | "text": [ 151 | "My favorite fruit is pineapple\n" 152 | ] 153 | } 154 | ], 155 | "source": [ 156 | "def myfunc(**kwargs):\n", 157 | " if 'fruit' in kwargs:\n", 158 | " print(f\"My favorite fruit is {kwargs['fruit']}\") # review String Formatting and f-strings if this syntax is unfamiliar\n", 159 | " else:\n", 160 | " print(\"I don't like fruit\")\n", 161 | " \n", 162 | "myfunc(fruit='pineapple')" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 6, 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "I don't like fruit\n" 175 | ] 176 | } 177 | ], 178 | "source": [ 179 | "myfunc()" 180 | ] 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "metadata": {}, 185 | "source": [ 186 | "## `*args` and `**kwargs` combined\n", 187 | "\n", 188 | "You can pass `*args` and `**kwargs` into the same function, but `*args` have to appear before `**kwargs`" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 7, 194 | "metadata": {}, 195 | "outputs": [ 196 | { 197 | "name": "stdout", 198 | "output_type": "stream", 199 | "text": [ 200 | "I like eggs and spam and my favorite fruit is cherries\n", 201 | "May I have some orange juice?\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "def myfunc(*args, **kwargs):\n", 207 | " if 'fruit' and 'juice' in kwargs:\n", 208 | " print(f\"I like {' and '.join(args)} and my favorite fruit is {kwargs['fruit']}\")\n", 209 | " print(f\"May I have some {kwargs['juice']} juice?\")\n", 210 | " else:\n", 211 | " pass\n", 212 | " \n", 213 | "myfunc('eggs','spam',fruit='cherries',juice='orange')" 214 | ] 215 | }, 216 | { 217 | "cell_type": "markdown", 218 | "metadata": {}, 219 | "source": [ 220 | "Placing keyworded arguments ahead of positional arguments raises an exception:" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 8, 226 | "metadata": {}, 227 | "outputs": [ 228 | { 229 | "ename": "SyntaxError", 230 | "evalue": "positional argument follows keyword argument (, line 1)", 231 | "output_type": "error", 232 | "traceback": [ 233 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m myfunc(fruit='cherries',juice='orange','eggs','spam')\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n" 234 | ] 235 | } 236 | ], 237 | "source": [ 238 | "myfunc(fruit='cherries',juice='orange','eggs','spam')" 239 | ] 240 | }, 241 | { 242 | "cell_type": "markdown", 243 | "metadata": {}, 244 | "source": [ 245 | "As with \"args\", you can use any name you'd like for keyworded arguments - \"kwargs\" is just a popular convention.\n", 246 | "\n", 247 | "That's it! Now you should understand how `*args` and `**kwargs` provide the flexibilty to work with arbitrary numbers of arguments!" 248 | ] 249 | } 250 | ], 251 | "metadata": { 252 | "kernelspec": { 253 | "display_name": "Python 3", 254 | "language": "python", 255 | "name": "python3" 256 | }, 257 | "language_info": { 258 | "codemirror_mode": { 259 | "name": "ipython", 260 | "version": 3 261 | }, 262 | "file_extension": ".py", 263 | "mimetype": "text/x-python", 264 | "name": "python", 265 | "nbconvert_exporter": "python", 266 | "pygments_lexer": "ipython3", 267 | "version": "3.6.2" 268 | } 269 | }, 270 | "nbformat": 4, 271 | "nbformat_minor": 2 272 | } 273 | -------------------------------------------------------------------------------- /03-Methods and Functions/01-Methods.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Methods\n", 8 | "\n", 9 | "We've already seen a few example of methods when learning about Object and Data Structure Types in Python. Methods are essentially functions built into objects. Later on in the course we will learn about how to create our own objects and methods using Object Oriented Programming (OOP) and classes.\n", 10 | "\n", 11 | "Methods perform specific actions on an object and can also take arguments, just like a function. This lecture will serve as just a brief introduction to methods and get you thinking about overall design methods that we will touch back upon when we reach OOP in the course.\n", 12 | "\n", 13 | "Methods are in the form:\n", 14 | "\n", 15 | " object.method(arg1,arg2,etc...)\n", 16 | " \n", 17 | "You'll later see that we can think of methods as having an argument 'self' referring to the object itself. You can't see this argument but we will be using it later on in the course during the OOP lectures.\n", 18 | "\n", 19 | "Let's take a quick look at what an example of the various methods a list has:" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 4, 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "# Create a simple list\n", 29 | "lst = [1,2,3,4,5]" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 5, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "lst.append(1)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 6, 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "data": { 48 | "text/plain": [ 49 | "[1, 2, 3, 4, 5, 1]" 50 | ] 51 | }, 52 | "execution_count": 6, 53 | "metadata": {}, 54 | "output_type": "execute_result" 55 | } 56 | ], 57 | "source": [ 58 | "lst" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "Fortunately, with iPython and the Jupyter Notebook we can quickly see all the possible methods using the tab key. The methods for a list are:\n", 66 | "\n", 67 | "* append\n", 68 | "* count\n", 69 | "* extend\n", 70 | "* insert\n", 71 | "* pop\n", 72 | "* remove\n", 73 | "* reverse\n", 74 | "* sort\n", 75 | "\n", 76 | "Let's try out a few of them:" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "append() allows us to add elements to the end of a list:" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 2, 89 | "metadata": {}, 90 | "outputs": [], 91 | "source": [ 92 | "lst.append(6)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 3, 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "data": { 102 | "text/plain": [ 103 | "[1, 2, 3, 4, 5, 6]" 104 | ] 105 | }, 106 | "execution_count": 3, 107 | "metadata": {}, 108 | "output_type": "execute_result" 109 | } 110 | ], 111 | "source": [ 112 | "lst" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "Great! Now how about count()? The count() method will count the number of occurrences of an element in a list." 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 4, 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "data": { 129 | "text/plain": [ 130 | "1" 131 | ] 132 | }, 133 | "execution_count": 4, 134 | "metadata": {}, 135 | "output_type": "execute_result" 136 | } 137 | ], 138 | "source": [ 139 | "# Check how many times 2 shows up in the list\n", 140 | "lst.count(2)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": {}, 146 | "source": [ 147 | "You can always use Shift+Tab in the Jupyter Notebook to get more help about the method. In general Python you can use the help() function: " 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 9, 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "name": "stdout", 157 | "output_type": "stream", 158 | "text": [ 159 | "Help on built-in function insert:\n", 160 | "\n", 161 | "insert(...)\n", 162 | " L.insert(index, object) -- insert object before index\n", 163 | "\n" 164 | ] 165 | } 166 | ], 167 | "source": [ 168 | "help(lst.insert)" 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": {}, 174 | "source": [ 175 | "Feel free to play around with the rest of the methods for a list. Later on in this section your quiz will involve using help and Google searching for methods of different types of objects!" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "Great! By this lecture you should feel comfortable calling methods of objects in Python!" 183 | ] 184 | } 185 | ], 186 | "metadata": { 187 | "kernelspec": { 188 | "display_name": "Python 3", 189 | "language": "python", 190 | "name": "python3" 191 | }, 192 | "language_info": { 193 | "codemirror_mode": { 194 | "name": "ipython", 195 | "version": 2 196 | }, 197 | "file_extension": ".py", 198 | "mimetype": "text/x-python", 199 | "name": "python", 200 | "nbconvert_exporter": "python", 201 | "pygments_lexer": "ipython2", 202 | "version": "2.7.12" 203 | } 204 | }, 205 | "nbformat": 4, 206 | "nbformat_minor": 2 207 | } 208 | -------------------------------------------------------------------------------- /03-Methods and Functions/07-args and kwargs.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# `*args` and `**kwargs`\n", 8 | "\n", 9 | "Work with Python long enough, and eventually you will encounter `*args` and `**kwargs`. These strange terms show up as parameters in function definitions. What do they do? Let's review a simple function:" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "5.0" 21 | ] 22 | }, 23 | "execution_count": 1, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "def myfunc(a,b):\n", 30 | " return sum((a,b))*.05\n", 31 | "\n", 32 | "myfunc(40,60)" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "This function returns 5% of the sum of **a** and **b**. In this example, **a** and **b** are *positional* arguments; that is, 40 is assigned to **a** because it is the first argument, and 60 to **b**. Notice also that to work with multiple positional arguments in the `sum()` function we had to pass them in as a tuple.\n", 40 | "\n", 41 | "What if we want to work with more than two numbers? One way would be to assign a *lot* of parameters, and give each one a default value." 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 2, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "data": { 51 | "text/plain": [ 52 | "6.0" 53 | ] 54 | }, 55 | "execution_count": 2, 56 | "metadata": {}, 57 | "output_type": "execute_result" 58 | } 59 | ], 60 | "source": [ 61 | "def myfunc(a=0,b=0,c=0,d=0,e=0):\n", 62 | " return sum((a,b,c,d,e))*.05\n", 63 | "\n", 64 | "myfunc(40,60,20)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "Obviously this is not a very efficient solution, and that's where `*args` comes in.\n", 72 | "\n", 73 | "## `*args`\n", 74 | "\n", 75 | "When a function parameter starts with an asterisk, it allows for an *arbitrary number* of arguments, and the function takes them in as a tuple of values. Rewriting the above function:" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 3, 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "data": { 85 | "text/plain": [ 86 | "6.0" 87 | ] 88 | }, 89 | "execution_count": 3, 90 | "metadata": {}, 91 | "output_type": "execute_result" 92 | } 93 | ], 94 | "source": [ 95 | "def myfunc(*args):\n", 96 | " return sum(args)*.05\n", 97 | "\n", 98 | "myfunc(40,60,20)" 99 | ] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "metadata": {}, 104 | "source": [ 105 | "Notice how passing the keyword \"args\" into the `sum()` function did the same thing as a tuple of arguments.\n", 106 | "\n", 107 | "It is worth noting that the word \"args\" is itself arbitrary - any word will do so long as it's preceded by an asterisk. To demonstrate this:" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 4, 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "data": { 117 | "text/plain": [ 118 | "6.0" 119 | ] 120 | }, 121 | "execution_count": 4, 122 | "metadata": {}, 123 | "output_type": "execute_result" 124 | } 125 | ], 126 | "source": [ 127 | "def myfunc(*spam):\n", 128 | " return sum(spam)*.05\n", 129 | "\n", 130 | "myfunc(40,60,20)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "## `**kwargs`\n", 138 | "\n", 139 | "Similarly, Python offers a way to handle arbitrary numbers of *keyworded* arguments. Instead of creating a tuple of values, `**kwargs` builds a dictionary of key/value pairs. For example:" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 5, 145 | "metadata": {}, 146 | "outputs": [ 147 | { 148 | "name": "stdout", 149 | "output_type": "stream", 150 | "text": [ 151 | "My favorite fruit is pineapple\n" 152 | ] 153 | } 154 | ], 155 | "source": [ 156 | "def myfunc(**kwargs):\n", 157 | " if 'fruit' in kwargs:\n", 158 | " print(f\"My favorite fruit is {kwargs['fruit']}\") # review String Formatting and f-strings if this syntax is unfamiliar\n", 159 | " else:\n", 160 | " print(\"I don't like fruit\")\n", 161 | " \n", 162 | "myfunc(fruit='pineapple')" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 6, 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "I don't like fruit\n" 175 | ] 176 | } 177 | ], 178 | "source": [ 179 | "myfunc()" 180 | ] 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "metadata": {}, 185 | "source": [ 186 | "## `*args` and `**kwargs` combined\n", 187 | "\n", 188 | "You can pass `*args` and `**kwargs` into the same function, but `*args` have to appear before `**kwargs`" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 7, 194 | "metadata": {}, 195 | "outputs": [ 196 | { 197 | "name": "stdout", 198 | "output_type": "stream", 199 | "text": [ 200 | "I like eggs and spam and my favorite fruit is cherries\n", 201 | "May I have some orange juice?\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "def myfunc(*args, **kwargs):\n", 207 | " if 'fruit' and 'juice' in kwargs:\n", 208 | " print(f\"I like {' and '.join(args)} and my favorite fruit is {kwargs['fruit']}\")\n", 209 | " print(f\"May I have some {kwargs['juice']} juice?\")\n", 210 | " else:\n", 211 | " pass\n", 212 | " \n", 213 | "myfunc('eggs','spam',fruit='cherries',juice='orange')" 214 | ] 215 | }, 216 | { 217 | "cell_type": "markdown", 218 | "metadata": {}, 219 | "source": [ 220 | "Placing keyworded arguments ahead of positional arguments raises an exception:" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 8, 226 | "metadata": {}, 227 | "outputs": [ 228 | { 229 | "ename": "SyntaxError", 230 | "evalue": "positional argument follows keyword argument (, line 1)", 231 | "output_type": "error", 232 | "traceback": [ 233 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m myfunc(fruit='cherries',juice='orange','eggs','spam')\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n" 234 | ] 235 | } 236 | ], 237 | "source": [ 238 | "myfunc(fruit='cherries',juice='orange','eggs','spam')" 239 | ] 240 | }, 241 | { 242 | "cell_type": "markdown", 243 | "metadata": {}, 244 | "source": [ 245 | "As with \"args\", you can use any name you'd like for keyworded arguments - \"kwargs\" is just a popular convention.\n", 246 | "\n", 247 | "That's it! Now you should understand how `*args` and `**kwargs` provide the flexibilty to work with arbitrary numbers of arguments!" 248 | ] 249 | } 250 | ], 251 | "metadata": { 252 | "kernelspec": { 253 | "display_name": "Python 3", 254 | "language": "python", 255 | "name": "python3" 256 | }, 257 | "language_info": { 258 | "codemirror_mode": { 259 | "name": "ipython", 260 | "version": 2 261 | }, 262 | "file_extension": ".py", 263 | "mimetype": "text/x-python", 264 | "name": "python", 265 | "nbconvert_exporter": "python", 266 | "pygments_lexer": "ipython2", 267 | "version": "2.7.12" 268 | } 269 | }, 270 | "nbformat": 4, 271 | "nbformat_minor": 2 272 | } 273 | -------------------------------------------------------------------------------- /04-Milestone Project - 1/.ipynb_checkpoints/01-Milestone Project 1 - Assignment-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Milestone Project 1\n", 8 | "## Congratulations on making it to your first milestone!\n", 9 | "You've already learned a ton and are ready to work on a real project.\n", 10 | "\n", 11 | "Your assignment: Create a Tic Tac Toe game. You are free to use any IDE you like.\n", 12 | "\n", 13 | "Here are the requirements:\n", 14 | "\n", 15 | "* 2 players should be able to play the game (both sitting at the same computer)\n", 16 | "* The board should be printed out every time a player makes a move\n", 17 | "* You should be able to accept input of the player position and then place a symbol on the board\n", 18 | "\n", 19 | "Feel free to use Google to help you figure anything out (but don't just Google \"Tic Tac Toe in Python\" otherwise you won't learn anything!) Keep in mind that this project can take anywhere between several hours to several days.\n", 20 | "\n", 21 | "There are 4 Jupyter Notebooks related to this assignment:\n", 22 | "\n", 23 | "* This Assignment Notebook\n", 24 | "* A \"Walkthrough Steps Workbook\" Notebook\n", 25 | "* A \"Complete Walkthrough Solution\" Notebook\n", 26 | "* An \"Advanced Solution\" Notebook\n", 27 | "\n", 28 | "I encourage you to just try to start the project on your own without referencing any of the notebooks. If you get stuck, check out the next lecture which is a text lecture with helpful hints and steps. If you're still stuck after that, then check out the Walkthrough Steps Workbook, which breaks up the project in steps for you to solve. Still stuck? Then check out the Complete Walkthrough Solution video for more help on approaching the project!" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "There are parts of this that will be a struggle...and that is good! I have complete faith that if you have made it this far through the course you have all the tools and knowledge to tackle this project. Remember, it's totally open book, so take your time, do a little research, and remember:\n", 36 | "\n", 37 | "## HAVE FUN!" 38 | ] 39 | } 40 | ], 41 | "metadata": { 42 | "kernelspec": { 43 | "display_name": "Python 3", 44 | "language": "python", 45 | "name": "python3" 46 | }, 47 | "language_info": { 48 | "codemirror_mode": { 49 | "name": "ipython", 50 | "version": 3 51 | }, 52 | "file_extension": ".py", 53 | "mimetype": "text/x-python", 54 | "name": "python", 55 | "nbconvert_exporter": "python", 56 | "pygments_lexer": "ipython3", 57 | "version": "3.6.2" 58 | } 59 | }, 60 | "nbformat": 4, 61 | "nbformat_minor": 1 62 | } 63 | -------------------------------------------------------------------------------- /04-Milestone Project - 1/.ipynb_checkpoints/02-Milestone Project 1 - Walkthrough Steps Workbook-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Milestone Project 1: Walkthrough Steps Workbook\n", 8 | "\n", 9 | "Below is a set of steps for you to follow to try to create the Tic Tac Toe Milestone Project game!" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "#### Some suggested tools before you get started:\n", 17 | "To take input from a user:\n", 18 | "\n", 19 | " player1 = input(\"Please pick a marker 'X' or 'O'\")\n", 20 | " \n", 21 | "Note that input() takes in a string. If you need an integer value, use\n", 22 | "\n", 23 | " position = int(input('Please enter a number'))\n", 24 | " \n", 25 | "
To clear the screen between moves:\n", 26 | "\n", 27 | " from IPython.display import clear_output\n", 28 | " clear_output()\n", 29 | " \n", 30 | "Note that clear_output() will only work in jupyter. To clear the screen in other IDEs, consider:\n", 31 | "\n", 32 | " print('\\n'*100)\n", 33 | " \n", 34 | "This scrolls the previous board up out of view. Now on to the program!" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "**Step 1: Write a function that can print out a board. Set up your board as a list, where each index 1-9 corresponds with a number on a number pad, so you get a 3 by 3 board representation.**" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 87, 47 | "metadata": { 48 | "collapsed": true 49 | }, 50 | "outputs": [], 51 | "source": [ 52 | "from IPython.display import clear_output\n", 53 | "\n", 54 | "def display_board(board):\n", 55 | " \n", 56 | " pass" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "**Step 2: Write a function that can take in a player input and assign their marker as 'X' or 'O'. Think about using *while* loops to continually ask until you get a correct answer.**" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 88, 69 | "metadata": { 70 | "collapsed": true 71 | }, 72 | "outputs": [], 73 | "source": [ 74 | "def player_input():\n", 75 | " \n", 76 | " pass" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "**Step 3: Write a function that takes in the board list object, a marker ('X' or 'O'), and a desired position (number 1-9) and assigns it to the board.**" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 89, 89 | "metadata": { 90 | "collapsed": true 91 | }, 92 | "outputs": [], 93 | "source": [ 94 | "def place_marker(board, marker, position):\n", 95 | " \n", 96 | " pass" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | "**Step 4: Write a function that takes in a board and a mark (X or O) and then checks to see if that mark has won. **" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 90, 109 | "metadata": { 110 | "collapsed": true 111 | }, 112 | "outputs": [], 113 | "source": [ 114 | "def win_check(board, mark):\n", 115 | " \n", 116 | " pass" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": {}, 122 | "source": [ 123 | "**Step 5: Write a function that uses the random module to randomly decide which player goes first. You may want to lookup random.randint() Return a string of which player went first.**" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 91, 129 | "metadata": { 130 | "collapsed": true 131 | }, 132 | "outputs": [], 133 | "source": [ 134 | "import random\n", 135 | "def choose_first():\n", 136 | " pass" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "**Step 6: Write a function that returns a boolean indicating whether a space on the board is freely available.**" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 92, 149 | "metadata": { 150 | "collapsed": true 151 | }, 152 | "outputs": [], 153 | "source": [ 154 | "def space_check(board, position):\n", 155 | " \n", 156 | " pass" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "**Step 7: Write a function that checks if the board is full and returns a boolean value. True if full, False otherwise.**" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 93, 169 | "metadata": { 170 | "collapsed": true 171 | }, 172 | "outputs": [], 173 | "source": [ 174 | "def full_board_check(board):\n", 175 | " \n", 176 | " pass" 177 | ] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "metadata": {}, 182 | "source": [ 183 | "**Step 8: Write a function that asks for a player's next position (as a number 1-9) and then uses the function from step 6 to check if it's a free position. If it is, then return the position for later use.**" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 94, 189 | "metadata": { 190 | "collapsed": true 191 | }, 192 | "outputs": [], 193 | "source": [ 194 | "def player_choice(board):\n", 195 | " \n", 196 | " pass" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "**Step 9: Write a function that asks the player if they want to play again and returns a boolean True if they do want to play again.**" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 95, 209 | "metadata": { 210 | "collapsed": true 211 | }, 212 | "outputs": [], 213 | "source": [ 214 | "def replay():\n", 215 | " \n", 216 | " pass" 217 | ] 218 | }, 219 | { 220 | "cell_type": "markdown", 221 | "metadata": { 222 | "collapsed": true 223 | }, 224 | "source": [ 225 | "**Step 10: Here comes the hard part! Use while loops and the functions you've made to run the game!**" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 7, 231 | "metadata": {}, 232 | "outputs": [ 233 | { 234 | "name": "stdout", 235 | "output_type": "stream", 236 | "text": [ 237 | "Welcome to Tic Tac Toe!\n" 238 | ] 239 | } 240 | ], 241 | "source": [ 242 | "print('Welcome to Tic Tac Toe!')\n", 243 | "\n", 244 | "#while True:\n", 245 | " # Set the game up here\n", 246 | " #pass\n", 247 | "\n", 248 | " #while game_on:\n", 249 | " #Player 1 Turn\n", 250 | " \n", 251 | " \n", 252 | " # Player2's turn.\n", 253 | " \n", 254 | " #pass\n", 255 | "\n", 256 | " #if not replay():\n", 257 | " #break" 258 | ] 259 | }, 260 | { 261 | "cell_type": "markdown", 262 | "metadata": { 263 | "collapsed": true 264 | }, 265 | "source": [ 266 | "## Good Job!" 267 | ] 268 | } 269 | ], 270 | "metadata": { 271 | "kernelspec": { 272 | "display_name": "Python 3", 273 | "language": "python", 274 | "name": "python3" 275 | }, 276 | "language_info": { 277 | "codemirror_mode": { 278 | "name": "ipython", 279 | "version": 3 280 | }, 281 | "file_extension": ".py", 282 | "mimetype": "text/x-python", 283 | "name": "python", 284 | "nbconvert_exporter": "python", 285 | "pygments_lexer": "ipython3", 286 | "version": "3.6.2" 287 | } 288 | }, 289 | "nbformat": 4, 290 | "nbformat_minor": 1 291 | } 292 | -------------------------------------------------------------------------------- /04-Milestone Project - 1/.ipynb_checkpoints/04-OPTIONAL -Milestone Project 1 - Advanced Solution-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Tic Tac Toe - Advanced Solution\n", 8 | "\n", 9 | "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!" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": { 16 | "collapsed": true 17 | }, 18 | "outputs": [], 19 | "source": [ 20 | "# Specifically for the iPython Notebook environment for clearing output\n", 21 | "from IPython.display import clear_output\n", 22 | "import random\n", 23 | "\n", 24 | "# Global variables\n", 25 | "theBoard = [' '] * 10 # a list of empty spaces\n", 26 | "available = [str(num) for num in range(0,10)] # a List Comprehension\n", 27 | "players = [0,'X','O'] # note that players[1] == 'X' and players[-1] == 'O'" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 2, 33 | "metadata": { 34 | "collapsed": false 35 | }, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "Available TIC-TAC-TOE\n", 42 | " moves\n", 43 | "\n", 44 | " 7|8|9 | | \n", 45 | " ----- -----\n", 46 | " 4|5|6 | | \n", 47 | " ----- -----\n", 48 | " 1|2|3 | | \n", 49 | "\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "def display_board(a,b):\n", 55 | " print('Available TIC-TAC-TOE\\n'+\n", 56 | " ' moves\\n\\n '+\n", 57 | " a[7]+'|'+a[8]+'|'+a[9]+' '+b[7]+'|'+b[8]+'|'+b[9]+'\\n '+\n", 58 | " '----- -----\\n '+\n", 59 | " a[4]+'|'+a[5]+'|'+a[6]+' '+b[4]+'|'+b[5]+'|'+b[6]+'\\n '+\n", 60 | " '----- -----\\n '+\n", 61 | " a[1]+'|'+a[2]+'|'+a[3]+' '+b[1]+'|'+b[2]+'|'+b[3]+'\\n')\n", 62 | "display_board(available,theBoard)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 11, 68 | "metadata": { 69 | "collapsed": false 70 | }, 71 | "outputs": [ 72 | { 73 | "name": "stdout", 74 | "output_type": "stream", 75 | "text": [ 76 | "Available TIC-TAC-TOE\n", 77 | " moves\n", 78 | "\n", 79 | " 7|8|9 | | \n", 80 | " ----- -----\n", 81 | " 4|5|6 | | \n", 82 | " ----- -----\n", 83 | " 1|2|3 | | \n", 84 | "\n" 85 | ] 86 | } 87 | ], 88 | "source": [ 89 | "def display_board(a,b):\n", 90 | " 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", 91 | "display_board(available,theBoard)\n" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 3, 97 | "metadata": { 98 | "collapsed": true 99 | }, 100 | "outputs": [], 101 | "source": [ 102 | "def place_marker(avail,board,marker,position):\n", 103 | " board[position] = marker\n", 104 | " avail[position] = ' '" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 4, 110 | "metadata": { 111 | "collapsed": true 112 | }, 113 | "outputs": [], 114 | "source": [ 115 | "def win_check(board,mark):\n", 116 | "\n", 117 | " return ((board[7] == board[8] == board[9] == mark) or # across the top\n", 118 | " (board[4] == board[5] == board[6] == mark) or # across the middle\n", 119 | " (board[1] == board[2] == board[3] == mark) or # across the bottom\n", 120 | " (board[7] == board[4] == board[1] == mark) or # down the middle\n", 121 | " (board[8] == board[5] == board[2] == mark) or # down the middle\n", 122 | " (board[9] == board[6] == board[3] == mark) or # down the right side\n", 123 | " (board[7] == board[5] == board[3] == mark) or # diagonal\n", 124 | " (board[9] == board[5] == board[1] == mark)) # diagonal" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 5, 130 | "metadata": { 131 | "collapsed": true 132 | }, 133 | "outputs": [], 134 | "source": [ 135 | "def random_player():\n", 136 | " return random.choice((-1, 1))\n", 137 | " \n", 138 | "def space_check(board,position):\n", 139 | " return board[position] == ' '\n", 140 | "\n", 141 | "def full_board_check(board):\n", 142 | " return ' ' not in board[1:]" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 6, 148 | "metadata": { 149 | "collapsed": true 150 | }, 151 | "outputs": [], 152 | "source": [ 153 | "def player_choice(board,player):\n", 154 | " position = 0\n", 155 | " \n", 156 | " while position not in [1,2,3,4,5,6,7,8,9] or not space_check(board, position):\n", 157 | " try:\n", 158 | " position = int(input('Player %s, choose your next position: (1-9) '%(player)))\n", 159 | " except:\n", 160 | " print(\"I'm sorry, please try again.\")\n", 161 | " \n", 162 | " return position" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 7, 168 | "metadata": { 169 | "collapsed": true 170 | }, 171 | "outputs": [], 172 | "source": [ 173 | "def replay():\n", 174 | " \n", 175 | " return input('Do you want to play again? Enter Yes or No: ').lower().startswith('y')" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": null, 181 | "metadata": { 182 | "collapsed": false 183 | }, 184 | "outputs": [ 185 | { 186 | "name": "stdout", 187 | "output_type": "stream", 188 | "text": [ 189 | "Welcome to Tic Tac Toe!\n", 190 | "For this round, Player X will go first!\n" 191 | ] 192 | } 193 | ], 194 | "source": [ 195 | "while True:\n", 196 | " clear_output()\n", 197 | " print('Welcome to Tic Tac Toe!')\n", 198 | " \n", 199 | " toggle = random_player()\n", 200 | " player = players[toggle]\n", 201 | " print('For this round, Player %s will go first!' %(player))\n", 202 | " \n", 203 | " game_on = True\n", 204 | " input('Hit Enter to continue')\n", 205 | " while game_on:\n", 206 | " display_board(available,theBoard)\n", 207 | " position = player_choice(theBoard,player)\n", 208 | " place_marker(available,theBoard,player,position)\n", 209 | "\n", 210 | " if win_check(theBoard, player):\n", 211 | " display_board(available,theBoard)\n", 212 | " print('Congratulations! Player '+player+' wins!')\n", 213 | " game_on = False\n", 214 | " else:\n", 215 | " if full_board_check(theBoard):\n", 216 | " display_board(available,theBoard)\n", 217 | " print('The game is a draw!')\n", 218 | " break\n", 219 | " else:\n", 220 | " toggle *= -1\n", 221 | " player = players[toggle]\n", 222 | " clear_output()\n", 223 | "\n", 224 | " # reset the board and available moves list\n", 225 | " theBoard = [' '] * 10\n", 226 | " available = [str(num) for num in range(0,10)]\n", 227 | " \n", 228 | " if not replay():\n", 229 | " break" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": null, 235 | "metadata": { 236 | "collapsed": true 237 | }, 238 | "outputs": [], 239 | "source": [] 240 | } 241 | ], 242 | "metadata": { 243 | "anaconda-cloud": {}, 244 | "kernelspec": { 245 | "display_name": "Python [default]", 246 | "language": "python", 247 | "name": "python3" 248 | }, 249 | "language_info": { 250 | "codemirror_mode": { 251 | "name": "ipython", 252 | "version": 3 253 | }, 254 | "file_extension": ".py", 255 | "mimetype": "text/x-python", 256 | "name": "python", 257 | "nbconvert_exporter": "python", 258 | "pygments_lexer": "ipython3", 259 | "version": "3.5.3" 260 | } 261 | }, 262 | "nbformat": 4, 263 | "nbformat_minor": 1 264 | } 265 | -------------------------------------------------------------------------------- /04-Milestone Project - 1/01-Milestone Project 1 - Assignment.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Milestone Project 1\n", 8 | "## Congratulations on making it to your first milestone!\n", 9 | "You've already learned a ton and are ready to work on a real project.\n", 10 | "\n", 11 | "Your assignment: Create a Tic Tac Toe game. You are free to use any IDE you like.\n", 12 | "\n", 13 | "Here are the requirements:\n", 14 | "\n", 15 | "* 2 players should be able to play the game (both sitting at the same computer)\n", 16 | "* The board should be printed out every time a player makes a move\n", 17 | "* You should be able to accept input of the player position and then place a symbol on the board\n", 18 | "\n", 19 | "Feel free to use Google to help you figure anything out (but don't just Google \"Tic Tac Toe in Python\" otherwise you won't learn anything!) Keep in mind that this project can take anywhere between several hours to several days.\n", 20 | "\n", 21 | "There are 4 Jupyter Notebooks related to this assignment:\n", 22 | "\n", 23 | "* This Assignment Notebook\n", 24 | "* A \"Walkthrough Steps Workbook\" Notebook\n", 25 | "* A \"Complete Walkthrough Solution\" Notebook\n", 26 | "* An \"Advanced Solution\" Notebook\n", 27 | "\n", 28 | "I encourage you to just try to start the project on your own without referencing any of the notebooks. If you get stuck, check out the next lecture which is a text lecture with helpful hints and steps. If you're still stuck after that, then check out the Walkthrough Steps Workbook, which breaks up the project in steps for you to solve. Still stuck? Then check out the Complete Walkthrough Solution video for more help on approaching the project!" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "There are parts of this that will be a struggle...and that is good! I have complete faith that if you have made it this far through the course you have all the tools and knowledge to tackle this project. Remember, it's totally open book, so take your time, do a little research, and remember:\n", 36 | "\n", 37 | "## HAVE FUN!" 38 | ] 39 | } 40 | ], 41 | "metadata": { 42 | "kernelspec": { 43 | "display_name": "Python 3", 44 | "language": "python", 45 | "name": "python3" 46 | }, 47 | "language_info": { 48 | "codemirror_mode": { 49 | "name": "ipython", 50 | "version": 3 51 | }, 52 | "file_extension": ".py", 53 | "mimetype": "text/x-python", 54 | "name": "python", 55 | "nbconvert_exporter": "python", 56 | "pygments_lexer": "ipython3", 57 | "version": "3.6.2" 58 | } 59 | }, 60 | "nbformat": 4, 61 | "nbformat_minor": 1 62 | } 63 | -------------------------------------------------------------------------------- /05-Object Oriented Programming/.ipynb_checkpoints/02-Object Oriented Programming Homework-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Object Oriented Programming\n", 8 | "## Homework Assignment\n", 9 | "\n", 10 | "#### Problem 1\n", 11 | "Fill in the Line class methods to accept coordinates as a pair of tuples and return the slope and distance of the line." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "class Line:\n", 21 | " \n", 22 | " def __init__(self,coor1,coor2):\n", 23 | " pass\n", 24 | " \n", 25 | " def distance(self):\n", 26 | " pass\n", 27 | " \n", 28 | " def slope(self):\n", 29 | " pass" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 2, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "# EXAMPLE OUTPUT\n", 39 | "\n", 40 | "coordinate1 = (3,2)\n", 41 | "coordinate2 = (8,10)\n", 42 | "\n", 43 | "li = Line(coordinate1,coordinate2)" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 3, 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "data": { 53 | "text/plain": [ 54 | "9.433981132056603" 55 | ] 56 | }, 57 | "execution_count": 3, 58 | "metadata": {}, 59 | "output_type": "execute_result" 60 | } 61 | ], 62 | "source": [ 63 | "li.distance()" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 4, 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "data": { 73 | "text/plain": [ 74 | "1.6" 75 | ] 76 | }, 77 | "execution_count": 4, 78 | "metadata": {}, 79 | "output_type": "execute_result" 80 | } 81 | ], 82 | "source": [ 83 | "li.slope()" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "________\n", 91 | "#### Problem 2" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "Fill in the class " 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 5, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "class Cylinder:\n", 108 | " \n", 109 | " def __init__(self,height=1,radius=1):\n", 110 | " pass\n", 111 | " \n", 112 | " def volume(self):\n", 113 | " pass\n", 114 | " \n", 115 | " def surface_area(self):\n", 116 | " pass" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 6, 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [ 125 | "# EXAMPLE OUTPUT\n", 126 | "c = Cylinder(2,3)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 7, 132 | "metadata": {}, 133 | "outputs": [ 134 | { 135 | "data": { 136 | "text/plain": [ 137 | "56.52" 138 | ] 139 | }, 140 | "execution_count": 7, 141 | "metadata": {}, 142 | "output_type": "execute_result" 143 | } 144 | ], 145 | "source": [ 146 | "c.volume()" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 8, 152 | "metadata": {}, 153 | "outputs": [ 154 | { 155 | "data": { 156 | "text/plain": [ 157 | "94.2" 158 | ] 159 | }, 160 | "execution_count": 8, 161 | "metadata": {}, 162 | "output_type": "execute_result" 163 | } 164 | ], 165 | "source": [ 166 | "c.surface_area()" 167 | ] 168 | } 169 | ], 170 | "metadata": { 171 | "kernelspec": { 172 | "display_name": "Python 3", 173 | "language": "python", 174 | "name": "python3" 175 | }, 176 | "language_info": { 177 | "codemirror_mode": { 178 | "name": "ipython", 179 | "version": 3 180 | }, 181 | "file_extension": ".py", 182 | "mimetype": "text/x-python", 183 | "name": "python", 184 | "nbconvert_exporter": "python", 185 | "pygments_lexer": "ipython3", 186 | "version": "3.6.2" 187 | } 188 | }, 189 | "nbformat": 4, 190 | "nbformat_minor": 1 191 | } 192 | -------------------------------------------------------------------------------- /05-Object Oriented Programming/.ipynb_checkpoints/03-Object Oriented Programming Homework - Solution-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Object Oriented Programming\n", 8 | "## Homework Assignment\n", 9 | "\n", 10 | "#### Problem 1\n", 11 | "Fill in the Line class methods to accept coordinates as a pair of tuples and return the slope and distance of the line." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "class Line(object):\n", 21 | " \n", 22 | " def __init__(self,coor1,coor2):\n", 23 | " self.coor1 = coor1\n", 24 | " self.coor2 = coor2\n", 25 | " \n", 26 | " def distance(self):\n", 27 | " x1,y1 = self.coor1\n", 28 | " x2,y2 = self.coor2\n", 29 | " return ((x2-x1)**2 + (y2-y1)**2)**0.5\n", 30 | " \n", 31 | " def slope(self):\n", 32 | " x1,y1 = self.coor1\n", 33 | " x2,y2 = self.coor2\n", 34 | " return (y2-y1)/(x2-x1)" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 2, 40 | "metadata": {}, 41 | "outputs": [], 42 | "source": [ 43 | "coordinate1 = (3,2)\n", 44 | "coordinate2 = (8,10)\n", 45 | "\n", 46 | "li = Line(coordinate1,coordinate2)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 3, 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "9.433981132056603" 58 | ] 59 | }, 60 | "execution_count": 3, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "li.distance()" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 4, 72 | "metadata": {}, 73 | "outputs": [ 74 | { 75 | "data": { 76 | "text/plain": [ 77 | "1.6" 78 | ] 79 | }, 80 | "execution_count": 4, 81 | "metadata": {}, 82 | "output_type": "execute_result" 83 | } 84 | ], 85 | "source": [ 86 | "li.slope()" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "________\n", 94 | "#### Problem 2" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "Fill in the class " 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 5, 107 | "metadata": {}, 108 | "outputs": [], 109 | "source": [ 110 | "class Cylinder:\n", 111 | " \n", 112 | " def __init__(self,height=1,radius=1):\n", 113 | " self.height = height\n", 114 | " self.radius = radius\n", 115 | " \n", 116 | " def volume(self):\n", 117 | " return self.height*3.14*(self.radius)**2\n", 118 | " \n", 119 | " def surface_area(self):\n", 120 | " top = 3.14 * (self.radius)**2\n", 121 | " return (2*top) + (2*3.14*self.radius*self.height)" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 6, 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "c = Cylinder(2,3)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 7, 136 | "metadata": {}, 137 | "outputs": [ 138 | { 139 | "data": { 140 | "text/plain": [ 141 | "56.52" 142 | ] 143 | }, 144 | "execution_count": 7, 145 | "metadata": {}, 146 | "output_type": "execute_result" 147 | } 148 | ], 149 | "source": [ 150 | "c.volume()" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 8, 156 | "metadata": {}, 157 | "outputs": [ 158 | { 159 | "data": { 160 | "text/plain": [ 161 | "94.2" 162 | ] 163 | }, 164 | "execution_count": 8, 165 | "metadata": {}, 166 | "output_type": "execute_result" 167 | } 168 | ], 169 | "source": [ 170 | "c.surface_area()" 171 | ] 172 | } 173 | ], 174 | "metadata": { 175 | "kernelspec": { 176 | "display_name": "Python 3", 177 | "language": "python", 178 | "name": "python3" 179 | }, 180 | "language_info": { 181 | "codemirror_mode": { 182 | "name": "ipython", 183 | "version": 3 184 | }, 185 | "file_extension": ".py", 186 | "mimetype": "text/x-python", 187 | "name": "python", 188 | "nbconvert_exporter": "python", 189 | "pygments_lexer": "ipython3", 190 | "version": "3.6.2" 191 | } 192 | }, 193 | "nbformat": 4, 194 | "nbformat_minor": 1 195 | } 196 | -------------------------------------------------------------------------------- /05-Object Oriented Programming/.ipynb_checkpoints/04-OOP Challenge-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Object Oriented Programming Challenge\n", 8 | "\n", 9 | "For this challenge, create a bank account class that has two attributes:\n", 10 | "\n", 11 | "* owner\n", 12 | "* balance\n", 13 | "\n", 14 | "and two methods:\n", 15 | "\n", 16 | "* deposit\n", 17 | "* withdraw\n", 18 | "\n", 19 | "As an added requirement, withdrawals may not exceed the available balance.\n", 20 | "\n", 21 | "Instantiate your class, make several deposits and withdrawals, and test to make sure the account can't be overdrawn." 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "class Account:\n", 31 | " pass" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 2, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "# 1. Instantiate the class\n", 41 | "acct1 = Account('Jose',100)" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 3, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "Account owner: Jose\n", 54 | "Account balance: $100\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "# 2. Print the object\n", 60 | "print(acct1)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 4, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "data": { 70 | "text/plain": [ 71 | "'Jose'" 72 | ] 73 | }, 74 | "execution_count": 4, 75 | "metadata": {}, 76 | "output_type": "execute_result" 77 | } 78 | ], 79 | "source": [ 80 | "# 3. Show the account owner attribute\n", 81 | "acct1.owner" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 5, 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "data": { 91 | "text/plain": [ 92 | "100" 93 | ] 94 | }, 95 | "execution_count": 5, 96 | "metadata": {}, 97 | "output_type": "execute_result" 98 | } 99 | ], 100 | "source": [ 101 | "# 4. Show the account balance attribute\n", 102 | "acct1.balance" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 6, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "name": "stdout", 112 | "output_type": "stream", 113 | "text": [ 114 | "Deposit Accepted\n" 115 | ] 116 | } 117 | ], 118 | "source": [ 119 | "# 5. Make a series of deposits and withdrawals\n", 120 | "acct1.deposit(50)" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 7, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "name": "stdout", 130 | "output_type": "stream", 131 | "text": [ 132 | "Withdrawal Accepted\n" 133 | ] 134 | } 135 | ], 136 | "source": [ 137 | "acct1.withdraw(75)" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 8, 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "name": "stdout", 147 | "output_type": "stream", 148 | "text": [ 149 | "Funds Unavailable!\n" 150 | ] 151 | } 152 | ], 153 | "source": [ 154 | "# 6. Make a withdrawal that exceeds the available balance\n", 155 | "acct1.withdraw(500)" 156 | ] 157 | }, 158 | { 159 | "cell_type": "markdown", 160 | "metadata": {}, 161 | "source": [ 162 | "## Good job!" 163 | ] 164 | } 165 | ], 166 | "metadata": { 167 | "kernelspec": { 168 | "display_name": "Python 3", 169 | "language": "python", 170 | "name": "python3" 171 | }, 172 | "language_info": { 173 | "codemirror_mode": { 174 | "name": "ipython", 175 | "version": 3 176 | }, 177 | "file_extension": ".py", 178 | "mimetype": "text/x-python", 179 | "name": "python", 180 | "nbconvert_exporter": "python", 181 | "pygments_lexer": "ipython3", 182 | "version": "3.6.2" 183 | } 184 | }, 185 | "nbformat": 4, 186 | "nbformat_minor": 2 187 | } 188 | -------------------------------------------------------------------------------- /05-Object Oriented Programming/.ipynb_checkpoints/05-OOP Challenge - Solution-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Object Oriented Programming Challenge - Solution\n", 8 | "\n", 9 | "For this challenge, create a bank account class that has two attributes:\n", 10 | "\n", 11 | "* owner\n", 12 | "* balance\n", 13 | "\n", 14 | "and two methods:\n", 15 | "\n", 16 | "* deposit\n", 17 | "* withdraw\n", 18 | "\n", 19 | "As an added requirement, withdrawals may not exceed the available balance.\n", 20 | "\n", 21 | "Instantiate your class, make several deposits and withdrawals, and test to make sure the account can't be overdrawn." 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "class Account:\n", 31 | " def __init__(self,owner,balance=0):\n", 32 | " self.owner = owner\n", 33 | " self.balance = balance\n", 34 | " \n", 35 | " def __str__(self):\n", 36 | " return f'Account owner: {self.owner}\\nAccount balance: ${self.balance}'\n", 37 | " \n", 38 | " def deposit(self,dep_amt):\n", 39 | " self.balance += dep_amt\n", 40 | " print('Deposit Accepted')\n", 41 | " \n", 42 | " def withdraw(self,wd_amt):\n", 43 | " if self.balance >= wd_amt:\n", 44 | " self.balance -= wd_amt\n", 45 | " print('Withdrawal Accepted')\n", 46 | " else:\n", 47 | " print('Funds Unavailable!')" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 2, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "# 1. Instantiate the class\n", 57 | "acct1 = Account('Jose',100)" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 3, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "Account owner: Jose\n", 70 | "Account balance: $100\n" 71 | ] 72 | } 73 | ], 74 | "source": [ 75 | "# 2. Print the object\n", 76 | "print(acct1)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 4, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "'Jose'" 88 | ] 89 | }, 90 | "execution_count": 4, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "# 3. Show the account owner attribute\n", 97 | "acct1.owner" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 5, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "data": { 107 | "text/plain": [ 108 | "100" 109 | ] 110 | }, 111 | "execution_count": 5, 112 | "metadata": {}, 113 | "output_type": "execute_result" 114 | } 115 | ], 116 | "source": [ 117 | "# 4. Show the account balance attribute\n", 118 | "acct1.balance" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 6, 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "name": "stdout", 128 | "output_type": "stream", 129 | "text": [ 130 | "Deposit Accepted\n" 131 | ] 132 | } 133 | ], 134 | "source": [ 135 | "# 5. Make a series of deposits and withdrawals\n", 136 | "acct1.deposit(50)" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 7, 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "name": "stdout", 146 | "output_type": "stream", 147 | "text": [ 148 | "Withdrawal Accepted\n" 149 | ] 150 | } 151 | ], 152 | "source": [ 153 | "acct1.withdraw(75)" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 8, 159 | "metadata": {}, 160 | "outputs": [ 161 | { 162 | "name": "stdout", 163 | "output_type": "stream", 164 | "text": [ 165 | "Funds Unavailable!\n" 166 | ] 167 | } 168 | ], 169 | "source": [ 170 | "# 6. Make a withdrawal that exceeds the available balance\n", 171 | "acct1.withdraw(500)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "metadata": {}, 177 | "source": [ 178 | "## Good job!" 179 | ] 180 | } 181 | ], 182 | "metadata": { 183 | "kernelspec": { 184 | "display_name": "Python 3", 185 | "language": "python", 186 | "name": "python3" 187 | }, 188 | "language_info": { 189 | "codemirror_mode": { 190 | "name": "ipython", 191 | "version": 3 192 | }, 193 | "file_extension": ".py", 194 | "mimetype": "text/x-python", 195 | "name": "python", 196 | "nbconvert_exporter": "python", 197 | "pygments_lexer": "ipython3", 198 | "version": "3.6.2" 199 | } 200 | }, 201 | "nbformat": 4, 202 | "nbformat_minor": 2 203 | } 204 | -------------------------------------------------------------------------------- /05-Object Oriented Programming/.ipynb_checkpoints/OOP_Introduction-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /05-Object Oriented Programming/Assignment/02-Object Oriented Programming Homework.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Object Oriented Programming\n", 8 | "## Homework Assignment\n", 9 | "\n", 10 | "#### Problem 1\n", 11 | "Fill in the Line class methods to accept coordinates as a pair of tuples and return the slope and distance of the line." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "class Line:\n", 21 | " \n", 22 | " def __init__(self,coor1,coor2):\n", 23 | " pass\n", 24 | " \n", 25 | " def distance(self):\n", 26 | " pass\n", 27 | " \n", 28 | " def slope(self):\n", 29 | " pass" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 2, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "# EXAMPLE OUTPUT\n", 39 | "\n", 40 | "coordinate1 = (3,2)\n", 41 | "coordinate2 = (8,10)\n", 42 | "\n", 43 | "li = Line(coordinate1,coordinate2)" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 3, 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "data": { 53 | "text/plain": [ 54 | "9.433981132056603" 55 | ] 56 | }, 57 | "execution_count": 3, 58 | "metadata": {}, 59 | "output_type": "execute_result" 60 | } 61 | ], 62 | "source": [ 63 | "li.distance()" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 4, 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "data": { 73 | "text/plain": [ 74 | "1.6" 75 | ] 76 | }, 77 | "execution_count": 4, 78 | "metadata": {}, 79 | "output_type": "execute_result" 80 | } 81 | ], 82 | "source": [ 83 | "li.slope()" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "________\n", 91 | "#### Problem 2" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "Fill in the class " 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 5, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "class Cylinder:\n", 108 | " \n", 109 | " def __init__(self,height=1,radius=1):\n", 110 | " pass\n", 111 | " \n", 112 | " def volume(self):\n", 113 | " pass\n", 114 | " \n", 115 | " def surface_area(self):\n", 116 | " pass" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 6, 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [ 125 | "# EXAMPLE OUTPUT\n", 126 | "c = Cylinder(2,3)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 7, 132 | "metadata": {}, 133 | "outputs": [ 134 | { 135 | "data": { 136 | "text/plain": [ 137 | "56.52" 138 | ] 139 | }, 140 | "execution_count": 7, 141 | "metadata": {}, 142 | "output_type": "execute_result" 143 | } 144 | ], 145 | "source": [ 146 | "c.volume()" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 8, 152 | "metadata": {}, 153 | "outputs": [ 154 | { 155 | "data": { 156 | "text/plain": [ 157 | "94.2" 158 | ] 159 | }, 160 | "execution_count": 8, 161 | "metadata": {}, 162 | "output_type": "execute_result" 163 | } 164 | ], 165 | "source": [ 166 | "c.surface_area()" 167 | ] 168 | } 169 | ], 170 | "metadata": { 171 | "kernelspec": { 172 | "display_name": "Python 3", 173 | "language": "python", 174 | "name": "python3" 175 | }, 176 | "language_info": { 177 | "codemirror_mode": { 178 | "name": "ipython", 179 | "version": 2 180 | }, 181 | "file_extension": ".py", 182 | "mimetype": "text/x-python", 183 | "name": "python", 184 | "nbconvert_exporter": "python", 185 | "pygments_lexer": "ipython2", 186 | "version": "2.7.12" 187 | } 188 | }, 189 | "nbformat": 4, 190 | "nbformat_minor": 2 191 | } 192 | -------------------------------------------------------------------------------- /05-Object Oriented Programming/Assignment/04-OOP Challenge.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Object Oriented Programming Challenge\n", 8 | "\n", 9 | "For this challenge, create a bank account class that has two attributes:\n", 10 | "\n", 11 | "* owner\n", 12 | "* balance\n", 13 | "\n", 14 | "and two methods:\n", 15 | "\n", 16 | "* deposit\n", 17 | "* withdraw\n", 18 | "\n", 19 | "As an added requirement, withdrawals may not exceed the available balance.\n", 20 | "\n", 21 | "Instantiate your class, make several deposits and withdrawals, and test to make sure the account can't be overdrawn." 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "class Account:\n", 31 | " pass" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 2, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "# 1. Instantiate the class\n", 41 | "acct1 = Account('Jose',100)" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 3, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "Account owner: Jose\n", 54 | "Account balance: $100\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "# 2. Print the object\n", 60 | "print(acct1)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 4, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "data": { 70 | "text/plain": [ 71 | "'Jose'" 72 | ] 73 | }, 74 | "execution_count": 4, 75 | "metadata": {}, 76 | "output_type": "execute_result" 77 | } 78 | ], 79 | "source": [ 80 | "# 3. Show the account owner attribute\n", 81 | "acct1.owner" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 5, 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "data": { 91 | "text/plain": [ 92 | "100" 93 | ] 94 | }, 95 | "execution_count": 5, 96 | "metadata": {}, 97 | "output_type": "execute_result" 98 | } 99 | ], 100 | "source": [ 101 | "# 4. Show the account balance attribute\n", 102 | "acct1.balance" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 6, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "name": "stdout", 112 | "output_type": "stream", 113 | "text": [ 114 | "Deposit Accepted\n" 115 | ] 116 | } 117 | ], 118 | "source": [ 119 | "# 5. Make a series of deposits and withdrawals\n", 120 | "acct1.deposit(50)" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 7, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "name": "stdout", 130 | "output_type": "stream", 131 | "text": [ 132 | "Withdrawal Accepted\n" 133 | ] 134 | } 135 | ], 136 | "source": [ 137 | "acct1.withdraw(75)" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 8, 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "name": "stdout", 147 | "output_type": "stream", 148 | "text": [ 149 | "Funds Unavailable!\n" 150 | ] 151 | } 152 | ], 153 | "source": [ 154 | "# 6. Make a withdrawal that exceeds the available balance\n", 155 | "acct1.withdraw(500)" 156 | ] 157 | }, 158 | { 159 | "cell_type": "markdown", 160 | "metadata": {}, 161 | "source": [ 162 | "## Good job!" 163 | ] 164 | } 165 | ], 166 | "metadata": { 167 | "kernelspec": { 168 | "display_name": "Python 3", 169 | "language": "python", 170 | "name": "python3" 171 | }, 172 | "language_info": { 173 | "codemirror_mode": { 174 | "name": "ipython", 175 | "version": 2 176 | }, 177 | "file_extension": ".py", 178 | "mimetype": "text/x-python", 179 | "name": "python", 180 | "nbconvert_exporter": "python", 181 | "pygments_lexer": "ipython2", 182 | "version": "2.7.12" 183 | } 184 | }, 185 | "nbformat": 4, 186 | "nbformat_minor": 2 187 | } 188 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-3 programming tutorial 2 | 3 | This is a concise Python 3 programming tutorial for people who think 4 | that reading is boring. I try to show everything with simple code 5 | examples; there are no long and complicated explanations with fancy 6 | words. If you have never programmed before click 7 | [here](basics/what-is-programming.md) to find out what programming is 8 | like and get started. 9 | 10 | This tutorial is aimed at people with no programming experience at all 11 | or very little programming experience. If you have programmed a lot in 12 | the past using some other language you may want to read [the official 13 | tutorial](https://docs.python.org/3/tutorial/) instead. 14 | 15 | You can use Python 3.3 or any newer Python with this tutorial. **Don't 16 | use Python 2.** If you write a Python 2 program now someone will need to 17 | convert it to Python 3 later, so it's best to just write Python 3 to 18 | begin with. Python 3 code will work just fine in Python 4, so you don't 19 | need to worry about that. Python 2 also has horrible 20 | [Unicode](http://www.unicode.org/standard/WhatIsUnicode.html) problems, 21 | so it's difficult to write Python 2 code that works correctly with 22 | non-English characters (like π and ♫). 23 | 24 | 25 | --------------------------------------------------------------------------------