San Jose State University Department of Applied Data Science
**DATA 200 Computational Programming for Data Analytics**
Spring 2023 Instructor: Ron Mak
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 6.3.3 Mutable Set Operators and Methods"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### The previous set operators and set methods each created a **new** set. The following operators and methods modify an **existing** set."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Mutable Mathematical Set Operations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### The **union augmented assignment operator** `|=` performs a set union and modifies the left set."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"numbers = {1, 3, 5}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"numbers |= {2, 3, 4}\n",
"\n",
"numbers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### The set method `update()` also performs a union operation that modifies the set that it's called upon."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"numbers.update(range(10))\n",
"\n",
"numbers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Other mutating set operators and their corresponding set methods:\n",
"\n",
"| Operator | Set method |\n",
"| ---: | :-- |\n",
"| intersection augmented assignment `&=` | `intersection_update()` |\n",
"| difference augmented assignment `-=` | `difference_update()` |\n",
"| symmetric difference augmented assignment `^=` | `symmetric_difference_update()` |\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Methods for Adding and Removing Elements"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Set method `add()` inserts its argument into the set if the argument is **not** already in the set. Otherwise, the set is unchanged."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"numbers.add(17)\n",
"numbers.add(3)\n",
"\n",
"numbers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"numbers.add(3)\n",
"\n",
"numbers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Set method `remove()` removes its argument from the set. A `KeyError` occurs if the element is not in the set."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"numbers.remove(3)\n",
"\n",
"numbers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"numbers.remove(99)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Set method `discard()` also removes an element but does not cause an error if the element is not in the set."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"numbers.discard(17)\n",
"\n",
"numbers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"numbers.discard(99)\n",
"\n",
"numbers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Set method `pop()` removes an **arbitrary** element from the set and returns the removed element's value. Since a set is unordered, you won't know which element will be removed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"numbers.pop()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"numbers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Set method `clear()` empties the set it is called upon."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"numbers.clear()\n",
"\n",
"numbers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Set method `pop()` causes a `KeyError` if the set is empty."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"numbers.pop()"
]
},
{
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}