0% found this document useful (0 votes)
2 views

Chapter 04 Other Python Data Structures.ipynb

The document is a Jupyter notebook focused on various Python data structures, specifically dictionaries. It includes code examples for creating, accessing, updating, and deleting dictionary entries, along with explanations of dictionary views. The notebook is designed for educational purposes, likely as part of a data science curriculum.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter 04 Other Python Data Structures.ipynb

The document is a Jupyter notebook focused on various Python data structures, specifically dictionaries. It includes code examples for creating, accessing, updating, and deleting dictionary entries, along with explanations of dictionary views. The notebook is designed for educational purposes, likely as part of a data science curriculum.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 62

{

"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Chapter4_Other_Python_Data_Structures.ipynb",
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"display_name": "Python 3",
"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.9.1"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/kbehrman/foundational-
python-for-data-science/blob/main/Chapter4_Other_Python_Data_Structures.ipynb\"
target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-
badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5Rloc60trkED"
},
"source": [
"# Other Python Data Structures \n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "FxP_kdittVdW"
},
"source": [
"\n",
"## Dictionaries\n",
"https://docs.python.org/3/library/stdtypes.html#mapping-types-dict\n",
"### Create a dictionary based on key/value pairs\n"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "s1-r2ZvPcLBp",
"outputId": "b19e64b8-40f5-4ac0-c0ef-838d366dc6b3"
},
"source": [
"{ 'name': 'Betty', 'height': 62, 'gpa': 3.6 }"
],
"execution_count": 1,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'gpa': 3.6, 'height': 62, 'name': 'Betty'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 1
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ZszwOTCUtZWt"
},
"source": [
"\n",
"### Create an empty dict using curly braces or constructor"
]
},
{
"cell_type": "code",
"metadata": {
"id": "nCtPVUFFuTCS"
},
"source": [
"dictionary = {}\n",
"dictionary = dict()"
],
"execution_count": 2,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "DGv9SCQhtegb"
},
"source": [
"\n",
"###Use curly braces to create a dictionary with initial key/values\n",
"### Access value using key"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "WlQlSR9HwuGw",
"outputId": "fe54971c-4e3f-4306-bd25-340bfea52378"
},
"source": [
"# Creating dictionaries\n",
"student_record_1 = dict(name='Paula', height=64, gpa=3.8)\n",
"student_record_2 = dict([['name','Paula'],['height',64],['gpa',3.8]])\n",
"student_record_3 = dict({'name':'Paula', 'height':64, 'gpa':3.8})\n",
"student_record_4 = {'name':'Paula', 'height':64, 'gpa':3.8}\n",
"student_record_1 == student_record_2 == student_record_3 ==
student_record_4"
],
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 3
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"id": "60ZWrmplZtAW",
"outputId": "06cad9d4-3942-45f9-ca3f-16786de67b92"
},
"source": [
"student_record_1['name']"
],
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'Paula'"
]
},
"metadata": {
"tags": []
},
"execution_count": 4
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "gRQ1UsvHbbp1",
"outputId": "98456f09-27c9-4940-9bce-8f234a61190d"
},
"source": [
"student_record_1['gpa']"
],
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"3.8"
]
},
"metadata": {
"tags": []
},
"execution_count": 5
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "5aSerTCDZpYO",
"outputId": "ae665c05-5899-41b5-a661-f7af995551c4"
},
"source": [
"# Add new key/value\n",
"student_record_1['ranking'] = 1\n",
"student_record_1"
],
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'gpa': 3.8, 'height': 64, 'name': 'Paula', 'ranking': 1}"
]
},
"metadata": {
"tags": []
},
"execution_count": 6
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "youAR8CBcIhR",
"outputId": "d1a1b262-2473-4ff1-d315-951272451b5f"
},
"source": [
"# Update existing key/value\n",
"student_record_1['gpa'] = 4.0\n",
"student_record_1['gpa']"
],
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"4.0"
]
},
"metadata": {
"tags": []
},
"execution_count": 7
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "WqgMGu6VuBLP"
},
"source": [
"\n",
"### Add a key/value pair to an existing dictionary\n"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vQY9LEHg-Wy2",
"outputId": "5dd8ca9a-913c-4ae2-f3b2-ba3c06833205"
},
"source": [
"student_record_1['applied'] = '2019-10-31'\n",
"student_record_1"
],
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'applied': '2019-10-31',\n",
" 'gpa': 4.0,\n",
" 'height': 64,\n",
" 'name': 'Paula',\n",
" 'ranking': 1}"
]
},
"metadata": {
"tags": []
},
"execution_count": 8
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3s7w6aYA_UUx"
},
"source": [
"### Update value for existing key\n"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 181
},
"id": "wZo_4Ksf_aLb",
"outputId": "b5aa81a6-1112-4ae9-a792-d631889d6c28"
},
"source": [
"student_record_1['gpa'] = 3.0\n",
"student_record_1['gpa']"
],
"execution_count": 9,
"outputs": [
{
"output_type": "error",
"ename": "NameError",
"evalue": "ignored",
"traceback": [
"\
u001b[0;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[0;31mNameError\u001b[0m
Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-9-205bb2cfd986>\u001b[0m in \
u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \
u001b[0mstudent_record\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'gpa'\u001b[0m\
u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m3.0\u001b[0m\u001b[0;34m\
u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \
u001b[0mstudent_record\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'gpa'\u001b[0m\
u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'student_record' is not defined"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "iN5ud-HKHktY"
},
"source": [
"student_record_1['gpa'] += 1.0\n",
"student_record_1['gpa']"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "erZPwp_1JDUL"
},
"source": [
"\n",
"### Remove item\n"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "oS9O-aGcKlsg",
"outputId": "779479e6-2210-4f07-c5d8-b5c26d48bbd6"
},
"source": [
"student_record_1['id'] = None\n",
"student_record_1"
],
"execution_count": 13,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'applied': '2019-10-31',\n",
" 'gpa': 4.0,\n",
" 'height': 64,\n",
" 'id': None,\n",
" 'name': 'Paula',\n",
" 'ranking': 1}"
]
},
"metadata": {
"tags": []
},
"execution_count": 13
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ldIQllFcLrBJ",
"outputId": "dc68935b-45c5-442e-b6a0-f060f7f76704"
},
"source": [
"del(student_record_1['id'])\n",
"student_record_1"
],
"execution_count": 14,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'applied': '2019-10-31',\n",
" 'gpa': 4.0,\n",
" 'height': 64,\n",
" 'name': 'Paula',\n",
" 'ranking': 1}"
]
},
"metadata": {
"tags": []
},
"execution_count": 14
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "-8WYLwdve6MX"
},
"source": [
"### Dictionary views\n",
"- https://docs.python.org/3/library/stdtypes.html#dict-views\n",
"- py3.7: keys,values in order inserted\n",
"- py3.8: dict views reversable\n",
"- Dynamic"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "I3AqQtqxdir8",
"outputId": "62b6cbd3-41b5-4ab4-be2e-35ae828564d4"
},
"source": [
"keys = student_record_1.keys()\n",
"keys"
],
"execution_count": 15,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"dict_keys(['name', 'height', 'gpa', 'ranking', 'applied'])"
]
},
"metadata": {
"tags": []
},
"execution_count": 15
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "s55eFyThd9OB",
"outputId": "415193fb-4c5a-4ab5-8469-0827e2efe1c1"
},
"source": [
"values = student_record_1.values()\n",
"values"
],
"execution_count": 16,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"dict_values(['Paula', 64, 4.0, 1, '2019-10-31'])"
]
},
"metadata": {
"tags": []
},
"execution_count": 16
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "hJ9qTBoDeANY",
"outputId": "23a31337-2e86-4174-9bfd-7a2b09f10790"
},
"source": [
"items = student_record_1.items()\n",
"items"
],
"execution_count": 17,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"dict_items([('name', 'Paula'), ('height', 64), ('gpa', 4.0),
('ranking', 1), ('applied', '2019-10-31')])"
]
},
"metadata": {
"tags": []
},
"execution_count": 17
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "LlvtF4ZefuYA",
"outputId": "a5693513-dccd-4689-d20e-19c663b8bb60"
},
"source": [
"'ranking' in keys"
],
"execution_count": 18,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 18
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "AXzlowLXf2Cf",
"outputId": "5e1b890f-7cde-4850-857b-2dcce0ba2f2a"
},
"source": [
"1 in values"
],
"execution_count": 19,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 19
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vA4WDX22f7Qu",
"outputId": "4453262c-4e69-49b8-bfe2-3ffea2663060"
},
"source": [
"('ranking',1) in items"
],
"execution_count": 20,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 20
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Mt2Pj5azeX5p",
"outputId": "cc841af4-ed3e-4784-a684-1f1794cb8d5d"
},
"source": [
"del student_record_1['ranking']\n",
"student_record_1"
],
"execution_count": 21,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'applied': '2019-10-31', 'gpa': 4.0, 'height': 64, 'name':
'Paula'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 21
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "cX9wAONYeKmk",
"outputId": "57a8aed6-c9fb-48ba-edf0-9af33b87d756"
},
"source": [
"keys"
],
"execution_count": 22,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"dict_keys(['name', 'height', 'gpa', 'applied'])"
]
},
"metadata": {
"tags": []
},
"execution_count": 22
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PTneg-T3gPkb",
"outputId": "d8a592b2-f291-44b2-9647-d5b314a82bbd"
},
"source": [
"'ranking' in keys"
],
"execution_count": 23,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {
"tags": []
},
"execution_count": 23
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TGJM4QV1gPkg",
"outputId": "170f0785-0d6f-4171-8ef7-e037f4a14f3b"
},
"source": [
"1 in values"
],
"execution_count": 24,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {
"tags": []
},
"execution_count": 24
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "xgFZ571EgPki",
"outputId": "c33dccc3-3625-4a27-89c7-f1ce6f2bfc70"
},
"source": [
"('ranking',1) in items"
],
"execution_count": 25,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {
"tags": []
},
"execution_count": 25
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "FwcGkfgSfoYc",
"outputId": "1b1bc642-0091-48b6-de75-a1730695aa0b"
},
"source": [
"len(keys)"
],
"execution_count": 26,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"4"
]
},
"metadata": {
"tags": []
},
"execution_count": 26
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "FSzyt8cHgiVl",
"outputId": "d98b41e4-7169-4a87-b730-1ad145b5618d"
},
"source": [
"len(values)"
],
"execution_count": 27,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"4"
]
},
"metadata": {
"tags": []
},
"execution_count": 27
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ccp-1L2WgkHA",
"outputId": "82991db4-15f6-4a25-9982-dbdf6eecb9f2"
},
"source": [
"len(items)"
],
"execution_count": 28,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"4"
]
},
"metadata": {
"tags": []
},
"execution_count": 28
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "FW9JFokNglno",
"outputId": "551aadad-1d84-4d05-a216-cf2489dee168"
},
"source": [
"keys\n"
],
"execution_count": 29,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"dict_keys(['name', 'height', 'gpa', 'applied'])"
]
},
"metadata": {
"tags": []
},
"execution_count": 29
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 164
},
"id": "L3OFRtxAecyQ",
"outputId": "74ab81ff-2c94-4789-9242-d598a5e3d322"
},
"source": [
"list(reversed(keys))"
],
"execution_count": 30,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "ignored",
"traceback": [
"\
u001b[0;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m
Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-30-42d315282bae>\u001b[0m in \
u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \
u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mreversed\u001b[0m\u001b[0;34m(\
u001b[0m\u001b[0mkeys\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",
"\u001b[0;31mTypeError\u001b[0m: 'dict_keys' object is not reversible"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "IZPGSjSY4Kgj"
},
"source": [
"# Keys are set-like objects\n",
"student_record_1 = {'first':'Julia', \n",
" 'last':'Brown', \n",
" 'id': 'ax012E4', \n",
" 'admitted':'2020-03-14'}\n",
"\n",
"student_record = {'first':'Julia', \n",
" 'last':'Brown', \n",
" 'id': 'ax012E4', \n",
" 'gpa':3.8,\n",
" 'major':'Data Science',\n",
" 'minor': 'Math',\n",
" 'advisor':'Pickerson'}"
],
"execution_count": 31,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "4pmFI-2A8Oz2",
"outputId": "c45b2f9f-1c7c-4442-fbc4-c277aeda499d"
},
"source": [
"student_record_1.keys() == student_record.keys()"
],
"execution_count": 32,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {
"tags": []
},
"execution_count": 32
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Hg87igNQ8UEZ",
"outputId": "accd2b0c-cacc-4936-cc5d-9727c330ceb1"
},
"source": [
"student_record_1.keys() ^ student_record.keys()"
],
"execution_count": 33,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'admitted', 'advisor', 'gpa', 'major', 'minor'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 33
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "9yuqinrT8c6k",
"outputId": "a1e66d73-eb3a-48a1-820b-4cd58a548167"
},
"source": [
"student_record_1.keys() & student_record.keys()"
],
"execution_count": 34,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'first', 'id', 'last'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 34
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ps_o_7zB806F",
"outputId": "f276a27d-def9-4986-cd90-a8eacbd00bed"
},
"source": [
"student_record_1.keys() - student_record.keys()"
],
"execution_count": 35,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'admitted'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 35
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "8bf3lrPO87cO",
"outputId": "bfa4b926-398c-4953-9c06-56799bb44820"
},
"source": [
"student_record_1.keys() | student_record.keys()"
],
"execution_count": 36,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'admitted', 'advisor', 'first', 'gpa', 'id', 'last', 'major',
'minor'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 36
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ZsVwDtCYKjqm"
},
"source": [
"\n",
"### Use items in for loop"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ScSn4-JsalSd",
"outputId": "c8893df1-932e-4fa3-cca3-11637ab66b82"
},
"source": [
"for k,v in student_record.items():\n",
" print(f\"{k} => {v}\")"
],
"execution_count": 37,
"outputs": [
{
"output_type": "stream",
"text": [
"first => Julia\n",
"last => Brown\n",
"id => ax012E4\n",
"gpa => 3.8\n",
"major => Data Science\n",
"minor => Math\n",
"advisor => Pickerson\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "VECwtCu0_Yp4"
},
"source": [
"\n",
"### Check if the dictionary has key\n"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "6fjG1a7vImRb",
"outputId": "78802995-ef00-446f-9b9d-d0ccaa5f4035"
},
"source": [
"'last' in student_record.keys()"
],
"execution_count": 38,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 38
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "rO9ebzIjIQv5",
"outputId": "10594d95-2913-4872-9cb5-272ef7586d7e"
},
"source": [
"'last' in student_record"
],
"execution_count": 39,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 39
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Rw4nRq5wIOxJ"
},
"source": [
"\n",
"### Get method\n"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 164
},
"id": "oHxnYO9jJed5",
"outputId": "660b7c24-ac91-4a91-8205-1850136dc91e"
},
"source": [
"student_record_1['name']"
],
"execution_count": 40,
"outputs": [
{
"output_type": "error",
"ename": "KeyError",
"evalue": "ignored",
"traceback": [
"\
u001b[0;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m
Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-40-3c61a6d43cc5>\u001b[0m in \
u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \
u001b[0mstudent_record_1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'name'\u001b[0m\
u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\
u001b[0m",
"\u001b[0;31mKeyError\u001b[0m: 'name'"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "rGTn61d5JECD",
"outputId": "9de11dcc-55b5-46ee-c2d0-4914b17f3812"
},
"source": [
"'name' in student_record"
],
"execution_count": 41,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {
"tags": []
},
"execution_count": 41
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "TKmO2SD4Jiob"
},
"source": [
"student_record.get('name')"
],
"execution_count": 42,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Oqw8Wr41Jrza",
"outputId": "d4a1a101-6ff7-4031-80c4-0cc740a4b209"
},
"source": [
"print( student_record.get('name') )"
],
"execution_count": 43,
"outputs": [
{
"output_type": "stream",
"text": [
"None\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"id": "qlW9vOpgJ1Zr",
"outputId": "f5dd38f9-b3d2-4fc6-e90f-c8262a110586"
},
"source": [
"student_record.get('name', 'no-name')"
],
"execution_count": 44,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'no-name'"
]
},
"metadata": {
"tags": []
},
"execution_count": 44
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"id": "XITnqHGNKKe9",
"outputId": "6d0f4e5b-2b91-4c8b-d4c0-a93a2ab82ece"
},
"source": [
"student_record.get('name', student_record_1.get('first', 'no-name'))"
],
"execution_count": 45,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'Julia'"
]
},
"metadata": {
"tags": []
},
"execution_count": 45
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "WBvcX50lKhPX"
},
"source": [
"\n",
"### What types can be used as keys\n",
"Immutable objects have a constant value that can't be changed. Among the
mutable types are strings, numbers and tuples. Objects such as lists are mutable,
they can be changed."
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "HPUe474QSEiU",
"outputId": "e4d7ed59-7c4d-478b-85af-759baa63db4e"
},
"source": [
"{ 1 : 'an integer',\n",
" 'string' : 'a string',\n",
" ('item',) : 'a tuple',\n",
" range(12) : 'a range',\n",
" b'binary' : 'a binary string'}"
],
"execution_count": 46,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{('item',): 'a tuple',\n",
" 1: 'an integer',\n",
" b'binary': 'a binary string',\n",
" range(0, 12): 'a range',\n",
" 'string': 'a string'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 46
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 164
},
"id": "KdmdbfHXTqMD",
"outputId": "28877e46-b8a9-44f3-c332-05d08ab4f592"
},
"source": [
"{ ['a', 'list'] : 'a list key' }"
],
"execution_count": 47,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "ignored",
"traceback": [
"\
u001b[0;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m
Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-47-1b0e555de2b5>\u001b[0m in \
u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \
u001b[0;34m{\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'a'\u001b[0m\u001b[0;34m,\
u001b[0m \u001b[0;34m'list'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m:\u001b[0m \
u001b[0;34m'a list key'\u001b[0m \u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\
u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "aucTMZBySXFx",
"outputId": "93cd5e0a-2194-4eb4-8c55-02e259ce108b"
},
"source": [
"# Valid tuple key\n",
"tuple_key = (1, 'one', 1.0, ('uno',))\n",
"{ tuple_key: 'some value' }"
],
"execution_count": 48,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{(1, 'one', 1.0, ('uno',)): 'some value'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 48
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 181
},
"id": "umuOV4igSsxq",
"outputId": "3accb42c-48f1-4fb2-f6af-a764003fd2ae"
},
"source": [
"bad_tuple = ([1, 2], 3)\n",
"{ bad_tuple: 'some value' }"
],
"execution_count": 49,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "ignored",
"traceback": [
"\
u001b[0;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m
Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-49-b2cddfdda91e>\u001b[0m in \
u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \
u001b[0mbad_tuple\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\
u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\
u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\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[0;34m{\u001b[0m \u001b[0mbad_tuple\u001b[0m\
u001b[0;34m:\u001b[0m \u001b[0;34m'some value'\u001b[0m \u001b[0;34m}\u001b[0m\
u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "pnAlaxsBS5-T",
"outputId": "169ecf9d-b7e1-4f7e-de33-e5f03f7a05bc"
},
"source": [
"a_string = 'a string'\n",
"a_string.__hash__()"
],
"execution_count": 50,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"-96690038947615399"
]
},
"metadata": {
"tags": []
},
"execution_count": 50
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2Zcze3nnTDP8",
"outputId": "1bedd5c2-b432-4acc-a1b3-0f63465d298c"
},
"source": [
"a_tuple = 'a','b',\n",
"a_tuple.__hash__()"
],
"execution_count": 51,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"-1060002088815028020"
]
},
"metadata": {
"tags": []
},
"execution_count": 51
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "3SfvxmdtUNsg",
"outputId": "534a652a-91a4-454d-a695-777cd4494475"
},
"source": [
"a_number = 13\n",
"a_number.__hash__()"
],
"execution_count": 52,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"13"
]
},
"metadata": {
"tags": []
},
"execution_count": 52
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 181
},
"id": "s053eRULUaDT",
"outputId": "3b27ae72-78a0-4d99-c9e5-81640ce08c3c"
},
"source": [
"a_list = ['a','b']\n",
"a_list.__hash__()"
],
"execution_count": 53,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "ignored",
"traceback": [
"\
u001b[0;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m
Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-53-c4f99d4ea902>\u001b[0m in \
u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \
u001b[0ma_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\
u001b[0;34m'a'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'b'\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[0ma_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__hash__\
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",
"\u001b[0;31mTypeError\u001b[0m: 'NoneType' object is not callable"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "z6u3-5Uwt3_4"
},
"source": [
"\n",
"## Sets\n",
"- members must be hashable\n"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "epPCLrckt4Zy",
"outputId": "8be69206-eb14-4edb-d741-da7a954d6b90"
},
"source": [
"letters = 'a', 'a', 'a', 'b', 'c'\n",
"unique_letters = set(letters)\n",
"unique_letters"
],
"execution_count": 54,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'a', 'b', 'c'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 54
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qnQHMNCRuebg"
},
"source": [
"##### Create set from a string"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "OFHdUibFHIZJ",
"outputId": "75666339-6699-472d-f61f-c545ad2a054d"
},
"source": [
"empty_set = set()\n",
"empty_set"
],
"execution_count": 55,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"set()"
]
},
"metadata": {
"tags": []
},
"execution_count": 55
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "IRsuJBVC_ORB",
"outputId": "e4a12c89-3d84-45c7-abf0-c33f65db7e7a"
},
"source": [
"unique_chars = set('mississippi')\n",
"unique_chars"
],
"execution_count": 56,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'i', 'm', 'p', 's'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 56
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "LUJyjEh1uzrv"
},
"source": [
"##### Create set using curley braces"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "afYk3yfTu3Pt",
"outputId": "0550fb7d-0b2a-4ebd-ce9e-cce29a513a1e"
},
"source": [
"unique_num = {1, 1, 2, 3, 4, 5, 5}\n",
"unique_num"
],
"execution_count": 57,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{1, 2, 3, 4, 5}"
]
},
"metadata": {
"tags": []
},
"execution_count": 57
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 164
},
"id": "DFzdIDV1HLhr",
"outputId": "a5d268ad-e9b1-4101-d1d6-dcad1c852436"
},
"source": [
"bad_set = { ['a','b'], 'c' }"
],
"execution_count": 58,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "ignored",
"traceback": [
"\
u001b[0;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m
Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-58-1179bc4af8b8>\u001b[0m in \
u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \
u001b[0mbad_set\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m \
u001b[0;34m[\u001b[0m\u001b[0;34m'a'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'b'\
u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'c'\u001b[0m \
u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\
u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "jDMYvC0avu_C"
},
"source": [
"##### Adding to a set"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "hoZ7hcrBvwtc",
"outputId": "770d2421-f951-4e62-c16e-0455281d6582"
},
"source": [
"unique_num.add(6)\n",
"unique_num"
],
"execution_count": 59,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{1, 2, 3, 4, 5, 6}"
]
},
"metadata": {
"tags": []
},
"execution_count": 59
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "3aFe5DyZM55r"
},
"source": [
""
],
"execution_count": 59,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "TQyoEjgivjA3"
},
"source": [
"##### Checking membership"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "3zOvkWX6vlf2",
"outputId": "ba50ed6b-63a3-498b-9fc0-6f87993f4752"
},
"source": [
"3 in unique_num"
],
"execution_count": 60,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 60
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "gr8qEbCLJZbL",
"outputId": "7a34ecb0-b158-4693-c9d3-57cd80df7f0f"
},
"source": [
"3 not in unique_num"
],
"execution_count": 61,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {
"tags": []
},
"execution_count": 61
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "8RlLww07Jcgs",
"outputId": "ac5bce8d-9846-4817-98b4-588bfc0742e7"
},
"source": [
"len(unique_num)"
],
"execution_count": 62,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"6"
]
},
"metadata": {
"tags": []
},
"execution_count": 62
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "yekvVNcfEHPR"
},
"source": [
"\n",
"\n",
"### Popping from a set\n"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "zuNvYAwOGuAm",
"outputId": "4425b382-7bca-40a1-aa69-4bc6863c6b54"
},
"source": [
"unique_num.pop()"
],
"execution_count": 63,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"1"
]
},
"metadata": {
"tags": []
},
"execution_count": 63
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "bWsjIJW6G9Wz",
"outputId": "2a188338-170d-4321-963c-56a7d18dec4b"
},
"source": [
"unique_num"
],
"execution_count": 64,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{2, 3, 4, 5, 6}"
]
},
"metadata": {
"tags": []
},
"execution_count": 64
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "NA5oHMBAQgJT"
},
"source": [
"### remove"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "7rQgoQA9IQOv",
"outputId": "e90e4a1d-e794-4a9c-a702-91d672a9f95d"
},
"source": [
"students = {'Karl', 'Max', 'Tik'}\n",
"students.remove('Karl')\n",
"students"
],
"execution_count": 65,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'Max', 'Tik'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 65
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 164
},
"id": "Oo68ZOFzaqym",
"outputId": "b783d0bc-3c1e-4e48-cfbb-df2090b47be5"
},
"source": [
"students.remove('Barb')"
],
"execution_count": 66,
"outputs": [
{
"output_type": "error",
"ename": "KeyError",
"evalue": "ignored",
"traceback": [
"\
u001b[0;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m
Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-66-a36a5744ac05>\u001b[0m in \
u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \
u001b[0mstudents\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\
u001b[0;34m(\u001b[0m\u001b[0;34m'Barb'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\
u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m: 'Barb'"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "66yANHtnQnwt"
},
"source": [
"### discard"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "GyuPCZovQp_P",
"outputId": "f7126546-2583-4774-fbe7-96d3c81446ce"
},
"source": [
"students.discard('Barb')\n",
"students.discard('Tik')\n",
"students"
],
"execution_count": 72,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"set()"
]
},
"metadata": {
"tags": []
},
"execution_count": 72
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tC5giVuPQu6r"
},
"source": [
"### Clear"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "wkk2BLUma4PH",
"outputId": "84a8b343-ff1f-4e81-f445-bf0d285ef093"
},
"source": [
"students.clear()\n",
"students"
],
"execution_count": 68,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"set()"
]
},
"metadata": {
"tags": []
},
"execution_count": 68
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "LjPQ2BQJGs0o"
},
"source": [
"\n",
"### Indexing"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 164
},
"id": "1qo-lTuoJOf2",
"outputId": "8b7e53f8-1f73-4c10-baea-bf3eb0bfad60"
},
"source": [
"unique_num[3]"
],
"execution_count": 69,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "ignored",
"traceback": [
"\
u001b[0;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m
Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-69-fecab0cd5f95>\u001b[0m in \
u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \
u001b[0munique_num\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\
u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\
u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: 'set' object is not subscriptable"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "-UUDL8yUeFNz"
},
"source": [
"### Equality"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "M9HiKi4LeHJi",
"outputId": "53db6f73-e02b-4ff6-c9b1-44751a588ec4"
},
"source": [
"first = {'a','b','c','d'}\n",
"second = {'d','c','b','a'}\n",
"first == second"
],
"execution_count": 73,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 73
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "uvUub31_eVJO",
"outputId": "63f8226a-e0b2-40e6-bb70-3fd626915d4e"
},
"source": [
"first != second"
],
"execution_count": 74,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {
"tags": []
},
"execution_count": 74
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "HoBP9DsIeYF8"
},
"source": [
""
],
"execution_count": 74,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "_TSJTZ7MtlVr"
},
"source": [
"\n",
"### Set operations\n",
"isdisjoint(other)\n",
"\n"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "tdPyLI3mb2_W",
"outputId": "89a74228-439e-4f44-e93a-43547e46a7f3"
},
"source": [
"even = set(range(0,10,2))\n",
"even"
],
"execution_count": 75,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{0, 2, 4, 6, 8}"
]
},
"metadata": {
"tags": []
},
"execution_count": 75
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "nvHoj4uNcPO2",
"outputId": "8741a3ef-e438-47b5-e86b-b6547605f105"
},
"source": [
"odd = set(range(1,11,2))\n",
"odd"
],
"execution_count": 76,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{1, 3, 5, 7, 9}"
]
},
"metadata": {
"tags": []
},
"execution_count": 76
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "j2Is8ANAcTd6",
"outputId": "58e7c834-1e81-4181-f0c5-2855772f75ca"
},
"source": [
"even.isdisjoint(odd)"
],
"execution_count": 77,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 77
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "npx6mU4lbzVS"
},
"source": [
"\n",
"## subset"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "QlV7N1gvcmtu",
"outputId": "084cf226-1d9f-4a8a-fab0-08697557f545"
},
"source": [
"nums = set(range(21))\n",
"nums"
],
"execution_count": 78,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20}"
]
},
"metadata": {
"tags": []
},
"execution_count": 78
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "wkj4pQXEcvyh",
"outputId": "36cdb160-8f56-417f-949b-f2a3a78e5f03"
},
"source": [
"threes = set(range(3,21,3))\n",
"threes"
],
"execution_count": 79,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{3, 6, 9, 12, 15, 18}"
]
},
"metadata": {
"tags": []
},
"execution_count": 79
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "jkF0K5vac0mT",
"outputId": "2efc3d35-395a-407d-e57d-fc6cc1de0b32"
},
"source": [
"threes.issubset(nums)"
],
"execution_count": 80,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 80
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "6OVWnQ8KdTlK",
"outputId": "c784cc20-950e-47ad-8e9c-ad5ebfe7f110"
},
"source": [
"threes.issubset(range(21))"
],
"execution_count": 81,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 81
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "stUoDuCXdXa6",
"outputId": "030ff809-b2ee-4e9c-be3a-6636726a1b74"
},
"source": [
"threes <= nums"
],
"execution_count": 82,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 82
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 164
},
"id": "OBfXXOMfdbec",
"outputId": "f214009e-a24e-40a8-dc09-2ddbe5b5ee52"
},
"source": [
"threes <= range(21)"
],
"execution_count": 83,
"outputs": [
{
"output_type": "error",
"ename": "TypeError",
"evalue": "ignored",
"traceback": [
"\
u001b[0;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m
Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-83-dbd51effe302>\u001b[0m in \
u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \
u001b[0mthrees\u001b[0m \u001b[0;34m<=\u001b[0m \u001b[0mrange\u001b[0m\
u001b[0;34m(\u001b[0m\u001b[0;36m21\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\
u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: '<=' not supported between instances
of 'set' and 'range'"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "YwGHpc_ccg9L"
},
"source": [
"\n",
"### proper subset "
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "iGRVN1Kyegcn",
"outputId": "6b119e0b-5094-4225-db85-79d8fe23a2b2"
},
"source": [
"threes <= nums"
],
"execution_count": 84,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 84
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "_AlK2sYeemp7",
"outputId": "a84011ec-2e04-4aef-bc40-bde489ae91b3"
},
"source": [
"threes <= {'3','6','9','12','15','18'}"
],
"execution_count": 85,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {
"tags": []
},
"execution_count": 85
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1O9tsxHQeeqf"
},
"source": [
"### Superset and proper superset"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "FEgxiNOFfufF",
"outputId": "eae6d04b-9aa0-4e1a-d208-6da98f96bbdb"
},
"source": [
"nums.issuperset(threes)"
],
"execution_count": 86,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 86
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "M0PnHT7ifyxY",
"outputId": "7d72474b-c809-4852-d1d1-3a23621e3637"
},
"source": [
"nums.issuperset([1,2,3,4])"
],
"execution_count": 87,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 87
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "3BLzq2dHewvw",
"outputId": "a7b14f83-f121-4868-84e2-98e7a422ce8a"
},
"source": [
"nums >= threes"
],
"execution_count": 88,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 88
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "D2XNB-lqforF",
"outputId": "66194ae9-738a-402f-88a6-1c7671a67f4d"
},
"source": [
"nums > threes"
],
"execution_count": 89,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 89
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PD5p10XqgEqP",
"outputId": "ebcfaf09-b745-466c-fc68-ce523ee02571"
},
"source": [
"nums >= nums"
],
"execution_count": 90,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 90
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "tj4JkcEpgGlP",
"outputId": "50917a72-aff5-4f82-89a3-85cfa17cad25"
},
"source": [
"nums > nums"
],
"execution_count": 91,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {
"tags": []
},
"execution_count": 91
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IAKmEGf6fWIO"
},
"source": [
"### Union"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "NMSq_Evcmsog",
"outputId": "ddb4a2e9-0614-4063-9257-616a409b8478"
},
"source": [
"odds = set(range(0,12,2))\n",
"odds"
],
"execution_count": 92,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{0, 2, 4, 6, 8, 10}"
]
},
"metadata": {
"tags": []
},
"execution_count": 92
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "gbOK6hOmm4Ch",
"outputId": "ce214e9b-3788-4a70-c33b-4afff1ad25b6"
},
"source": [
"evens = set(range(1,13,2))\n",
"evens"
],
"execution_count": 93,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{1, 3, 5, 7, 9, 11}"
]
},
"metadata": {
"tags": []
},
"execution_count": 93
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "slWpydRVm6Dv",
"outputId": "1730601a-5550-4601-d2a3-443392cd2549"
},
"source": [
"odds.union(evens)"
],
"execution_count": 94,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}"
]
},
"metadata": {
"tags": []
},
"execution_count": 94
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "hhhEyvGAnJ6D",
"outputId": "a112675e-dcbc-4117-974e-c9ca9c94924b"
},
"source": [
"odds.union(range(0,12))"
],
"execution_count": 95,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}"
]
},
"metadata": {
"tags": []
},
"execution_count": 95
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "6N821zpQnQNr",
"outputId": "60bd7357-64b3-4328-d8bd-9b1996351b9c"
},
"source": [
"odds | evens"
],
"execution_count": 96,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}"
]
},
"metadata": {
"tags": []
},
"execution_count": 96
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "T9Xg-kWjfX2W"
},
"source": [
"### Intersection"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "KYJH3b7Wngh9",
"outputId": "339bb628-37a3-455e-b068-b10fcbbb8512"
},
"source": [
"under_ten = set(range(10))\n",
"odds = set(range(1,21,2))\n",
"under_ten.intersection(odds)"
],
"execution_count": 97,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{1, 3, 5, 7, 9}"
]
},
"metadata": {
"tags": []
},
"execution_count": 97
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "NpWqFYfSnrf5",
"outputId": "5b1db37f-5a28-40e5-e235-28d4a282e2f0"
},
"source": [
"under_ten & odds"
],
"execution_count": 98,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{1, 3, 5, 7, 9}"
]
},
"metadata": {
"tags": []
},
"execution_count": 98
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Rsxr6LFlfZl2"
},
"source": [
"### Difference"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "fOOxchWJn4uN",
"outputId": "f0fc1aac-e6ab-4ac9-832e-9e69472b139f"
},
"source": [
"odds.difference(under_ten)"
],
"execution_count": 99,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{11, 13, 15, 17, 19}"
]
},
"metadata": {
"tags": []
},
"execution_count": 99
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "uwmanerNn85v",
"outputId": "ee1c7ffd-fdef-4e87-e9ab-dfcf389cad06"
},
"source": [
"odds - under_ten"
],
"execution_count": 100,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{11, 13, 15, 17, 19}"
]
},
"metadata": {
"tags": []
},
"execution_count": 100
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_CLu7eMlfbDm"
},
"source": [
"### Symmetric_difference"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Rt0cv2D6oMU9",
"outputId": "1fcbeb80-3454-45bd-da16-0525a3b418b1"
},
"source": [
"under_ten = set(range(10))\n",
"over_five = set(range(5, 15))\n",
"under_ten.symmetric_difference(over_five)"
],
"execution_count": 101,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{0, 1, 2, 3, 4, 10, 11, 12, 13, 14}"
]
},
"metadata": {
"tags": []
},
"execution_count": 101
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "celSluyiOElg",
"outputId": "e8378bfd-7726-421c-ba11-72099cd15d43"
},
"source": [
"under_ten ^ over_five"
],
"execution_count": 102,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{0, 1, 2, 3, 4, 10, 11, 12, 13, 14}"
]
},
"metadata": {
"tags": []
},
"execution_count": 102
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IZVuo7fQNQrr"
},
"source": [
"### Updating sets"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "CbNlsh5JJk1X",
"outputId": "848820d3-19c7-45a4-c099-8b03d50619c0"
},
"source": [
"unique_num = {0, 1, 2}\n",
"unique_num.update( {3, 4, 5, 7} )\n",
"unique_num"
],
"execution_count": 103,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{0, 1, 2, 3, 4, 5, 7}"
]
},
"metadata": {
"tags": []
},
"execution_count": 103
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "kvTmomIDJxu_",
"outputId": "023985da-151e-4897-cef5-66d4a1f4eac2"
},
"source": [
"unique_num.update( [8, 9, 10] )\n",
"unique_num"
],
"execution_count": 104,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{0, 1, 2, 3, 4, 5, 7, 8, 9, 10}"
]
},
"metadata": {
"tags": []
},
"execution_count": 104
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "hfyFYb_AJ29S",
"outputId": "4b33e27e-99e0-4fbe-98af-f40b70b4c4a2"
},
"source": [
"unique_num.difference_update( range(0,12,2) )\n",
"unique_num"
],
"execution_count": 105,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{1, 3, 5, 7, 9}"
]
},
"metadata": {
"tags": []
},
"execution_count": 105
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "YA-vNAdDLglv",
"outputId": "3c47a19e-a1f6-4cdd-9562-5cec25802402"
},
"source": [
"unique_num.intersection_update( { 2, 3, 4, 5 } )\n",
"unique_num"
],
"execution_count": 106,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{3, 5}"
]
},
"metadata": {
"tags": []
},
"execution_count": 106
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "sj3GJJWzLyzg",
"outputId": "dd6530d1-b1db-4296-e08a-37cf16759759"
},
"source": [
"unique_num.symmetric_difference_update( {5, 6, 7 } )\n",
"unique_num"
],
"execution_count": 107,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{3, 6, 7}"
]
},
"metadata": {
"tags": []
},
"execution_count": 107
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "P2dNHmdyrSo2",
"outputId": "fdf8f1e0-61ec-4cd1-a237-bee8bdd64a0e"
},
"source": [
"unique_letters = set(\"mississippi\")\n",
"unique_letters"
],
"execution_count": 108,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'i', 'm', 'p', 's'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 108
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ieiZkgW_O5fM",
"outputId": "d7f1ea3c-864a-44db-cd2f-9226fcee1f28"
},
"source": [
"unique_letters |= set(\"Arkansas\")\n",
"unique_letters"
],
"execution_count": 109,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'A', 'a', 'i', 'k', 'm', 'n', 'p', 'r', 's'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 109
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "pVWRBgYKPGOK",
"outputId": "af6e0a02-f94d-4f17-84f3-12bcae4cf560"
},
"source": [
"unique_letters -= set('Arkansas')\n",
"unique_letters"
],
"execution_count": 110,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'i', 'm', 'p'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 110
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "cbW1cxG1PfyU",
"outputId": "a2737b02-1a40-47a0-b7ef-ec953a781f02"
},
"source": [
"unique_letters &= set('permanent')\n",
"unique_letters"
],
"execution_count": 111,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'m', 'p'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 111
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ZHNMrzgqPo90",
"outputId": "ea59d67f-94d5-4b4e-ef3a-f495b7b2de3d"
},
"source": [
"unique_letters ^= set('mud')\n",
"unique_letters"
],
"execution_count": 112,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'d', 'p', 'u'}"
]
},
"metadata": {
"tags": []
},
"execution_count": 112
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "VlQEli4sboWq"
},
"source": [
"## Frozenset"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "VkdAI4hDpMXv",
"outputId": "e640ca64-e861-41f8-d88a-9002cdf5f1ee"
},
"source": [
"froze = frozenset(range(10))\n",
"froze"
],
"execution_count": 113,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})"
]
},
"metadata": {
"tags": []
},
"execution_count": 113
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "0tl3Yr4DpYLp",
"outputId": "dd98cf78-8430-45de-9d46-a6ca21537c30"
},
"source": [
"froze < set(range(21))"
],
"execution_count": 114,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 114
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vF_2LMKyp7cX",
"outputId": "3abf921d-49cc-47db-cecb-a156f6874e34"
},
"source": [
"froze & set(range(5, 15))"
],
"execution_count": 115,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"frozenset({5, 6, 7, 8, 9})"
]
},
"metadata": {
"tags": []
},
"execution_count": 115
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TudqMQdyqDK_",
"outputId": "5777ec23-4cf9-4002-ed84-37c1869310c0"
},
"source": [
"froze ^ set(range(5, 15))"
],
"execution_count": 116,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"frozenset({0, 1, 2, 3, 4, 10, 11, 12, 13, 14})"
]
},
"metadata": {
"tags": []
},
"execution_count": 116
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "dD-yPNLEqLQT",
"outputId": "bdf5216e-2b49-42cd-f2d7-627f98259522"
},
"source": [
"froze | set(range(5,15))"
],
"execution_count": 117,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14})"
]
},
"metadata": {
"tags": []
},
"execution_count": 117
}
]
}
]
}

You might also like