Python Sets

In Python, a set is a data structure that stores an unordered collection of unique items. Unlike a list or a tuple, a set does not allow duplicate items, and the items in a set are not ordered. In Python, sets are represented by curly braces {} and the items in a set are separated by commas.

Creating a Set

To create a set in Python, use curly braces {} and separate the items in the set with commas. Here are some examples of creating sets in Python:

# Create an empty set
my_set = set()

# Create a set of numbers
numbers = {1, 2, 3, 4, 5}

# Create a set of strings
colors = {"red", "green", "blue"}

# Create a set of mixed data types
mixed = {1, "two", 3.0, (4, 5)}

As you can see, a set can store items of any data type, but it does not allow duplicate items.

Accessing and Modifying Set Items

In Python, you cannot access the items in a set using indexing, because sets are unordered collections of items. Instead, you can use the in keyword to check if an item is in a set, and you can use the add() and remove() methods to add or remove items from a set. Here are some examples of accessing and modifying set items in Python:

# Check if an item is in a set
print(1 in numbers)  # Output: True

# Add an item to a set
numbers.add(6)
print(numbers)  # Output: {1, 2, 3, 4, 5, 6}

# Remove an item from a set
numbers.remove(4)
print(numbers)  # Output: {1, 2, 3, 5, 6}

As you can see, you can use the in keyword and the add() and remove() methods to access and modify the items in a set.

Common Set Methods

In Python, sets have a few built-in methods that allow you to manipulate the items in a set. Here are some common set methods that you might find useful:

  • add(): adds an item to a set
  • remove(): removes an item from a set
  • union(): returns the union of two sets (all the items that appear in either set)
  • intersection(): returns the intersection of two sets (all the items that appear in both sets)
  • difference(): returns the difference of two sets (all the items that appear in the first set, but not in the second set)
  • issubset(): returns True if the first set is a subset of the second set (all the items in the first set appear in the second set)

Here are some examples of using these set methods:

# Add an item to a set
colors.add("purple")
print(colors)  # Output: {"red", "green", "blue", "purple"}

# Remove an item from a set
colors.remove("green")
print(colors)  # Output: {"red", "blue", "purple"}

# Get the union of two sets
numbers = {1, 2, 3, 4, 5}
alphabets = {'a', 'b', 'c', 'd'}
print(numbers.union(alphabets))  # Output: {1, 2, 3, 4, 5, 'a', 'b', 'c', 'd'}

# Get the intersection of two sets
numbers = {1, 2, 3, 4, 5}
evens = {2, 4, 6, 8, 10}
print(numbers.intersection(evens))  # Output: {2, 4}

# Get the difference of two sets
numbers = {1, 2, 3, 4, 5}
evens = {2, 4, 6, 8, 10}
print(numbers.difference(evens))  # Output: {1, 3, 5}

# Check if a set is a subset of another set
numbers = {1, 2, 3, 4, 5}
evens = {2, 4, 6, 8, 10}
print(evens.issubset(numbers))  # Output: False

Conclusion

Sets are a powerful data structure in Python that allows you to store and manipulate collections of items. Sets are represented by curly braces {}, the items in a set are separated by commas, and you can access and manipulate the items in a set using set methods.

Sets are different from lists and tuples because they are unordered and they do not allow duplicate items. Sets are useful when you need to store a collection of items and perform set operations like union, intersection, and difference.

Server Academy Members Only

Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments

Saving Progress...

Sign up for free!

Sign up for free and get instant access to this course!.

Python 3 for Beginners

0%

0/1 Lessons

Installing Python on Windows

• 1hr 17min

0 / 4 lessons complete

Python Basics

• 28min

0 / 7 lessons complete

Python Variables

• 41min

0 / 8 lessons complete

Even more Python Variables!

• 41min

0 / 6 lessons complete

Conditional Statements

• 15min

0 / 3 lessons complete

Writing Functions

• 30min

0 / 5 lessons complete

Python Loops

• 23min

0 / 5 lessons complete

Python PIP and Modules

• 18min

0 / 4 lessons complete

RegEx

• 26min

0 / 4 lessons complete

Working with APIs

• 12min

0 / 3 lessons complete

Course Conclusion

• 2min

0 / 1 lessons complete