Python set() Function

Python set() Function

Python set() Function

1. Introduction

The set() function is a built-in function in Python that creates a set. A set is an unordered, non-duplicate data structure consisting of multiple elements. Elements in a set can be of any type, such as numbers, strings, or other sets.

The syntax of the set() function is as follows:

set(iterable)

Parameter Description:

  • iterable: An iterable object, which can be a list, tuple, string, etc. If no parameter is provided or the parameter is None, an empty set is created.

2. Creating a Set

The set() function can be used to quickly create a set. You can convert an iterable object into a set by providing it as a parameter.

Here are some sample codes:

# Create an empty set
set1 = set()
print(set1) # Output set()

# Create a set containing elements
set2 = set([1, 2, 3, 4, 5])
print(set2) # Output {1, 2, 3, 4, 5}

# Create a set containing repeated elements
set3 = set([1, 2, 2, 3, 3, 3])
print(set3) # Output {1, 2, 3}

# Create a set containing strings
set4 = set("hello")
print(set4) # Output {'l', 'o', 'e', 'h'}

3. Basic Operations

3.1 Adding Elements

You can use the add() method to add elements to a set. If the element already exists in the set, it will not be added repeatedly.

Here is the sample code:

set1 = set([1, 2, 3])
set1.add(4)
print(set1) # Output {1, 2, 3, 4}

set1.add(2)
print(set1) # Output {1, 2, 3, 4}, element 2 already exists and will not be added repeatedly.

3.2 Deleting Elements

You can use the remove() method to remove elements from a set. If the element does not exist, a KeyError exception is raised.

The following is sample code:

set1 = set([1, 2, 3, 4])
set1.remove(3)
print(set1) # Outputs {1, 2, 4}

set1.remove(5) # Removes the non-existent element 5, raising a KeyError exception

3.3 Checking Whether an Element Exists

You can use the in keyword to check whether an element exists in a set.

The following is sample code:

set1 = set([1, 2, 3, 4])

if 3 in set1:
print("Element 3 exists in the set")
else:
print("Element 3 does not exist in the set")

3.4 Set Operations

Set operators (such as union, intersection, and difference) can be used to perform operations on multiple sets.

Here are some example codes:

set1 = set([1, 2, 3])
set2 = set([3, 4, 5])

# Union
union_set = set1 | set2
print(union_set) # Output {1, 2, 3, 4, 5}

# Intersection
intersection_set = set1 & set2
print(intersection_set) # Output {3}

# Difference
difference_set = set1 - set2
print(difference_set) # Output {1, 2}

4. Some Common Methods

The set() function also provides some other common methods for operating on sets.

4.1 Collection Length

You can use the len() method to get the length (number of elements) of a collection.

Here’s some sample code:

set1 = set([1, 2, 3, 4, 5])
print(len(set1)) # Outputs 5

4.2 Clearing a Collection

You can use the clear() method to clear all elements in a collection.

Here’s some sample code:

set1 = set([1, 2, 3])
set1.clear()
print(set1) # Outputs set()

4.3 Copying a Collection

You can use the copy() method to copy a collection.

The following is sample code:

set1 = set([1, 2, 3])
set2 = set1.copy()
print(set2) # Output {1, 2, 3}

4.4 Determining Whether a Set is Empty

You can use the is_empty() method to determine whether a set is empty.

The following is sample code:

set1 = set()
if not set1:
print("The set is empty")

5. Summary

The set() function is a built-in function in Python for creating sets. We can use the set() function to create a set and perform operations on the set using various methods.

This article introduces the basic usage of the set() function, including creating sets, adding elements, removing elements, determining whether an element exists, set operations, and common methods.

Leave a Reply

Your email address will not be published. Required fields are marked *