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.

26. Problem to find difference 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.

Difference of two sets: The difference between sets A and B is the set of all elements of A that are not elements of B.

Difference between two sets:

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the set.
3. Subtract the second set from the first set to find the difference 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("Difference between two sets using - operator: ",a-b)
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {2, 4}
Difference between two sets using - operator:  {1, 5}
				
			

Related Articles

remove a random element from a set.

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

25. Problem to remove a random element from a set

In this Python set program, we will remove a random 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 a random element from a set:

Steps to solve the program

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

				
					a = {1,2,4,5}
print("Original set: ",a)
print("Remove element from set: ",a.pop())
print(a)
				
			

Output :

				
					Original set:  {1, 2, 4, 5}
Remove element from set:  1
{2, 4, 5}
				
			

Related Articles

remove all elements from a set.

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

24. Problem to remove all elements from a set

In this Python set program, we will remove all 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 all elements from a set:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Remove all elements from a set using clear() function.
4. Print the set to check the output.

				
					a = {1,2,4,5}
print("Original set: ",a)
a.clear()
print(a)
				
			

Output :

				
					Original set:  {1, 2, 4, 5}
set()
				
			

Related Articles

remove a random element from a set.