19. Problem to check if all elements in a set are odd

In this Python set program, we will check whether the all elements in a set are odd or not 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.

Elements in a set are odd:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Create a variable count and assign its value equal to 0.
4. Use a for loop to iterate over elements in the set.
5. Use an if statement inside the loop to check whether an element is not divisible by 2.
6. If yes then add 1 to the count variable.
7. Use an if-else statement to check whether the value of count variable is equal to the length of the set.
8. If yes then all elements in a set are odd,else not.
9. Print the respective output.

				
					Set = {1,2,3,4,5}
print("Original set1: ",Set)
count = 0
for ele in Set:
    if ele%2 != 0:
        count += 1
if count == len(Set):
    print("All elements in the set are odd")
else:
    print("All elements in the set are not odd")
				
			

Output :

				
					Original set1:  {1, 2, 3, 4, 5}
All elements in the set are not odd
				
			

Related Articles

check if all elements in a set are even.

check if all elements in a set are prime.

Leave a Comment