|
374 | 374 | "print(empty_dict.keys())"
|
375 | 375 | ]
|
376 | 376 | },
|
| 377 | + { |
| 378 | + "cell_type": "code", |
| 379 | + "execution_count": 25, |
| 380 | + "metadata": {}, |
| 381 | + "outputs": [ |
| 382 | + { |
| 383 | + "name": "stdout", |
| 384 | + "output_type": "stream", |
| 385 | + "text": [ |
| 386 | + "Before dictionary is updated\n", |
| 387 | + "dict_keys(['name', 'age'])\n", |
| 388 | + "\n", |
| 389 | + "After dictionary is updated\n", |
| 390 | + "dict_keys(['name', 'age', 'salary'])\n" |
| 391 | + ] |
| 392 | + } |
| 393 | + ], |
| 394 | + "source": [ |
| 395 | + "person = {'name': 'Phill', 'age': 22, }\n", |
| 396 | + "\n", |
| 397 | + "print('Before dictionary is updated')\n", |
| 398 | + "keys = person.keys()\n", |
| 399 | + "print(keys)\n", |
| 400 | + "\n", |
| 401 | + "person.update({'salary': 3500.0})\n", |
| 402 | + "print('\\nAfter dictionary is updated')\n", |
| 403 | + "print(keys)" |
| 404 | + ] |
| 405 | + }, |
| 406 | + { |
| 407 | + "cell_type": "code", |
| 408 | + "execution_count": 26, |
| 409 | + "metadata": {}, |
| 410 | + "outputs": [ |
| 411 | + { |
| 412 | + "name": "stdout", |
| 413 | + "output_type": "stream", |
| 414 | + "text": [ |
| 415 | + "dict_items([('apple', 2), ('orange', 3), ('grapes', 4)])\n" |
| 416 | + ] |
| 417 | + } |
| 418 | + ], |
| 419 | + "source": [ |
| 420 | + "sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }\n", |
| 421 | + "\n", |
| 422 | + "print(sales.items())" |
| 423 | + ] |
| 424 | + }, |
| 425 | + { |
| 426 | + "cell_type": "code", |
| 427 | + "execution_count": 27, |
| 428 | + "metadata": {}, |
| 429 | + "outputs": [ |
| 430 | + { |
| 431 | + "name": "stdout", |
| 432 | + "output_type": "stream", |
| 433 | + "text": [ |
| 434 | + "Original items: dict_items([('apple', 2), ('orange', 3), ('grapes', 4)])\n", |
| 435 | + "Updated items: dict_items([('orange', 3), ('grapes', 4)])\n" |
| 436 | + ] |
| 437 | + } |
| 438 | + ], |
| 439 | + "source": [ |
| 440 | + "sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }\n", |
| 441 | + "\n", |
| 442 | + "items = sales.items()\n", |
| 443 | + "print('Original items:', items)\n", |
| 444 | + "\n", |
| 445 | + "del[sales['apple']]\n", |
| 446 | + "print('Updated items:', items)" |
| 447 | + ] |
| 448 | + }, |
| 449 | + { |
| 450 | + "cell_type": "code", |
| 451 | + "execution_count": 28, |
| 452 | + "metadata": {}, |
| 453 | + "outputs": [ |
| 454 | + { |
| 455 | + "data": { |
| 456 | + "text/plain": [ |
| 457 | + "{'orange': 3, 'grapes': 4}" |
| 458 | + ] |
| 459 | + }, |
| 460 | + "execution_count": 28, |
| 461 | + "metadata": {}, |
| 462 | + "output_type": "execute_result" |
| 463 | + } |
| 464 | + ], |
| 465 | + "source": [ |
| 466 | + "sales" |
| 467 | + ] |
| 468 | + }, |
| 469 | + { |
| 470 | + "cell_type": "code", |
| 471 | + "execution_count": 29, |
| 472 | + "metadata": {}, |
| 473 | + "outputs": [ |
| 474 | + { |
| 475 | + "name": "stdout", |
| 476 | + "output_type": "stream", |
| 477 | + "text": [ |
| 478 | + "Name: Phill\n", |
| 479 | + "Salary: None\n", |
| 480 | + "Salary: 0.0\n" |
| 481 | + ] |
| 482 | + } |
| 483 | + ], |
| 484 | + "source": [ |
| 485 | + "person = {'name': 'Phill', 'age': 22}\n", |
| 486 | + "\n", |
| 487 | + "print('Name: ', person.get('name'))\n", |
| 488 | + "\n", |
| 489 | + "print('Salary: ', person.get('salary'))\n", |
| 490 | + "\n", |
| 491 | + "print('Salary: ', person.get('salary', 0.0))" |
| 492 | + ] |
| 493 | + }, |
| 494 | + { |
| 495 | + "cell_type": "code", |
| 496 | + "execution_count": 30, |
| 497 | + "metadata": {}, |
| 498 | + "outputs": [ |
| 499 | + { |
| 500 | + "data": { |
| 501 | + "text/plain": [ |
| 502 | + "{'name': 'Phill', 'age': 22}" |
| 503 | + ] |
| 504 | + }, |
| 505 | + "execution_count": 30, |
| 506 | + "metadata": {}, |
| 507 | + "output_type": "execute_result" |
| 508 | + } |
| 509 | + ], |
| 510 | + "source": [ |
| 511 | + "person" |
| 512 | + ] |
| 513 | + }, |
| 514 | + { |
| 515 | + "cell_type": "code", |
| 516 | + "execution_count": 31, |
| 517 | + "metadata": {}, |
| 518 | + "outputs": [ |
| 519 | + { |
| 520 | + "name": "stdout", |
| 521 | + "output_type": "stream", |
| 522 | + "text": [ |
| 523 | + "Salary: None\n" |
| 524 | + ] |
| 525 | + }, |
| 526 | + { |
| 527 | + "ename": "KeyError", |
| 528 | + "evalue": "'salary'", |
| 529 | + "output_type": "error", |
| 530 | + "traceback": [ |
| 531 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 532 | + "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", |
| 533 | + "\u001b[0;32m<ipython-input-31-b00495ba314f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Salary: '\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mperson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'salary'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mperson\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'salary'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", |
| 534 | + "\u001b[0;31mKeyError\u001b[0m: 'salary'" |
| 535 | + ] |
| 536 | + } |
| 537 | + ], |
| 538 | + "source": [ |
| 539 | + "print('Salary: ', person.get('salary'))\n", |
| 540 | + "print(person['salary'])" |
| 541 | + ] |
| 542 | + }, |
377 | 543 | {
|
378 | 544 | "cell_type": "code",
|
379 | 545 | "execution_count": null,
|
|
0 commit comments