7. Problem to find the intersection of two sets

In this Python set program, we will find the intersection of two sets using Python 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 of two sets:

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the sets.
3. Get the intersection of sets using the intersection() function.
4. Print the output.

				
					a = {1,2,4,5}
b = {7,8,9,1}
print("Original set1: ",a)
print("Original set2: ",b)
print("Intersection of a and b: ",a.intersection(b))
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {8, 9, 1, 7}
Intersection of a and b:  {1}
				
			

find the union of two sets.

find the difference of two sets.

6. Problem to find the union of two sets

In this Python set program, we will find the union of two sets using Python 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 two sets: Union is the combination of all the elements from both sets.

Union of two sets:

Steps to solve the program

1. Create two sets using {}.
2. Add some elements in the sets.
3. Get the union of two sets using union() function.
4. Print the output.

				
					a = {1,2,4,5}
b = {7,8,9,1}
print("Original set1: ",a)
print("Original set2: ",b)
print("Union of a and b: ",a.union(b))
				
			

Output :

				
					Original set1:  {1, 2, 4, 5}
Original set2:  {8, 9, 1, 7}
Union of a and b:  {1, 2, 4, 5, 7, 8, 9}
				
			

check if an element is present in a set.

Find the intersection of two sets.

5. Problem to check if an element is in a set

In this python set program, we will check if an element is in a set and if yes then we will print True, else print False 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 an element is in a set:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Use an if-else statement to check whether 5 is in the set or not.
4. Print the respective output.

				
					a = {1,2,3,5,6}
print("Original set: ",a)
if 5 in a:
    print("True")
else:
    print("False")
				
			

Output :

				
					Original set:  {1, 2, 3, 5, 6}
True
				
			

Find the length of a set.

find the union of two sets.

4. Problem to find the length of a set

In the Python set program, we will find the length of a set and print it to see the total number of elements in that 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.

Length of a set:

Steps to solve the program

1. Create a set using {}.
2. Add some elements to the set.
3. Find the length of set using the len() function.
4. Print the output.

				
					a = {1,2,3,5,6}
print("Original set: ",a)
print("Length of given set: ",len(a))
				
			

Output :

				
					Original set:  {1, 2, 3, 5, 6}
Length of given set:  5
				
			

remove an element from a set.

check if an element is present in a set.

3. Problem to remove an element from set

In this Python set program, we will remove an element from set and print the set to see the result 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 an element from set:

Steps to solve the program

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

				
					a = {1,2,3,5,6}
print("Original set: ",a)
a.remove(5)
print("New set: ",a)
				
			

Output :

				
					Original set:  {1, 2, 3, 5, 6}
New set:  {1, 2, 3, 6}
				
			

add an element to a set.

find the length of a set.

2. Problem to add elements to set in python

In this Python set program, we will add elements to set in Python and print the set to see the result 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 elements to set:

Steps to solve the program
  1. Create a set using {}.
  2. Add some elements in the set.
  3. Add an element in the set using add() function.
  4. Print the output.
				
					a = {1,2,3,5,6}
print("Original set: ",a)
a.add(7)
print("New set: ",a)
				
			

Output :

				
					Original set:  {1, 2, 3, 5, 6}
New set:  {1, 2, 3, 5, 6, 7}
				
			

create a set with some elements.

remove an element from a set.

1. Problem to create a set in Python

In this Python set program, we will create a set in Python and add some elements in it as an example 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.

Create a set in Python: 

Steps to solve the program
  1. Create a set using {}.
  2. Add some elements to the set.
  3. Print the set.
				
					a = {1,2,3,5,6}
print("Set: ",a)
				
			

Output :

				
					Set:  {1, 2, 3, 5, 6}
				
			

add an element to a set.