-
Notifications
You must be signed in to change notification settings - Fork 189
docs: add comprehensive chapter on Sets in Python #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| --- | ||
| id: python-set | ||
| title: Set in Python | ||
| sidebar_label: Set in Python #displays in sidebar | ||
| sidebar_position: 10 | ||
| tags: | ||
| [ | ||
| Python, | ||
| List in Python, | ||
| Introduction of python, | ||
| Python Syntax, | ||
| Variables, | ||
| Operators, | ||
| Type Casting, | ||
| String, | ||
| Tuple in Python, | ||
| Set in Python | ||
| ] | ||
|
|
||
| --- | ||
|
|
||
| # Set in Python | ||
|
|
||
| A **Set** in Python is a built-in data structure used to store multiple **unique** elements in a single variable. Sets are **unordered** and **mutable**, making them an efficient tool for membership tests, eliminating duplicates, and performing mathematical set operations. | ||
|
|
||
|
|
||
| ## Why Use Sets? | ||
|
|
||
| Sets are commonly used when: | ||
| - You need to **store unique values only** | ||
| - The order of elements **does not matter** | ||
| - You want to perform **fast membership testing** | ||
| - You need efficient **union, intersection, and difference operations** | ||
|
|
||
|
|
||
| ## Key Characteristics | ||
|
|
||
| - **Unordered:** Elements have no defined order. | ||
| - **Unique:** No duplicates allowed. | ||
| - **Mutable:** You can add or remove items. | ||
| - **Iterable:** You can loop through a set. | ||
|
|
||
|
|
||
| ## Creating a Set | ||
|
|
||
| There are two main ways to create a set: | ||
|
|
||
| ### Using Curly Braces `{}` | ||
|
|
||
| ```python | ||
| fruits = {"apple", "banana", "cherry"} | ||
| print(fruits) # Output: {'banana', 'apple', 'cherry'} | ||
| ```` | ||
|
|
||
| ### Using the `set()` Constructor | ||
|
|
||
| ```python | ||
| numbers = set([1, 2, 3, 2]) | ||
| print(numbers) # Output: {1, 2, 3} | ||
| ``` | ||
|
|
||
| > **Important:** Empty curly braces `{}` create an **empty dictionary**, not a set. Use `set()` instead: | ||
|
|
||
| > | ||
| > ```python | ||
| > empty_set = set() | ||
| > ``` | ||
|
|
||
|
|
||
| ## Adding Elements to a Set | ||
|
|
||
| You can add elements using the `add()` method: | ||
|
|
||
| ```python | ||
| colors = {"red", "green"} | ||
| colors.add("blue") | ||
| print(colors) # Output: {'red', 'green', 'blue'} | ||
| ``` | ||
|
|
||
| ## Removing Elements from a Set | ||
|
|
||
| Python provides several methods to remove elements: | ||
|
|
||
| ### `remove()` | ||
|
|
||
| Removes the specified item. Raises an error if the item does not exist. | ||
|
|
||
| ```python | ||
| colors = {"red", "green"} | ||
| colors.remove("red") | ||
| print(colors) # Output: {'green'} | ||
| ``` | ||
|
|
||
| > If you try to remove an item not present: | ||
| > | ||
| > ```python | ||
| > colors.remove("yellow") # KeyError | ||
| > ``` | ||
|
|
||
|
|
||
| ### `discard()` | ||
|
|
||
| Removes the specified item without raising an error if the item is not found. | ||
|
|
||
| ```python | ||
| colors = {"green"} | ||
| colors.discard("yellow") # No error | ||
| ``` | ||
|
|
||
| ### `pop()` | ||
|
|
||
| Removes and returns an arbitrary element. | ||
|
|
||
| ```python | ||
| colors = {"red", "blue"} | ||
| removed = colors.pop() | ||
| print("Removed:", removed) | ||
| print("Remaining:", colors) | ||
| ``` | ||
|
|
||
|
|
||
| ### `clear()` | ||
|
|
||
| Removes all elements. | ||
|
|
||
| ```python | ||
| colors.clear() | ||
| print(colors) # Output: set() | ||
| ``` | ||
|
|
||
|
|
||
| ## Updating a Set | ||
|
|
||
| To add multiple items at once, use `update()`: | ||
|
|
||
| ```python | ||
| a = {1, 2} | ||
| a.update([3, 4], {5, 6}) | ||
| print(a) # Output: {1, 2, 3, 4, 5, 6} | ||
| ``` | ||
|
|
||
|
|
||
| ## Membership Testing | ||
|
|
||
| Check if an item is in a set using `in` or `not in`: | ||
|
|
||
| ```python | ||
| nums = {1, 2, 3} | ||
| print(2 in nums) # Output: True | ||
| print(5 not in nums) # Output: True | ||
| ``` | ||
|
|
||
|
|
||
| ## Common Set Operations | ||
|
|
||
| Python sets support powerful operations: | ||
|
|
||
| | Operation | Syntax | Description | | | ||
| | -------------------- | -------------------------------------- | ------------------------------------ | ----------------------------------- | | ||
| | Union | \`a | b`or`a.union(b)\` | Combine all elements from both sets | | ||
| | Intersection | `a & b` or `a.intersection(b)` | Elements common to both sets | | | ||
| | Difference | `a - b` or `a.difference(b)` | Elements in `a` but not in `b` | | | ||
| | Symmetric Difference | `a ^ b` or `a.symmetric_difference(b)` | Elements in either set, but not both | | | ||
|
|
||
| --- | ||
|
|
||
| ### Example | ||
|
|
||
| ```python | ||
| a = {1, 2, 3} | ||
| b = {3, 4, 5} | ||
|
|
||
| # Union | ||
| print(a | b) # {1, 2, 3, 4, 5} | ||
|
|
||
| # Intersection | ||
| print(a & b) # {3} | ||
|
|
||
| # Difference | ||
| print(a - b) # {1, 2} | ||
|
|
||
| # Symmetric Difference | ||
| print(a ^ b) # {1, 2, 4, 5} | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Iterating Over a Set | ||
|
|
||
| Loop through elements with a `for` loop: | ||
|
|
||
| ```python | ||
| animals = {"cat", "dog", "rabbit"} | ||
| for animal in animals: | ||
| print(animal) | ||
| ``` | ||
|
|
||
| > Since sets are unordered, the output order may differ every time. | ||
|
|
||
|
|
||
| ## Frozenset | ||
|
|
||
| A **frozenset** is an immutable version of a set. Once created, you cannot modify it. | ||
|
|
||
| ```python | ||
| fs = frozenset([1, 2, 3]) | ||
| print(fs) # frozenset({1, 2, 3}) | ||
|
|
||
| # fs.add(4) # AttributeError | ||
| ``` | ||
|
|
||
| Use `frozenset` when you need a **hashable set**, e.g., as dictionary keys. | ||
|
|
||
|
|
||
| ## When to Use Sets? | ||
|
|
||
| * Removing duplicates from lists | ||
| * Fast membership tests | ||
| * Performing set algebra (union, intersection) | ||
| * Representing unique collections | ||
|
|
||
|
|
||
| ## Best Practices | ||
|
|
||
| * Use `set()` to create empty sets. | ||
| * Use `discard()` if you are not sure whether an element exists. | ||
| * Prefer sets over lists when you need uniqueness or fast lookups. | ||
|
|
||
| --- | ||
|
|
||
| ## Practice Exercises | ||
|
|
||
| 1. **Create a set of your favorite fruits. Add one more fruit and remove another.** | ||
| 2. **Find the union and intersection of `{1, 2, 3}` and `{3, 4, 5}`.** | ||
| 3. **Write a program that removes duplicates from a list using a set.** | ||
| 4. **Create a frozenset and show that it cannot be modified.** | ||
|
|
||
| --- | ||
|
|
||
| ## Summary Table | ||
|
|
||
| | Feature | Description | | ||
| | --------------------- | -------------------------------------------- | | ||
| | **Mutable** | Yes (can add/remove elements) | | ||
| | **Ordered** | No | | ||
| | **Duplicates** | Not allowed | | ||
| | **Empty Declaration** | `set()` | | ||
| | **Immutable Variant** | `frozenset` | | ||
| | **Typical Use Cases** | Membership tests, uniqueness, set operations | | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.