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.

Python file program to replace space by an underscore in a file

In this python file program, we will replace space by an underscore in a file with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.

				
					#readcontent.txt
My name is john.
I'm 20 years old.
I'm learning python at sqatools.
I'm from Australia.

				
			
Steps to solve the program
  1. Open the first file using open(“readcontent.txt”,”r”).
  2. Read the data in the file using read().
  3. Replace the space in the data with “_” using replace() and store the output in the new variable.
  4. Open the second file using open(“writecontent.txt”,”w”).
  5. Write the new data in the second file using write().
				
					# Open file in read mode
f1=open("readcontent.txt","r")
# Read data
data=f1.read()
# Replace space by underscore
data=data.replace(" ","_")
# Open file in write mode
f2=open("file2.txt","w")
# Write new data to file
f2.write(data)
				
			

Output: Open the writecontent.txt file to see the ouput.

My_name_is_john.
I’m_20_years_old.
I’m_learning_python_at_sqatools.
I’m_from_Australia.

 

 

display words from a file that has less than 5 characters.

Program to display words from a file that has less than 5 characters

In this python file program, we will display words from a file that has less than 5 characters with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.

				
					#readcontent.txt
My name is john.
I'm 20 years old.
I'm learning python at sqatools.
I'm from Australia.

				
			
Steps to solve the program
  1. Open the file by using open(“readcontent.txt”).
  2. Where the file name is the name of the file.
  3. Read the data and split it into words using file.read().split().
  4. Use a for loop to iterate over the words.
  5. If the length of the word is less than 5 then print that word.
  6. Use an if statement for this purpose.
				
					# Open the file
file = open('readcontent.txt')
# Read data and converting it into words
words = file.read().split()
# Iterate over words
for word in words:
# Check for words having length less than 5
    if len(word)<5:
    # Print output
        print(word,end=" ")
				
			

Output:

My name is I’m 20 old. I’m at I’m from

 

 

remove all the lines that contain the character ‘t’ in a file and write it to another file.

replace space by an underscore in a file.

Program to remove specific line in a file and write it to another file

In this python file program, we will remove specific line in a file and write it to another file with the help of the below-given steps. Let’s consider we have readcontent.txt file with the below content.

				
					#readcontent.txt
My name is john.
I'm 20 years old.
I'm learning python at sqatools.
I'm from Australia.

				
			
Steps to solve the program
  1. Open the first file in the reading mode using open(‘readcontent.txt’,’r’).
  2. Open the second file in the append mode using open(‘writecontent.txt’,’a’).
  3. Read the lines of the file using readlines().
  4. Use a for loop to iterate over the lines.
  5. Using an if statement to check whether ‘t’ is in the line or not.
  6. If yes then add that line to the second file using write().
  7. Now close both files.
				
					# open file read mode
f1=open('readcontent.txt','r')
# open file in append mode
f2=open('writecontent.txt','a')
# Read lines from the file
lines=f1.readlines()
# Iterate over lines
for line in lines:
# Check for t in the lines
    if 't' in line:
    # Write lines to writecontent.txt file
        f2.write(line)
# Close the file
f1.close()
f2.close()
				
			

Output : Open writecontent.txt file to see the output.

I’m learning python at sqatools.
I’m from Australia.

 

 

count the total number of consonants in a file.

display words from a file that has less than 5 characters.