Python Sets
Server Academy Members Only
Sorry, this lesson is only available to Server Academy members. Create a free account now to get instant access to this and more free courses. Click the Sign Up Free button below to get access to our free courses now, or sign in if you have an account.
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 setremove()
: removes an item from a setunion()
: 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()
: returnsTrue
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..
Saving Progress...
Python 3 for Beginners
Installing Python on Windows • 1hr 17min
0 / 4 lessons complete
Section Overview
Free Preview Lesson
Text | 1 min
Downloading and Installing Python on Windows
Free Preview Lesson
Text | 8 min
Installing and configuring VS Code for Python
Free Preview Lesson
Text | 8 min
Lab: Installing Python
Devops Lab | 60
Python Basics • 28min
0 / 7 lessons complete
Section Overview
Text | 2 min
Executing Python Code
Free Preview Lesson
Text | 3 min
Python 3 Syntax
Free Preview Lesson
Text | 5 min
Help! Python Keywords
Free Preview Lesson
Text | 4 min
Printing to the console!
Text | 5 min
Python Operators
Text | 4 min
Section Review
Quiz | 5 min
Python Variables • 41min
0 / 8 lessons complete
Section Overview
Text | 4 min
Casting
Text | 4 min
Strings
Text | 5 min
Booleans
Text | 8 min
User Input
Text | 2 min
Numbers
Text | 7 min
NoneType
Free Preview Lesson
Text | 5 min
Assignment: Write a Mad Libs Script
Text | 6 min
Even more Python Variables! • 41min
0 / 6 lessons complete
Conditional Statements • 15min
0 / 3 lessons complete
Writing Functions • 30min
0 / 5 lessons complete
Section Overview
Text | 3 min
Defining Functions
Text | 4 min
Importing functions from another file
Text | 4 min
Assignment: Rock, paper, scissors
Text | 12 min
Assignment: Count Dictionary Words
Text | 7 min
Python Loops • 23min
0 / 5 lessons complete
Section Overview
Text | 2 min
For In Loops
Text | 5 min
While Loops
Text | 5 min
Nested Loops
Text | 3 min
Python Loops Challenge!
Text | 8 min
Python PIP and Modules • 18min
0 / 4 lessons complete
Section Overview
Text | 3 min
Installing Python PIP
Text | 4 min
Installing Modules with PIP
Text | 5 min
Importing Modules
Text | 6 min
RegEx • 26min
0 / 4 lessons complete
Section Overview
Text | 4 min
Regex 101
Text | 10 min
Importing Regex and manipulating strings
Text | 7 min
Regex Challenge!
Text | 5 min
Working with APIs • 12min
0 / 3 lessons complete
Making HTTP Requests
Text | 3 min
Working with JSON
Text | 5 min
Get your weather with the OpenWeatherMap
Text | 4 min
Course Conclusion • 2min
0 / 1 lessons complete