36. Problem to check if two sets are equal

In this Python set program, we will check if two sets are equal or not with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Check if two sets are equal:

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the set.
3. Use an if-else statemnet with “==” operator to check if two sets are equal or not.
4. Print the respective output

				
					a = {1, 2, 4, 5, 7, 8, 9}
b = {2,3,4}
print("Original set1: ",a)
print("Original set2: ",b)
if a == b:
    print("Both sets are equal")
else:
    print("Both sets are not equal")
				
			

Output :

				
					Original set1:  {1, 2, 4, 5, 7, 8, 9}
Original set2:  {2, 3, 4}
Both sets are not equal
				
			

Related Articles

check if a set is empty.

check if a set is a frozen set.

35. Problem to check if a set is empty

In this Python set program, we will check if a set is empty or not with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Check if a set is empty:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. If the length of the set is equal to 0 then set is empty, else not.
4. Use an if-else statement for this purpose.

				
					a = {1, 2, 4, 5, 7, 8, 9}
print("Original set1: ",a)
if len(a) == 0:
    print("Set is empty")
else:
    print("Set is not empty")
				
			

Output :

				
					Original set1:  {1, 2, 4, 5, 7, 8, 9}
Set is not empty
				
			

Related Articles

remove multiple elements from a set.

check if two sets are equal.

34. Problem to remove multiple elements from a set

In this Python set program, we will remove multiple elements from a set with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Remove multiple elements from a set:

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the set.
3. Use a for loop to iterate over the second set.
4. Use an if statement to check whether the element is in the first set.
5. If yes then remove that element from the first set using remove() function.
6. Print the set.

				
					a = {1, 2, 4, 5, 7, 8, 9}
print("Original set: ",a)
b = {7,8,9}
for ele in b:
    if ele in a:
        a.remove(ele)
print("New set: ",a)
				
			

Output :

				
					Original set:  {1, 2, 4, 5, 7, 8, 9}
New set:  {1, 2, 4, 5}

				
			

Related Articles

add multiple elements to a set.

check if a set is empty.

33. Problem to add multiple element to set

In this Python set program, we will add multiple element to set with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Add multiple element to set:

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the set.
3. Use a for loop to iterate over the second set.
4. During each iteration add the element to the first set using add() function.
5. Print the new set.

				
					a = {1,2,4,5}
print("Original set: ",a)
b = {7,8,9}
for ele in b:
    a.add(ele)
print("New set: ",a)
				
			

Output :

				
					Original set:  {1, 2, 4, 5}
New set:  {1, 2, 4, 5, 7, 8, 9}
				
			

Related Articles

remove a specific element from a set.

remove multiple elements from a set.

32. Problem to remove specific element from a set

In this Python set program, we will remove specific element from a set with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Remove specific element from a set:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Remove specific element from a set using the remove() function.
4. Print the set to see the result.

				
					a = {1,2,4,5}
print("Original set: ",a)
a.remove(5)
print("After removing 5 from the given set: ",a)
				
			

Output :

				
					Original set:  {1, 2, 4, 5}
After removing 5 from the given set:  {1, 2, 4}
				
			

Related Articles

find the common elements between two sets.

add multiple elements to a set.

31. Problem to find the common elements between two sets

In this Python set program, we will find the common elements between two sets with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Common elements between two sets:

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the set.
3. Use a for loop to iterate over elements in the first set.
4. Use an if statement to check whether the element is in the second set.
5. If yes then print that element.

				
					a = {1,2,4,5}
b = {4,1}
print("Original set1: ",a)
print("Original set2: ",b)
print("Common elements: ")
for ele in a:
    if ele in b:
        print(ele)
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {1, 4}
Common elements: 
1
4
				
			

Related Articles

check if a set is a superset of another set.

remove a specific element from a set.

30. Problem to check if a set is a superset

In this Python set program, we will check whether the given set is a superset of another set with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Superset: If there are two sets A and B, then set A is considered as the superset of B, if all the elements of set B are the elements of set A

Set is a superset:

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the set.
3. Use issuperset to check whether the first set is a superset of second set.
4. Print the output.

				
					a = {1,2,4,5}
b = {4,1}
print("Original set1: ",a)
print("Original set2: ",b)
print("A is superset of B: ",a.issuperset(b))
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {1, 4}
A is superset of B:  True
				
			

Related Articles

find the symmetric difference of two sets using the “^” operator

find the common elements between two sets.

29. Problem to find the symmetric difference between two sets

In this Python set program, we will find the symmetric difference between two sets using the “^” operator with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Symmetric of sets: The symmetric difference of set A and B is the set of elements which are in either of the sets A and B, but not in their intersection.

Symmetric difference between two sets:

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the set.
3. Use “^” to find the symmetric difference between sets using the “^” operator.
4. Print the output.

				
					a = {1,2,4,5}
b = {4,1}
print("Original set1: ",a)
print("Original set2: ",b)
print("Symmetric difference of two sets using the “^” operator: ",a^b)
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {1, 4}
Symmetric difference of two sets using the “^” operator:  {2, 5}
				
			

Related Articles

find the union of multiple sets using the | operator.

check if a set is a superset of another set.

28. Problem to find the union of multiple sets

In this Python set program, we will find the union of multiple sets by using the | operator with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Union of sets: Union is the combination of all the elements from more than one sets.

Union of multiple sets:

Steps to solve the program

1. Create multiple sets using {}.
2. Add some elements in the set.
3. Use “|” to find the union of sets using the “|” operator.
4. Print the output.

				
					a = {1,2,4,5}
b = {7,8}
c={6,10,0}
print("Original set1: ",a)
print("Original set2: ",b)
print("Original set3: ",c)
print("Union of multiple sets using the | operator : ",a|b|c)
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {8, 7}
Original set3:  {0, 10, 6}
Union of multiple sets using the | operator :  {0, 1, 2, 4, 5, 6, 7, 8, 10}
				
			

Related Articles

find the intersection between two sets using the “&” operator.

find the symmetric difference of two sets using the “^” operator

27. Problem to find the intersection between two sets

In this Python set program, we will find the difference between two sets with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Intersection of two sets: The intersection is common elements between the two sets.

Intersection between two sets:

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the set.
3. Use “&” to find the intersection between sets using the “&” operator.
4. Print the output.

				
					a = {1,2,4,5}
b = {2,4}
print("Original set1: ",a)
print("Original set2: ",b)
print("Intersection between two sets using the “&” operator: ",a&b)
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {2, 4}
Intersection between two sets using the “&” operator:  {2, 4}
				
			

Related Articles

find the difference between two sets using the “-” operator.

find the union of multiple sets using the | operator.