46. Problem to create a set of even numbers

In this Python set program, we will create a set of even numbers from 1 to 20 with the help of the below-given steps.

Even number:
even numbers are those numbers that can be divided into two equal parts.

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.

Set of even numbers:

Steps to solve the program

1. Create a set using the set() function.
2. Use a for loop to iterate over numbers from 1 to 20.
3. Use an if statement to check whether the number is a even number or not.
4. If yes then add it to the set using the add() function.
5. Print the set to see the output.

				
					Set = set()
for num in range(1,21):
    if num%2 == 0:
        Set.add(num)
print("Set of even number: ",Set)
				
			

Output :

				
					Set of even number:  {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
				
			

Related Articles

convert a set to a dictionary with each element as key and value to an empty set.

create a set of odd numbers from 1 to 20.

Leave a Comment