|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "metadata": {}, |
| 5 | + "cell_type": "markdown", |
| 6 | + "source": [ |
| 7 | + "## Data Structures\n", |
| 8 | + "\n", |
| 9 | + "A \"Data Structure\" is just a fancy way of saying, \"I put some information in a container.\" Conveniently, it makes me seem more professional and fancy. Python has a few types of Data Structures, but the most common ones are lists, tuples, and dictionaries. These structures let you store and organize data in different ways, making it easier to work with.\n", |
| 10 | + "\n", |
| 11 | + "### Lists\n", |
| 12 | + "Lists are ordered collections of items that can be changed (mutable). You can think of them as a shopping list where you can add, remove, or change items. Lists are defined using square brackets `[]`, and you can access items using their index (starting from 0), like this: `list[n]`. Each item in a list is called an \"element.\" You can have as many elements as you can fit in your array without running out of memory. By the way, this is called a \"Stack Overflow\" error, and it happens when you try to use more memory than your computer has available in the RAM. It's like trying to fit two gallons of milk into a one-gallon milk jug, it just doesn't work.\n", |
| 13 | + " Anyone who thinks that lists should be 1-indexed is insane or uses Lua. The two are not mutually exclusive.\n", |
| 14 | + "Anyways, here's an example of a list:\n" |
| 15 | + ], |
| 16 | + "id": "7f5460c23543de90" |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "code", |
| 20 | + "execution_count": null, |
| 21 | + "id": "initial_id", |
| 22 | + "metadata": { |
| 23 | + "collapsed": true |
| 24 | + }, |
| 25 | + "outputs": [], |
| 26 | + "source": [ |
| 27 | + "list_of_fruits = [\"apple\", \"banana\", \"cherry\"]\n", |
| 28 | + "print(list_of_fruits[0]) # Access the first item\n", |
| 29 | + "print(list_of_fruits[-1])" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "metadata": {}, |
| 34 | + "cell_type": "markdown", |
| 35 | + "source": [ |
| 36 | + "Now, you might be thinking, \"What the heck, Jay? There are no negative elements in the list!\" Well, you're right. However, Python allows you to access elements from the front _and_ the back of a list. In Java, if you wanted to access the last element of a list, you would have to do `list.get(list.size() - 1)`. What excessive bloat for just accessing the last element of a list.\n", |
| 37 | + "\n", |
| 38 | + "But in Python, you can just do `list[-1]` to get the last element. This is because Python allows negative indexing, where `-1` refers to the last item, `-2` refers to the second-to-last item, and so on. Now, in JavaScript, you can straight up have negative indexes in your list, but that's a whole different topic.\n", |
| 39 | + "\n", |
| 40 | + "You can also add items to a list using the `append(element)` method, remove items using the `remove(element)` or `pop(index)` methods, and sort the list using the `sort()` method. Here's an example:" |
| 41 | + ], |
| 42 | + "id": "1df39f3d09ca66d9" |
| 43 | + }, |
| 44 | + { |
| 45 | + "metadata": {}, |
| 46 | + "cell_type": "code", |
| 47 | + "outputs": [], |
| 48 | + "execution_count": null, |
| 49 | + "source": [ |
| 50 | + "students = [\"Jay\", \"Reuben\", \"Alex\"]\n", |
| 51 | + "# trying appending your name here!\n", |
| 52 | + "students.append(\"Noah\")\n", |
| 53 | + "print(students) # Output: ['Jay', 'Reuben', 'Alex', 'Noah']\n", |
| 54 | + "students.remove(\"Reuben\")\n", |
| 55 | + "print(students) # Output: ['Jay', 'Alex', 'Noah']\n", |
| 56 | + "students.sort() # Sorts the list in alphabetical order\n", |
| 57 | + "print(students) # Output: ['Alex', 'Jay', 'Noah']" |
| 58 | + ], |
| 59 | + "id": "f691020fa9c6d982" |
| 60 | + }, |
| 61 | + { |
| 62 | + "metadata": {}, |
| 63 | + "cell_type": "markdown", |
| 64 | + "source": "Python also has a few built-in functions that work with lists, such as `len(list)` to get the number of elements in a list, the <i>len</i>gth `min(list)` to get the smallest element (*min*imum), and `max(list)` to get the largest element (*max*imum). Try running the following code block:", |
| 65 | + "id": "4da42bee367d6db5" |
| 66 | + }, |
| 67 | + { |
| 68 | + "metadata": {}, |
| 69 | + "cell_type": "code", |
| 70 | + "outputs": [], |
| 71 | + "execution_count": null, |
| 72 | + "source": [ |
| 73 | + "my_grades=[69,42, 21, 67, 100, 98, 82]\n", |
| 74 | + "print(\"I have\", len(my_grades), \"grades.\")\n", |
| 75 | + "print(\"My lowest grade is a\", min(my_grades)) # worse than not doing your assignment bru\n", |
| 76 | + "print(\"My highest grade is a\", max(my_grades))" |
| 77 | + ], |
| 78 | + "id": "2b47c43356770265" |
| 79 | + }, |
| 80 | + { |
| 81 | + "metadata": {}, |
| 82 | + "cell_type": "markdown", |
| 83 | + "source": [ |
| 84 | + "### Exercise: Average of a List\n", |
| 85 | + "\n", |
| 86 | + "In the following code block, try calculating the average of the numbers in the list `data`. If you're in high school, surely you know how to calculate an average, right? If not, here's a quick reminder: To find a list of numbers' average, you add up all the numbers and then divide by the count of numbers in the list." |
| 87 | + ], |
| 88 | + "id": "b47bfd40ce8f6513" |
| 89 | + }, |
| 90 | + { |
| 91 | + "metadata": {}, |
| 92 | + "cell_type": "code", |
| 93 | + "outputs": [], |
| 94 | + "execution_count": null, |
| 95 | + "source": [ |
| 96 | + "data = [3.6, 3.14, 2.9, 6.33, 4.1, 8.9, 5.3, 7.3, 1.56, 8.1]\n", |
| 97 | + "average = 0.0\n", |
| 98 | + "#\n", |
| 99 | + "#\n", |
| 100 | + "# your code here!\n", |
| 101 | + "#\n", |
| 102 | + "#\n", |
| 103 | + "print(\"The average is:\", average) # you should get sm around 5.123" |
| 104 | + ], |
| 105 | + "id": "129eb26d7df4c0c7" |
| 106 | + }, |
| 107 | + { |
| 108 | + "metadata": {}, |
| 109 | + "cell_type": "markdown", |
| 110 | + "source": [ |
| 111 | + "## Tuples\n", |
| 112 | + "A tuple is similar to a list, but it is immutable. That means you cannot change the contents of the list once you've declared it. Tuples are defined using parentheses `()`, and you can access items using their index, just like lists. Tuples are often used to store ordered, related pieces of data that should not change, like coordinates or RGB color values. Here's an example:" |
| 113 | + ], |
| 114 | + "id": "75ec9bf50b678774" |
| 115 | + }, |
| 116 | + { |
| 117 | + "metadata": {}, |
| 118 | + "cell_type": "code", |
| 119 | + "outputs": [], |
| 120 | + "execution_count": null, |
| 121 | + "source": [ |
| 122 | + "GREEN = (0, 255, 0) # RGB color value for green\n", |
| 123 | + "ORIGIN = (0,0) # Coordinates for the origin point\n", |
| 124 | + "print(\"The RGB value for green is:\", GREEN)\n", |
| 125 | + "print(\"The origin point is:\", ORIGIN)" |
| 126 | + ], |
| 127 | + "id": "1886431322da6920" |
| 128 | + }, |
| 129 | + { |
| 130 | + "metadata": {}, |
| 131 | + "cell_type": "markdown", |
| 132 | + "source": "Tuples can have duplicates, along with multiple types of data. For example, you can have a tuple that contains a string, an integer, and a float. You can also have a tuple that contains only one element, but you must include a comma at the end to differentiate it from a regular parenthesis.", |
| 133 | + "id": "52d58a40a70cdca3" |
| 134 | + }, |
| 135 | + { |
| 136 | + "metadata": {}, |
| 137 | + "cell_type": "code", |
| 138 | + "outputs": [], |
| 139 | + "execution_count": null, |
| 140 | + "source": [ |
| 141 | + "single_element_tuple = (42,) # A tuple with one element\n", |
| 142 | + "mixed_tuple = (\"apple\", 3, 4.5, (6.9,2)) # A tuple with some different data types\n", |
| 143 | + "# and yes, you can have nested tuples, like if you needed a set of coordinate points for a polygon" |
| 144 | + ], |
| 145 | + "id": "9e7746b2f36ef157" |
| 146 | + }, |
| 147 | + { |
| 148 | + "metadata": {}, |
| 149 | + "cell_type": "markdown", |
| 150 | + "source": [ |
| 151 | + "### Some fun tuple methods\n", |
| 152 | + "\n", |
| 153 | + "Haven't you ever wanted to...uh...add two tuples together? I sure haven't, but there's got to be a use case for it. You can add tuples together like strings using the `+` operator. This will concatenate the two tuples together, creating a new tuple that contains all the elements from both tuples." |
| 154 | + ], |
| 155 | + "id": "7655ca21de2974bb" |
| 156 | + }, |
| 157 | + { |
| 158 | + "metadata": {}, |
| 159 | + "cell_type": "code", |
| 160 | + "outputs": [], |
| 161 | + "execution_count": null, |
| 162 | + "source": [ |
| 163 | + "my_shopping_list = (\"laptop\", \"mouse\", \"kitten\", \"4 count monster energy\")\n", |
| 164 | + "alex_yin_shopping_list = (\"violin\", \"robux for deepwoken\", \"hershey bar\")\n", |
| 165 | + "\n", |
| 166 | + "combined_shopping_list = my_shopping_list + alex_yin_shopping_list\n", |
| 167 | + "print(\"Our shopping list:\", my_shopping_list)" |
| 168 | + ], |
| 169 | + "id": "95669f08f5064b95" |
| 170 | + }, |
| 171 | + { |
| 172 | + "metadata": {}, |
| 173 | + "cell_type": "markdown", |
| 174 | + "source": "I've also recently been informed that there exists a way to multiply tuples. Why you would need to use this, I have no idea. I have racked my brain for a use case, but it's even harder to think of than finding a use case for adding two tuples together. Anyways, multiplying a tuple by an integer will repeat the elements of the tuple that n times, creating a new tuple.", |
| 175 | + "id": "c23759f95b095c9" |
| 176 | + }, |
| 177 | + { |
| 178 | + "metadata": {}, |
| 179 | + "cell_type": "code", |
| 180 | + "outputs": [], |
| 181 | + "execution_count": null, |
| 182 | + "source": [ |
| 183 | + "tuple1 = (\"ha\",)\n", |
| 184 | + "tuple2 = tuple1 * 5\n", |
| 185 | + "print(tuple2) # Output: ('ha', 'ha', 'ha', 'ha', 'ha')\n", |
| 186 | + "# see, i am not laughing because this tuple multiplication is so funny (its not)" |
| 187 | + ], |
| 188 | + "id": "55822ee0c748aafb" |
| 189 | + }, |
| 190 | + { |
| 191 | + "metadata": {}, |
| 192 | + "cell_type": "markdown", |
| 193 | + "source": [ |
| 194 | + "### unpacking tuples\n", |
| 195 | + "\n", |
| 196 | + "tuples can be \"unpacked\" from their little casing into different variables. let's say that you had a tuple that stored the x and y coordinates of a point, like `(6,7)`. You can unpack this tuple into two separate variables like this:" |
| 197 | + ], |
| 198 | + "id": "6d125c0a65c90ae7" |
| 199 | + }, |
| 200 | + { |
| 201 | + "metadata": {}, |
| 202 | + "cell_type": "code", |
| 203 | + "outputs": [], |
| 204 | + "execution_count": null, |
| 205 | + "source": [ |
| 206 | + "coordinate1 = (6,7)\n", |
| 207 | + "x, y = coordinate1 # Unpacking the tuple into x and y variables\n", |
| 208 | + "print(\"The x coordinate is:\", x)\n", |
| 209 | + "print(\"The y coordinate is:\", y)" |
| 210 | + ], |
| 211 | + "id": "18a93f41ce1fe553" |
| 212 | + }, |
| 213 | + { |
| 214 | + "metadata": {}, |
| 215 | + "cell_type": "markdown", |
| 216 | + "source": [ |
| 217 | + "### Exercise with tuples: distance between two points\n", |
| 218 | + "\n", |
| 219 | + "The distance between two points in a 2d space can be calculated using the pythagorean theorem. You can think about it like a triangle, where the bottom leg is the difference in the x coordinates, and the side leg is the difference in y coordinates, and the hypotenuse is the distance.\n", |
| 220 | + "\n", |
| 221 | + "You can use `math.sqrt()` for a square root function, and `x**2` for squaring a number.\n", |
| 222 | + "In the code block below, write some code that will calculate the distance of two points as a tuple." |
| 223 | + ], |
| 224 | + "id": "13c38b7388024f2a" |
| 225 | + }, |
| 226 | + { |
| 227 | + "metadata": {}, |
| 228 | + "cell_type": "code", |
| 229 | + "outputs": [], |
| 230 | + "execution_count": null, |
| 231 | + "source": [ |
| 232 | + "n=6\n", |
| 233 | + "print(n^2) # MAKE SURE NOT TO DO THIS!! this is a bitwise XOR operation, not squaring a number!!\n", |
| 234 | + "print(n ** 2) # this is the correct way to do exponentiation\n", |
| 235 | + "\n", |
| 236 | + "point_1=(3, 4) # x1 and y1 coordinates\n", |
| 237 | + "point_2=(-8, 5)\n", |
| 238 | + "# your code here!!" |
| 239 | + ], |
| 240 | + "id": "9d7a07cd1d46ae14" |
| 241 | + }, |
| 242 | + { |
| 243 | + "metadata": {}, |
| 244 | + "cell_type": "markdown", |
| 245 | + "source": [ |
| 246 | + "## Dictionaries\n", |
| 247 | + "Dictionaries are ordered collections of key-value pairs. They are like a real-life dictionary, where you look up a word (the key) to find its definition (the value). Dictionaries are defined using curly braces `{}`, and you can access values using their keys, like this: `dict[key]`. This is very helpful when you are storing data that has a specific relationship between keys and values, like a phone book or a contact list. Instead of making two lists called `names` and `phone_numbers`, you can just make a dictionary called `contacts` that has the names as keys and the phone numbers as values." |
| 248 | + ], |
| 249 | + "id": "d7ec5d0487a48e84" |
| 250 | + }, |
| 251 | + { |
| 252 | + "metadata": {}, |
| 253 | + "cell_type": "code", |
| 254 | + "outputs": [], |
| 255 | + "execution_count": null, |
| 256 | + "source": [ |
| 257 | + "contacts = {\n", |
| 258 | + " \"Jay\": \"123-456-7890\",\n", |
| 259 | + " \"Reuben\": \"987-654-3210\",\n", |
| 260 | + " \"Alex\": \"555-555-5555\",\n", |
| 261 | + " \"bbno$\" : \"1-800-HIT-MY-LINE\",\n", |
| 262 | + "}" |
| 263 | + ], |
| 264 | + "id": "1c04423957675e8f" |
| 265 | + }, |
| 266 | + { |
| 267 | + "metadata": {}, |
| 268 | + "cell_type": "markdown", |
| 269 | + "source": "Dictionaries can have keys and values of different data types, including strings, integers, floats, lists, and even other dictionaries. However, the biggest thing you need to remember is that dictionaries cannot have duplicate keys. If you try to add a key that already exists in the dictionary, it will overwrite the existing value with the new value. It's just like how you can't have two people with the same name in your class, you'll never be able to tell them apart.", |
| 270 | + "id": "359cda4ca364e0b6" |
| 271 | + }, |
| 272 | + { |
| 273 | + "metadata": { |
| 274 | + "ExecuteTime": { |
| 275 | + "end_time": "2025-08-20T13:52:57.750298Z", |
| 276 | + "start_time": "2025-08-20T13:52:57.742823Z" |
| 277 | + } |
| 278 | + }, |
| 279 | + "cell_type": "code", |
| 280 | + "source": [ |
| 281 | + "classroom = {\n", |
| 282 | + " \"Benn\": 101,\n", |
| 283 | + " \"Frederick\": 102,\n", |
| 284 | + " \"Sir John Marshall Harlan Bunting XIVV Junior\": 103,\n", |
| 285 | + " \"Steve\": 104,\n", |
| 286 | + " \"John\": 105, # notice how this gets overwritten\n", |
| 287 | + " \"John\": 106,\n", |
| 288 | + "}\n", |
| 289 | + "print(classroom)" |
| 290 | + ], |
| 291 | + "id": "83e61f1027bad569", |
| 292 | + "outputs": [ |
| 293 | + { |
| 294 | + "name": "stdout", |
| 295 | + "output_type": "stream", |
| 296 | + "text": [ |
| 297 | + "{'Benn': 101, 'Frederick': 102, 'Sir John Marshall Harlan Bunting XIVV Junior': 103, 'Steve': 104, 'John': 106}\n" |
| 298 | + ] |
| 299 | + } |
| 300 | + ], |
| 301 | + "execution_count": 2 |
| 302 | + }, |
| 303 | + { |
| 304 | + "metadata": {}, |
| 305 | + "cell_type": "markdown", |
| 306 | + "source": [ |
| 307 | + "## Methods for Data Structures\n", |
| 308 | + "\n", |
| 309 | + "- `in` operator: Checks if an item is in a list or tuple.\n", |
| 310 | + "- `index(item)`: Returns the index of the first occurrence of an item in a list.\n", |
| 311 | + "- `count(item)`: Returns the number of occurrences of an item in a list.\n", |
| 312 | + "- `join()`: Joins elements of a list into a string\n" |
| 313 | + ], |
| 314 | + "id": "1825c37cce7b82d7" |
| 315 | + }, |
| 316 | + { |
| 317 | + "metadata": {}, |
| 318 | + "cell_type": "code", |
| 319 | + "outputs": [], |
| 320 | + "execution_count": null, |
| 321 | + "source": [ |
| 322 | + "students = [\"Benn\", \"Frederick\", \"Jarquantavius\", \"Steve\", \"John\", \"John\", \"John\"]\n", |
| 323 | + "print(\"Class Roster: \", \", \".join(students)) # Joins the list into a string\n", |
| 324 | + "print(\"Is Benn a student?\", \"Benn\" in students) # Checks if \"Benn\" is in the list\n", |
| 325 | + "print(\"Index of Steve:\", students.index(\"Steve\")) # Gets the index of \"Steve\"\n", |
| 326 | + "print(\"Number of times 'John' appears:\", students.count(\"John\")) # how many times john is in the list" |
| 327 | + ], |
| 328 | + "id": "1f326905b82a434b" |
| 329 | + } |
| 330 | + ], |
| 331 | + "metadata": { |
| 332 | + "kernelspec": { |
| 333 | + "display_name": "Python 3", |
| 334 | + "language": "python", |
| 335 | + "name": "python3" |
| 336 | + }, |
| 337 | + "language_info": { |
| 338 | + "codemirror_mode": { |
| 339 | + "name": "ipython", |
| 340 | + "version": 2 |
| 341 | + }, |
| 342 | + "file_extension": ".py", |
| 343 | + "mimetype": "text/x-python", |
| 344 | + "name": "python", |
| 345 | + "nbconvert_exporter": "python", |
| 346 | + "pygments_lexer": "ipython2", |
| 347 | + "version": "2.7.6" |
| 348 | + } |
| 349 | + }, |
| 350 | + "nbformat": 4, |
| 351 | + "nbformat_minor": 5 |
| 352 | +} |
0 commit comments