7554425D-0054-440D-B95E-D2ABC13D62CD Created with sketchtool.
Your Saved Info Packages

View your saved Course or Program Packages containing pricing and detailed curriculum.

Speak with a Learning Advisor.

Have any questions? We'll call you.

Fill out the form below and a Learning Advisor will reach out at a time convenient for you.

Please pick a valid date and time between 9 AM and 8 PM eastern (Monday to Friday)

By clicking "Book a call," you accept our Terms and will also receive exclusive offers and updates about new courses, workshops and events.

Python Tutorial

Python Set

Ready to start your career in Data? Find out more about BrainStation's Python Course

Python sets just an unordered collection of items which does not allow duplicate elements. Python set is used when data structure needs to only have unique elements in it. Python sets also allow mathematical operations like union, intersection, and difference to find common and unique items between two sets.

Let’s dive into the Python set and understand how to use them as a data type. Below is an example that creates two sets as a wishlist of movies to watch for two different people. Sets are created using curly brackets. As you can see, Python sets do not allow duplicate values and will remove it automatically from a set.

Mathematical Operations on Python Sets

Let’s use common mathematical set operations like union, intersection and difference on our sets. Union will combine all the elements from both sets, intersection will provide all the elements present in both sets and difference will provide all the elements unique to each set.

Let’s do the following set operations on our movies wishlist for john and adam.

  • Movies in John’s wishlist but not Adam’s - using - operator
  • Movies in either John’s or Adam’s wishlist or in both of their wishlist - using | operator
  • Movies in both John’s and Adam’s wishlist - using & operator
  • Movies unique in John’s and Adam’s wishlist but not both - using ^ operator
Set Password

You already have an account with BrainStation, but you still need to set up a password.