Open links in new tab
    • Work Report
    • Email
    • Rewrite
    • Speech
    • Title Generator
    • Smart Reply
    • Poem
    • Essay
    • Joke
    • Instagram Post
    • X Post
    • Facebook Post
    • Story
    • Cover Letter
    • Resume
    • Job Description
    • Recommendation Letter
    • Resignation Letter
    • Invitation Letter
    • Greeting Message
    • Try more templates
  1. Sets in Python are a collection of unique elements, meaning no duplicates are allowed. They are useful for various mathematical operations such as union, intersection, difference, and symmetric difference. Let's explore these operations with examples.

    Union

    The union of two sets includes all elements from both sets without duplicates. You can use the | operator or the union() method.

    A = {0, 2, 4, 6, 8}
    B = {1, 2, 3, 4, 5}
    # Using | operator
    print("Union:", A | B)
    # Using union() method
    print("Union:", A.union(B))
    Copied!

    Output:

    Union: {0, 1, 2, 3, 4, 5, 6, 8}
    Union: {0, 1, 2, 3, 4, 5, 6, 8}
    Copied!

    Intersection

    The intersection of two sets includes only the elements that are present in both sets. You can use the & operator or the intersection() method.

    # Using & operator
    print("Intersection:", A & B)
    # Using intersection() method
    print("Intersection:", A.intersection(B))
    Copied!

    Output:

    Intersection: {2, 4}
    Intersection: {2, 4}
    Copied!

    Difference

    Feedback
  2. Python Set (With Examples) - Programiz

    In Python, we create sets by placing all the elements inside curly braces {}, separated by commas. A set can have any number of items and they may be of different types (integ…
    Create An Empty Set in Python

    Creating an empty set is a bit tricky. Empty curly braces {}will make an empty dictionary in Python. To make a set without any elements, we use the set()function without any argument. For example, Output Here, 1. empty_set - an empty set created …

    Duplicate Items in A Set

    Let's see what will happen if we try to include duplicate items in a set. Here, we can see there are no duplicate items in the set as a set cannot contain duplicates.

    Add and Update Set Items in Python

    Sets are mutable. However, since they are unordered, indexing has no meaning. We cannot access or change an element of a set using indexing or slicing. The set data type does not support it.

  3. Sets in Python - GeeksforGeeks

    Jan 10, 2026 · In, Python Sets are implemented using a dictionary with dummy variables, where key beings the members set with greater optimizations to the …

  4. Python Sets - W3Schools

    Sets are used to store multiple items in a single variable. Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities …

  5. A Basic Guide to the Python Set By Practical Examples

    Mar 30, 2025 · In this tutorial, you'll learn about Python Set type and how to manage set elements effectively including adding, removing, and clearing.

  6. Python Set Examples: From Basic to Advanced Use Cases

    Nov 20, 2023 · We will go over the most common Python set operations, with some examples that represent each operation. You can also check out our other …

  7. People also ask
    Loading
    Unable to load answer
  8. Sets in Python – Real Python

    May 5, 2025 · In this tutorial, you’ll dive deep into the features of Python sets and explore topics like set creation and initialization, common set operations, set …

  9. Python Set (With Examples)

    In this tutorial, we will learn about Python dictionaries (creating sets, adding and removing elements, and other set operations) with the help of examples.

  10. Python Sets

    In this tutorial, you shall learn about the fundamentals of Sets in Python language, the basic operations with Sets, with examples.

  11. 5. Data Structures — Python 3.14.3 documentation

    1 day ago · 5. Data Structures ¶ This chapter describes some things you’ve learned about already in more detail, and adds some new things as well. 5.1. More on …

  12. Python Set: The Why And How With Example Code

    Sep 16, 2025 · Python sets can be used to deduplicate lists, but they can do much more. Learn all about sets with this clear tutorial full of example code.

By using this site you agree to the use of cookies for analytics, personalized content, and ads.Learn more about third party cookies|Microsoft Privacy Policy