In this Python set program, we will create a set of odd numbers from 1 to 20 with the help of the below-given steps.
Odd number:
Odd numbers are those numbers that cannot 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 odd 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 odd 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 odd number: ",Set)
Output :
Set of odd number: {1, 3, 5, 7, 9, 11, 13, 15, 17, 19}
Related Articles
Python program to create a set of your favorite actors.
Python program to create a set of your favorite movies.
Python program to create two sets of books and find the intersection of sets.
Python program to find the longest word in a set.
Python program to create a set with some elements.
Python program to add an element to a set.