└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Objective 2 | Give students a solid foundation in Python, which they can use to build for the web or do data science, data analytics and beyond. 3 | 4 | # Why Python? 5 | Let's discuss. 6 | 7 | # Where We'll Code 8 | Open this link in the browser: [Repl.it](https://repl.it/languages/python3) 9 | 10 | # The Basic Data Types 11 | What are the basic data types? 12 | - Integers, e.g. 1, 2, 3 13 | - Floating point numbers, e.g 2.3, 3.7 14 | - String, e.g. "John Krasinski" 15 | - bool, True or False 16 | - None, which just corresponds to empty or null, if you've used other languages 17 | 18 | # Numerical Data 19 | Let's start with integers and floats. 20 | 21 | Let's go into the python shell. 22 | To do this, open up your terminal and type 'python' and then press Enter. 23 | 24 | We can do math in the shell. 25 | ```python 26 | >>> 2 + 2 27 | 4 28 | >>> 2 * 3.5 29 | 7.0 30 | >>> 31 | ``` 32 | 33 | ## Practice question - try these out in the shell: 34 | ```python 35 | 1. 2 * 7 * 3.5 36 | 2. 3 + 5 - (2 * 6) 37 | 3. (3 + 5 - 2) * 6 38 | 4. 4 == 5 39 | 5. 5 == 5 40 | 6. 4 < 5 41 | 7. 5 >= 4 42 | 8. 9 != 9 43 | 9. 8 != 9 44 | ``` 45 | 46 | # Variables 47 | Now let's talk variables. What's a variable? It's a placeholder for a value. 48 | ```python 49 | >>> x = 5 50 | >>> y = 2 51 | >>> x + y 52 | 7 53 | >>> x = 3 54 | >>> x + y 55 | 5 56 | ``` 57 | 58 | # Assignment vs Equality Check 59 | 60 | ```python 61 | >>> x = 2 62 | >>> x == 5 63 | >>> x = 5 64 | ``` 65 | 66 | # Boolean: True or False 67 | Now let's talk about Booleans. 68 | Simply put, a boolean variable is either True or False, and every python object or data has a boolean value. 69 | We can find out what the true-false value is by using bool() 70 | 71 | Let's try it out: 72 | ```python 73 | >>> bool(False) 74 | False 75 | >>> bool(True) 76 | True 77 | >>> bool(0) 78 | False 79 | >>> bool(7) 80 | True 81 | >>> bool(None) 82 | False 83 | >>> bool(7 == 0) 84 | False 85 | >>> 7 == 0 86 | False 87 | ``` 88 | 89 | # Strings 90 | Last stop on our journey into basic data types: Strings 91 | A string is a sequence of characters, for example "Hello" or "My id # is 1235!" 92 | Let's try a few in our shell: 93 | ```python 94 | >>> "Hello World!" 95 | Hello World! 96 | >>> "Hello" + " World!" 97 | >>> 1 + "world" 98 | ``` 99 | 100 | # Practice problem 101 | ``` 102 | - Set a variable called `name` equal to your full name. 103 | - Set a variable called `first_name` equal to your first name. 104 | - Set a variable called `last_name` equal to your last name. 105 | - Then combine `first_name` and `last_name` so that it equals your full name, and save this in a variable called `full_name`. 106 | - Prove using the == operator that they are equal. 107 | ``` 108 | 109 | # Lists 110 | Lists is a super useful data type. 111 | It's an ordered collection of items, which you can modify by adding or removing elements. 112 | 113 | Let's learn the syntax of a list: 114 | ```python 115 | >>> x = [1, 2, 3, 4, 5] 116 | >>> len(x) 117 | 5 118 | >>> # what's between the brackets is called the index 119 | >>> x[0] 120 | 1 121 | >>> x[1] 122 | 2 123 | >>> x[4] 124 | 5 125 | >>> x[6] 126 | IndexError 127 | >>> x.append("hello") 128 | >>> len(x) 129 | 6 130 | >>> x[5] 131 | "Hello" 132 | >>> # can remove elements, defaults to popping off the last one 133 | >>> x.pop() 134 | >>> print x 135 | >>> # can specify the index 136 | >> x.pop(2) 137 | >>> print x 138 | >>> # can do a boolean check if something is in the list 139 | >>> 5 in x 140 | False 141 | >>> 1 in x 142 | ``` 143 | 144 | # Practice problem 145 | ``` 146 | - Create a list called `restaurants` consisting of the following elements in this order: 147 | "Laut", "Random String", "Chipotle", "Eataly", "Sophie's Cuban", "Chop't", "Potbelly's" 148 | - Get the third element of the list 149 | - Add "Ootoya" to the end of the list 150 | - Use .pop() to remove "Random String" from the list 151 | - Print the list 152 | - Check if 'Chipotle' is in the list 153 | - Check if 'Random String' is in the list 154 | - Get the length of the list 155 | ``` 156 | 157 | # Dictionaries 158 | Dictionaries are also massively useful. You can think of them as maps, between as set of keys and their corresponding values. 159 | Let's look at one in the shell to better understand how they work and how they can be useful: 160 | 161 | ```python 162 | >>> # Here's the syntax, {} with key:value, with key being a string. 163 | >>> person = {"name": "John Jameson", "age": 31, "hobbies": ["tennis", "python", "piano"]} 164 | >>> person["name"] 165 | >>> # like a string, we use the brackets but instead of the index, we provide the key name 166 | >>> person["age"] 167 | >>> hobbies = person["hobbies"] 168 | >>> len(hobbies) 169 | >>> hobbies[1] 170 | >>> person["random"] 171 | KeyError 172 | >>> person["location"] = "New York" 173 | >>> person["hometown"] = "Jupiter, Florida" 174 | print person 175 | >>> del person["hometown"] 176 | print person 177 | >>> person.keys() 178 | >>> person.values() 179 | >>> person.items() 180 | >>> # check if a key exists in the dictionary 181 | >>> "name" in person 182 | >>> "secret" in person 183 | ``` 184 | 185 | # Practice problem 186 | ``` 187 | Create a dictionary called `class_data` with the following keys: 188 | - "course_name", which should correspond to "Betaworks Intro to Python" 189 | - "student_count", which should correspond to number of students, say 20 190 | - "instructor", which should itself be a dictionary with the following keys 191 | - "name" ("Suneel") 192 | - "can_program" (True) 193 | - get the student count from the dictionary 194 | - get the instructor name from the dictionary 195 | ``` 196 | 197 | # If/Else 198 | If/else statements are blocks of our code that allow us to do different things based on some logical condition 199 | Let's learn the syntax, note INDENTATION is important: 200 | ```python 201 | >>> x = 5 202 | >>> if x > 4: 203 | print "Hello" 204 | else: 205 | print "World" 206 | Hello 207 | >>> if x < 5: 208 | print "Less than five" 209 | else: 210 | print "Greater than or equal to five" 211 | >>> if x == 1 or x == 2: 212 | print "Ha" 213 | elif x == 3: 214 | print "Hey" 215 | else: 216 | print "Hi" 217 | ``` 218 | 219 | # Practice problem: 220 | ``` 221 | Given x = "John Adams", 222 | - Construct an if else statement according to this logic: if "John" is in x, print "John is in x", otherwise print "John is not in x" 223 | - Construct an if/elif/else statement according to this logic: if "roger" is in x, print "Hi Roger!", elif the length of x is greater than 20, print "thats a long string", else print "Oh well!" 224 | ``` 225 | 226 | # Loops 227 | Loops let us pass through a set of values and do some operation on each. 228 | There are different kinds of loops, for loops and while loops. They're quite similar, but let's look at the canonical loop, the for loop. 229 | 230 | ```python 231 | >>> numbers = [1, 2, 8, 7, 9, 10] 232 | >>> odds = [] 233 | >>> for zebra in numbers: 234 | if zebra % 2 == 1: 235 | odds.append(zebra) 236 | 237 | print odds 238 | ``` 239 | - the `number` in the for loop syntax is a dummy variable that refers to the element in the list that we're passing through. The first time around, number refers to 1, the last time it refers to 10. 240 | - we are going through this list and if the number is odd, we add it to the odds list that we initialized before the for loop. 241 | 242 | Before we move on to some practice problems, here is a useful function: 243 | ```python 244 | >>> range(10) 245 | ``` 246 | 247 | # Practice problems 248 | ``` 249 | - Construct a list of numbers between 0 and 1000 that are divisible by 33 250 | - Given the following list 251 | ["Donald Duck", "Mickey Mouse", "Daffy Duck", "Goofy", "Minnie Mouse", "Pluto"] 252 | Get the sum of all of the lengths of these strings. 253 | - Given the above list, create a list of all of the elements that have the letter "d" or the letter "m" in them 254 | ``` 255 | 256 | # Functions 257 | Functions are the heart and soul of python. Functions are blocks of code that take an input and based on some rules produce an output. 258 | 259 | Let's learn the syntax of functions: 260 | 261 | ```python 262 | >>> def add(a, b): 263 | return a + b 264 | >>> z = add(3, 4) 265 | >>> print z 266 | ``` 267 | a and b are variables that the function `add` takes 268 | 269 | Practice problems: 270 | ``` 271 | - create function `multiply` that takes two variables and returns their product 272 | - create function `subtract` that takes two variables and returns their difference 273 | - write a function that takes a variable, which is a list of numbers, and then returns just the ones that are divisible by 33. 274 | - write a function that takes a list of numbers and returns True if the sum of the numbers is even and False otherwise. 275 | ``` 276 | 277 | # Using Other Libraries 278 | What is a library? A reusable, collection of code that someone else (or you) has already written. Some great built-in libraries: 279 | - `random` 280 | - `csv` 281 | - `collections` 282 | - `datetime` 283 | 284 | To use other libraries, we need to be able to: 285 | - understand the documentation of that library 286 | - understand what the inputs and outputs of their functions are 287 | - how to call those functions correctly 288 | 289 | Let's start with the random library. 290 | 291 | ## The `random` library. 292 | Our first step is to locate the documentation. Google "python random". It should take you [here](https://docs.python.org/2/library/random.html) 293 | 294 | Let's import the library 295 | ```python 296 | >>> import random 297 | >>> dir(random) 298 | ``` 299 | 300 | How does python know what `random` is and how to find the code? Because it comes built-in to the Python language. Other libraries such as `pandas` will have to be installed prior to use. 301 | 302 | ### Exercises 303 | 1. Find the `randint` function in the documentation and explain to your neighbor what it does and how to use it. 304 | 2. Again, with your partner, use the `randint` function to generate a random number between 1 and 125. 305 | 306 | 307 | # What's next? 308 | - Web development: [Django](http://djangoproject.com) or [Flask](http://flask.pocoo.org/) 309 | - Data science: [pandas](http://pandas.pydata.org/) 310 | - Tutorial: [Learn Python the Hard Way](http://learnpythonthehardway.org/book/) 311 | - [Practice problems with full solutions](https://github.com/suneel0101/python-powerup) 312 | - Practice problems: [Project Euler](https://projecteuler.net/) --------------------------------------------------------------------------------