0% found this document useful (0 votes)
48 views

Sets in Python

A set in Python is an unordered collection of unique elements. Sets can be created using the set() function or curly braces. Elements within a set are immutable but the set itself is mutable. Common set operations include union, intersection, difference, and subset testing. Frozensets are like sets but are immutable.

Uploaded by

rishiraj goswami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Sets in Python

A set in Python is an unordered collection of unique elements. Sets can be created using the set() function or curly braces. Elements within a set are immutable but the set itself is mutable. Common set operations include union, intersection, difference, and subset testing. Frozensets are like sets but are immutable.

Uploaded by

rishiraj goswami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Sets in Python

A set is a group of elements that are not in any specific sequence. A Python set is comparable
to this mathematical description, but with the additional requirements listed below.

● The set's elements cannot be duplicated.


● The set's elements are immutable (cannot be changed), but the set as a whole is
mutable.
● A python set has no indexes associated with its elements. As a result, no indexing or
slicing operations are supported.

Defining a Set
A set can be created in two ways. First, you can define a set with the built-in set() function:
x = set(<iter>)

Output:

Alternately, a set can be defined with curly braces ({}):


x = {<obj>, <obj>, ..., <obj>}

Output:

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
Set Size and Membership
The len() function returns the number of elements in a set, and the in and not in operators can

Output:

Set Operations
Python sets are commonly used for mathematical operations such as union, intersection,
difference, and complement, among others. We can make a set, access its elements, and
perform the following mathematical operations, as indicated below.

Creating a set
A set is formed by using the set() function or by enclosing all elements in a pair of curly braces.

Ex

Output:
When the above code is run, it yields the following result. Please take note of how the order of
the elements has altered in the final output.

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
Getting Values from a Set
Individual values in a set cannot be accessed. We can only access all of the elements at the
same time, as illustrated above. However, by looping through the set, we may obtain a list of
individual elements.
Example

Output:
When the above code is run, it yields the following result:

Adding Items to a Set


The add() method is used to add elements to a set. As previously stated, there is no specific
index associated with the newly inserted element.
Ex:

Output:
When the above code is run, it yields the following result:

Removing Item from a Set


Using the discard() method, we can remove elements from a set. As previously stated, there is
no specific index associated with the newly inserted element.
Learnvista Pvt Ltd.
2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
Ex:

Output:
When the above code is run, it yields the following result.

Union of Sets
The union operation on two sets yields a new set that contains all of the different elements from
both sets. In the following example, the element "Wed" appears in both collections.
Ex:

Output:
When the above code is run, it yields the following result. Please keep in mind that the outcome
contains only one "wed."

Intersection of Sets
When two sets are intersected, a new set is created that contains just the elements that are
shared by both sets. In the following example, the element "Wed" appears in both collections.
Ex:

Output:
When the above code is run, it yields the following result. Please keep in mind that the outcome
contains only one "wed."

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
Difference of Sets
When you perform a difference operation on two sets, you get a new set that has only the items
from the first set and none from the second. The element "Wed" is present in both sets in the
example below, hence it will not be found in the result set.
Ex:

Output:
When the above code is run, it yields the following result. Please keep in mind that the outcome
contains only one "wed."

Compare Sets

We can determine whether a given set is a subset or superset of another. Depending on the
elements in the sets, the result is True or False.
Ex:

Output:
When the above code is run, it yields the following result:

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]
Frozen Sets
Python has an additional built-in type called a frozenset, which is identical to a set except that it
is immutable. On a frozenset, you can do the following non-modifying operations:

Output:

Methods that attempt to alter a frozenset, on the other hand, fail:

Output:

Learnvista Pvt Ltd.


2nd Floor, 147, 5th Main Rd, Rajiv Gandhi Nagar HSR Sector 7,Near Salarpuria Serenity, Bengaluru, Karnataka 560102
Mob:- +91 779568798, Email:- [email protected]

You might also like