{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2024
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6.2.3 Basic Dictionary Operations" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "roman_numerals = {'I': 1, 'II': 2, 'III': 3, 'V': 5, 'X': 100}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "roman_numerals" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Accessing the Value Associated with a Key" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "roman_numerals['V']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Updating the Value of an Existing Key–Value Pair" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "roman_numerals['X'] = 10" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "roman_numerals" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adding a New Key–Value Pair" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "roman_numerals['L'] = 50" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "roman_numerals" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Removing a Key–Value Pair" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "del roman_numerals['III']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "roman_numerals" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Instead of the `del` statement, you can use a dictionary's `pop()` method. The method also returns the value that it removed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "roman_numerals.pop('X')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "roman_numerals" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Attempting to Access a Nonexistent Key" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "roman_numerals['III']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### To prevent this error, use a dictionary's `get()` method. Pass the method a key, and it either returns the corresponding value, or `None` if there is no such key." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "roman_numerals.get('III')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(roman_numerals.get('III'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(roman_numerals.get('V'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### You can pass method `get()` a second argument, which is returned if the key in the first argument isn't in the dictionary." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "roman_numerals.get('III', 'III not in dictionary')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "roman_numerals.get('III')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Testing Whether a Dictionary Contains a Specified Key" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Use `in` and `not in` to check whether or not a **key** is in a dictionary." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'V' in roman_numerals" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'III' in roman_numerals" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'III' not in roman_numerals" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "##########################################################################\n", "# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n", "# Pearson Education, Inc. All Rights Reserved. #\n", "# #\n", "# DISCLAIMER: The authors and publisher of this book have used their #\n", "# best efforts in preparing the book. These efforts include the #\n", "# development, research, and testing of the theories and programs #\n", "# to determine their effectiveness. The authors and publisher make #\n", "# no warranty of any kind, expressed or implied, with regard to these #\n", "# programs or to the documentation contained in these books. The authors #\n", "# and publisher shall not be liable in any event for incidental or #\n", "# consequential damages in connection with, or arising out of, the #\n", "# furnishing, performance, or use of these programs. #\n", "##########################################################################\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Additional material (C) Copyright 2023 by Ronald Mak" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }