PYTHON SETS

In Python, a set is a collection of unique elements, similar to mathematical sets. Sets are mutable, unordered, and do not allow duplicate elements. Sets are defined using curly braces `{}` and elements are separated by commas. Sets are commonly used for tasks that involve testing membership, removing duplicates, and performing set operations such as union, intersection, and difference.

Characteristics of Sets:

1. Mutable:

– Sets can be modified after creation. You can add or remove elements from a set.

2. Unordered:

– Sets do not maintain the order of elements as they are added.

– Therefore, sets do not support indexing or slicing.

3. Unique Elements:

– Sets only contain unique elements. Duplicate elements are automatically removed.

Creating Sets:

– Sets are defined using curly braces `{}`.

– Elements within the set are separated by commas.

Example:

“`python

my_set = {1, 2, 3, 4, 5}

“`

Â

Here are some code examples illustrating the characteristics of sets in Python:

1. Mutable:

“`python

# Create a set

my_set = {1, 2, 3, 4, 5}

# Add an element to the set

my_set.add(6)

print(my_set) # Output: {1, 2, 3, 4, 5, 6}

# Remove an element from the set

my_set.remove(3)

print(my_set) # Output: {1, 2, 4, 5, 6}

“`

2. Unordered:

“`python

# Create a set

my_set = {3, 1, 4, 1, 5}

# Print the set

print(my_set) # Output: {1, 3, 4, 5}

“`

3. Unique Elements:

“`python

# Create a set with duplicate elements

my_set = {1, 2, 2, 3, 3, 4, 5, 5}

# Print the set

print(my_set) # Output: {1, 2, 3, 4, 5}

“`

These examples demonstrate how sets in Python are mutable, unordered, and contain only unique elements, making them useful for various applications such as removing duplicates from lists or performing set operations like union, intersection, and difference.

ACCESSING ITEMS IN PYTHON SETS

– Since sets are unordered, they do not support indexing or slicing.

– You can iterate over the elements of a set using loops.

Example:

“`python

for element in my_set:

Â Â Â Â print(element)

“`

Â

Checking for Element Existence: You can check if an element exists in a set using the `in`

keyword.

“`python

my_set = {1, 2, 3, 4, 5}

# Check if an element exists

if 3 in my_set:

print(“Element found in the set”)

“`

Adding and Removing Elements:

– You can add elements to a set using the `add()` method, and remove elements using the `remove()` or `discard()` methods.

– If you attempt to remove an element that does not exist in the set, an error will occur with `remove()`, while `discard()` will simply do nothing.

Example:

“`python

my_set.add(6)

my_set.remove(3)

“`

Â

ALL AVAILABLE FUNCTIONS IN PYTHON SET

To list all available functions in Python’s set data type, you can use the `dir()` function. This function returns a list of valid attributes and methods for the specified object. Here’s how you can do it for sets:

“`python

# Get all available functions for sets

set_functions = [func for func in dir(set) if not func.startswith(“__”)]

# Print the list of functions

print(set_functions)

“`

This code will print out a list of all available functions (methods) for sets in Python. You can then explore these functions further in the Python documentation or through other resources.

Â

COMMON IN-BUILT FUNCTIONS FOR PYTHON SETS

Here are some common functions and methods used with sets in Python:

1. `len()`:

– Returns the number of elements in the set.

“`python

my_set = {1, 2, 3, 4, 5}

print(len(my_set)) # Output: 5

“`

2. `add()`:

– Adds an element to the set if it is not already present.

“`python

my_set = {1, 2, 3}

my_set.add(4)

print(my_set) # Output: {1, 2, 3, 4}

“`

3. `remove()`:

– Removes the specified element from the set. Raises a KeyError if the element is not present.

“`python

my_set = {1, 2, 3}

my_set.remove(2)

print(my_set) # Output: {1, 3}

“`

4. `discard()`:

– Removes the specified element from the set if it is present. Does not raise any error if the element is not present.

“`python

my_set = {1, 2, 3}

my_set.discard(2)

print(my_set) # Output: {1, 3}

“`

5. `pop()`:

– Removes and returns an arbitrary element from the set. Raises a KeyError if the set is empty.

“`python

my_set = {1, 2, 3}

x = my_set.pop()

print(x) # Output: 1 or 2 or 3 (arbitrary element)

“`

6. `clear()`:

– Removes all elements from the set.

“`python

my_set = {1, 2, 3}

my_set.clear()

print(my_set) # Output: set()

“`

7. `union()`:

– Returns a new set containing all distinct elements from both sets.

“`python

set1 = {1, 2, 3}

set2 = {3, 4, 5}

union_set = set1.union(set2)

print(union_set) # Output: {1, 2, 3, 4, 5}

“`

8. `intersection()`:

– Returns a new set containing only the common elements between two sets.

“`python

set1 = {1, 2, 3}

set2 = {3, 4, 5}

intersection_set = set1.intersection(set2)

print(intersection_set) # Output: {3}

“`

9. `difference()`:

– Returns a new set containing elements that are present in the first set but not in the second set.

“`python

set1 = {1, 2, 3}

set2 = {3, 4, 5}

difference_set = set1.difference(set2)

print(difference_set) # Output: {1, 2}

“`

10. `symmetric_difference()`:

– Returns a new set containing elements that are present in either set, but not in both.

“`python

set1 = {1, 2, 3}

set2 = {3, 4, 5}

sym_diff_set = set1.symmetric_difference(set2)

print(sym_diff_set) # Output: {1, 2, 4, 5}

“`

11. `issubset()`:

– Returns `True` if all elements of the set are present in the specified set.

“`python

set1 = {1, 2}

set2 = {1, 2, 3, 4, 5}

print(set1.issubset(set2)) # Output: True

“`

12. `issuperset()`:

– Returns `True` if the set contains all elements of the specified set.

“`python

set1 = {1, 2, 3, 4, 5}

set2 = {1, 2}

print(set1.issuperset(set2)) # Output: True

“`

13. `copy()`:

– Returns a shallow copy of the set.

“`python

set1 = {1, 2, 3}

set2 = set1.copy()

print(set2) # Output: {1, 2, 3}

“`

These are some of the commonly used functions and methods available for working with sets in Python.

Set Operations:

– Sets support various mathematical operations such as union, intersection, difference, and symmetric difference.

– These operations can be performed using built-in methods or operators.

Example:

“`python

set1 = {1, 2, 3}

set2 = {3, 4, 5}

union_set = set1.union(set2)

intersection_set = set1.intersection(set2)

difference_set = set1.difference(set2)

“`

USE CASES:

– Sets are useful for removing duplicates from a list or sequence.

– They are commonly used for testing membership and performing set operations in mathematical and computational tasks.

Sets provide a convenient way to work with unique collections of elements and perform set operations efficiently. Their flexibility and built-in methods make them valuable tools in Python programming.

Â

EXERCISES

Here are some Python set exercises for practice:

1. Set Intersection:

Write a Python program to find the intersection of two sets.

2. Set Union:

Write a Python program to find the union of two sets.

3. Set Difference:

Write a Python program to find the difference between two sets.

4. Set Symmetric Difference:

Write a Python program to find the symmetric difference between two sets.

5. Set Subset:

Write a Python program to check if a set is a subset of another set.

6. Set Superset:

Write a Python program to check if a set is a superset of another set.

7. Set Disjoint:

Write a Python program to check if two sets are disjoint.

8. Set Update:

Write a Python program to update a set with the union of itself and others.

9. Set Intersection Update:

Write a Python program to update a set with the intersection of itself and others.

10. Set Difference Update:

Write a Python program to update a set with the difference of itself and another set.

These exercises cover various set operations and will help you become familiar with working with sets in Python.

Â