- Python Features
- Python Installation
- PyCharm Configuration
- Python Variables
- Python Data Types
- Python If Else
- Python Loops
- Python Strings
- Python Lists
- Python Tuples
- Python List Vs Tuple
- Python Sets
- Python Dictionary
- Python Functions
- Python Files I/O
- Read Write Excel
- Read Write JSON
- Read Write CSV
- Python OS Module
- Python Exceptions
- Python Datetime
- Python Collection Module
- Python Sys Module
- Python Decorator
- Python Generators
- Python OOPS
- Python Numpy Module
- Python Pandas Module
- Python Sqlite Module
Python Set Introduction:
In Python, a set is an unordered collection of unique elements. Sets are mutable and can be modified using various methods.
Creating Sets:
To create a set in Python, you can use curly braces `{}` or the built-in `set()` function. Here’s an example.
# Using curly braces
my_set = {1, 2, 3, 4, 5}
# Using set() function
my_set = set([1, 2, 3, 4, 5])
Features of Python Set:
- Uniqueness: Sets are collections of unique elements. Each element appears only once in a set. If you try to add a duplicate element, it will be ignored.
- Mutable: Sets are mutable, meaning you can modify them by adding or removing elements after they are created.
- Unordered: Sets are unordered collections, which means the elements are not stored in any particular order. You cannot access elements by indexing or slicing.
- Creation: Sets can be created using curly braces `{}` or the built-in `set()` function. For example.
my_set = {1, 2, 3} # Using curly braces
my_set = set([1, 2, 3]) # Using set() function
5. Membership Testing: Sets provide an efficient way to test if an element exists in a set using the `in` operator. This operation has a constant-time complexity compared to lists or tuples.
my_set = {1, 2, 3}
print(1 in my_set) # Output: True
print(5 in my_set) # Output: False
6. Iteration: You can iterate over the elements of a set using a `for` loop, which will visit each element in an arbitrary order.
my_set = {1, 2, 3}
for element in my_set:
print(element)
Deleting Python set:
In Python, you can delete a set using the `del` keyword. Here’s an example of how to delete a set. Here’s an example:
my_set = {1, 2, 3, 4, 5}
# Deleting the set
del my_set
# Trying to access the set after deletion will raise an error
print(my_set) # Raises NameError: name 'my_set' is not defined
In the example above, the `del` keyword is used to delete the `my_set` variable, which contains the set. After deleting the set, any attempt to access or use the set will result in a `NameError` because the set no longer exists.
Python Set operators:
- Union: The union of two sets `A` and `B` contains all unique elements from both sets. In Python, you can perform the union operation using the `union()` method or the `|` operator.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union = set1.union(set2) # Using method
union = set1 | set2 # Using operator
print(union) # Output: {1, 2, 3, 4, 5}
- Intersection: The intersection of two sets `A` and `B` contains only the elements that are common to both sets. In Python, you can perform the intersection operation using the `intersection()` method or the `&` operator.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection = set1.intersection(set2) # Using method
intersection = set1 & set2 # Using operator
print(intersection) # Output: {3}
- Difference: The difference between two sets `A` and `B` contains the elements that are in `A` but not in `B`. In Python, you can perform the difference operation using the `difference()` method or the `-` operator.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference = set1.difference(set2) # Using method
difference = set1 - set2 # Using operator
print(difference) # Output: {1, 2}
- Symmetric Difference: The symmetric difference of two sets `A` and `B` contains the elements that are in either `A` or `B`, but not both. In Python, you can perform the symmetric difference operation using the `symmetric_difference()` method or the `^` operator.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
symmetric_difference = set1.symmetric_difference(set2) # Using method
symmetric_difference = set1 ^ set2 # Using operator
print(symmetric_difference) # Output: {1, 2, 4, 5}
Python Set functions:
- len(): The `len()` function returns the number of elements in a set.
my_set = {1, 2, 3, 4, 5, 6}
length = len(my_set)
print(length) # Output: 6
- add(): The `add()` method adds an element to a set.
my_set = {1, 2, 3, 4}
my_set.add(5)
print(my_set) # Output: {1, 2, 3, 4, 5}
- remove(): The `remove()` method removes an element from a set. It raises a KeyError if the element does not exist in the set.
my_set = {1, 2, 3, 4}
my_set.remove(3)
print(my_set) # Output: {1, 2, 4}
- discard(): The `discard()` method removes an element from a set if it exists. It does not raise an error if the element is not found.
my_set = {1, 2, 3, 4}
my_set.discard(4)
print(my_set) # Output: {1, 2, 3}
- pop(): The `pop()` method removes and returns an arbitrary element from the set. Since sets are unordered, the popped element is not guaranteed to be the first or last element.
my_set = {1, 2, 3, 4}
element = my_set.pop()
print(element) # Output: a random element, e.g., 1
print(my_set) # {2, 3, 4}
- clear(): The `clear()`method removes all elements from a set, making it empty.
my_set = {1, 2, 3, 4}
my_set.clear()
print(my_set) # Output: set()
- copy(): The `copy()` method creates a shallow copy of a set, allowing you to work with a separate copy of the original set.
my_set = {1, 2, 3}
my_set_new = my_set.copy()
print(my_set_new) # Output: {1, 2, 3}