|
224 | 224 | "print(my_dict)" |
225 | 225 | ] |
226 | 226 | }, |
| 227 | + { |
| 228 | + "cell_type": "markdown", |
| 229 | + "metadata": {}, |
| 230 | + "source": [ |
| 231 | + "## Big picture\n", |
| 232 | + "\n", |
| 233 | + "Data structures are variables with more \"stuff\" in them than a single number or string. That way you don't have to label every single value you store. To get specific items out, it's always square brackets.\n", |
| 234 | + "\n", |
| 235 | + "var_name[stuff here]\n" |
| 236 | + ] |
| 237 | + }, |
| 238 | + { |
| 239 | + "cell_type": "code", |
| 240 | + "execution_count": 6, |
| 241 | + "metadata": {}, |
| 242 | + "outputs": [], |
| 243 | + "source": [ |
| 244 | + "# From Intro_assignment.ipynb, dictionary probelm\n", |
| 245 | + "# Creating one variable for each value you want to store\n", |
| 246 | + "\n", |
| 247 | + "name = \"Me\"\n", |
| 248 | + "age = 10\n", |
| 249 | + "first_grade = \"A\"\n", |
| 250 | + "second_grade = \"A-\"\n", |
| 251 | + "third_grade = \"B+\"" |
| 252 | + ] |
| 253 | + }, |
| 254 | + { |
| 255 | + "cell_type": "code", |
| 256 | + "execution_count": null, |
| 257 | + "metadata": {}, |
| 258 | + "outputs": [], |
| 259 | + "source": [ |
| 260 | + "# Grades are a good candidate for a list - they're already numbered\n", |
| 261 | + "grades = [\"A\", \"A-\", \"B+\"]\n", |
| 262 | + "\n", |
| 263 | + "# Or you could do - it's the same as above\n", |
| 264 | + "grades = [first_grade, second_grade, third_grade]\n", |
| 265 | + "\n", |
| 266 | + "# Notice the [] in the print statement, and the 'A' instead of A\n", |
| 267 | + "print(f\"Grades (individual variables) {first_grade}, {second_grade}, {third_grade}\")\n", |
| 268 | + "print(f\"Grades (list) {grades}\")\n", |
| 269 | + "\n" |
| 270 | + ] |
| 271 | + }, |
| 272 | + { |
| 273 | + "cell_type": "code", |
| 274 | + "execution_count": null, |
| 275 | + "metadata": {}, |
| 276 | + "outputs": [], |
| 277 | + "source": [ |
| 278 | + "# Edit the variables\n", |
| 279 | + "# Make the first grade be an A+\n", |
| 280 | + "first_grade = first_grade + \"+\"\n", |
| 281 | + "\n", |
| 282 | + "# Do the same thing for the list - edit the first element (notice the [0])\n", |
| 283 | + "grades[0] = grades[0] + \"+\"\n", |
| 284 | + "\n", |
| 285 | + "# Re-create the list from the variables - remember that first_grade has changed\n", |
| 286 | + "grades_same_thing = [first_grade, second_grade, third_grade]\n", |
| 287 | + "\n", |
| 288 | + "# Notice that all the A's were changed to A+'s\n", |
| 289 | + "print(f\"Grades (individual variables) {first_grade}, {second_grade}, {third_grade}\")\n", |
| 290 | + "print(f\"Grades (list) {grades}\")\n" |
| 291 | + ] |
| 292 | + }, |
| 293 | + { |
| 294 | + "cell_type": "code", |
| 295 | + "execution_count": null, |
| 296 | + "metadata": {}, |
| 297 | + "outputs": [], |
| 298 | + "source": [ |
| 299 | + "# Adding another variable\n", |
| 300 | + "\n", |
| 301 | + "# If you're keeping individual variables, you have to make another variable\n", |
| 302 | + "fourth_grade = \"B\"\n", |
| 303 | + "\n", |
| 304 | + "# If you have a list, just append to the list\n", |
| 305 | + "grades.append(\"B\")\n", |
| 306 | + "\n", |
| 307 | + "# Notice that we have to add another {fourth_grade}\n", |
| 308 | + "print(f\"Grades (individual variables) {first_grade}, {second_grade}, {third_grade}, {fourth_grade}\")\n", |
| 309 | + "# This just works\n", |
| 310 | + "print(f\"Grades (list) {grades}\")\n" |
| 311 | + ] |
| 312 | + }, |
227 | 313 | { |
228 | 314 | "cell_type": "code", |
229 | 315 | "execution_count": null, |
230 | 316 | "metadata": {}, |
231 | 317 | "outputs": [], |
232 | | - "source": [] |
| 318 | + "source": [ |
| 319 | + "# Now let's do the same thing for the dictionary\n", |
| 320 | + "# All three of these do the same thing, btw\n", |
| 321 | + "# Make the whole list using key:value, key:value, all in {}\n", |
| 322 | + "my_grade_stuff = {\"name\": \"Me\", \"age\": 10, \"grades\": [\"A\", \"A-\", \"B+\"]}\n", |
| 323 | + "print(f\"Grades version 1 {my_grade_stuff}\")\n", |
| 324 | + "\n", |
| 325 | + "# The same thing, but this time use the variables as the values\n", |
| 326 | + "# TODO: Why is this slightly different than the one above? Reminder that\n", |
| 327 | + "# we edited grades to have 4 grades...\n", |
| 328 | + "my_grade_stuff = {\"name\": name, \"age\": age, \"grades\": grades}\n", |
| 329 | + "print(f\"Grades version 2 {my_grade_stuff}\")\n", |
| 330 | + "\n", |
| 331 | + "# One more time, this time making an empty dictionary and adding stuff \n", |
| 332 | + "# in one at a time\n", |
| 333 | + "my_grade_stuff = {}\n", |
| 334 | + " # Notice that the [\"foo\"] is on the left-hand side - this *makes*\n", |
| 335 | + " # the key, value pair in the dictionary, instead of *getting* it\n", |
| 336 | + "my_grade_stuff[\"name\"] = name\n", |
| 337 | + "my_grade_stuff[\"age\"] = age\n", |
| 338 | + "my_grade_stuff[\"grades\"] = grades\n", |
| 339 | + "print(f\"Grades version 3 {my_grade_stuff}\")\n" |
| 340 | + ] |
| 341 | + }, |
| 342 | + { |
| 343 | + "cell_type": "code", |
| 344 | + "execution_count": 13, |
| 345 | + "metadata": {}, |
| 346 | + "outputs": [], |
| 347 | + "source": [ |
| 348 | + "# Now do an edit of the age\n", |
| 349 | + "# These do the same thing - but one edits the age variable, one\n", |
| 350 | + "# edits the age value in with the key \"age\"\n", |
| 351 | + "age = age + 3 # Add 3 to the age variable\n", |
| 352 | + "my_grade_stuff[\"age\"] = my_grade_stuff[\"age\"] + 3\n", |
| 353 | + "\n", |
| 354 | + "# You could do this, if it's helpful to think of it this way\n", |
| 355 | + "my_current_age = my_grade_stuff[\"age\"] # Get the current age\n", |
| 356 | + "my_current_age = my_current_age + 3\n", |
| 357 | + "my_grade_stuff[\"age\"] = my_current_age" |
| 358 | + ] |
233 | 359 | } |
234 | 360 | ], |
235 | 361 | "metadata": { |
|
248 | 374 | "name": "python", |
249 | 375 | "nbconvert_exporter": "python", |
250 | 376 | "pygments_lexer": "ipython3", |
251 | | - "version": "3.7.4" |
| 377 | + "version": "3.12.7" |
252 | 378 | } |
253 | 379 | }, |
254 | 380 | "nbformat": 4, |
|
0 commit comments