{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 7. Set\n", "\n", "**Sets** are unordered collections of unique elements in Python. They are similar to lists but with the key difference that they do not allow duplicate elements. Sets are often used for membership testing, removing duplicates from a list, and performing set operations like union, intersection, and difference.\n", "\n", "We'll learn about the following topics:\n", "\n", " - [7.1. Creating Sets](#Creating_Sets)\n", " - [7.2. Set Properties](#Set_Properties)\n", " - [7.3. Set Operators](#Set_Operators)\n", " - [7.4. Built-in Set Methods](#Builtin_Set_Methods)\n", " - [7.5. Frozen Sets](#Frozen_Sets)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n",
"
\n",
"
| Name | \n", "Type in Python | \n", "Description | \n", "Example | \n", "
|---|---|---|---|
| Sets | \n", "set | \n", "unordered collection of unique elements. | \n", "{10, 'hello'} | \n", "
| Method | \n", "Description | \n", "
|---|---|
| union(set) | \n", "merge sets and keep unique elements from all sets | \n", "
| intersection(set) | \n", "return the set of elements present in all sets | \n", "
| difference(set) | \n", "x1.difference(x2) return the set of all elements that are in x1 but not in x2 | \n", "
| symmetric_difference(set) | \n", "return the set of all elements in either sets | \n", "
| isdisjoint(set) | \n", "determines whether or not two sets have any elements in common. returns True if they have no elements in common | \n", "
| issubset(set) | \n", "determine whether one set is a subset of the other | \n", "
| issuperset(set) | \n", "set a is considered as the superset of b, if all the elements of set b are the elements of set a | \n", "
| update(set) | \n", "adds any elements in new set that our set does not already have | \n", "
| intersection_update(set) | \n", "retain only elements found in both | \n", "
| difference_update(set) | \n", "it's like difference method except it updates the original set | \n", "
| symmetric_difference_update(set) | \n", "it's like symmetric difference method except it updates the original set | \n", "
| add(set) | \n", "add an item to the set | \n", "
| remove(m) | \n", "remove m from the set | \n", "
| discard(m) | \n", "remove m from the set. However, if m is not in set, discard does nothing instead of raising an exception | \n", "
| pop() | \n", "removes a random element from the set | \n", "
| clear() | \n", "removes all elements from the set | \n", "
\n",
"
\n",
"
\n",
"
\n",
"
\n",
"
\n",
"
\n",
"
\n",
"