Create a set of even numbers from 1 to 50 that are not divisible by 4


This Python code creates a set named even_not_div_by_4, which contains even numbers between 2 and 50 (inclusive) that are not divisible by 4. Here's how the code works:

  • even_not_div_by_4 = {x for x in range(2, 51, 2) if x % 4 != 0}: This line initializes the set even_not_div_by_4 using a set comprehension.
    • for x in range(2, 51, 2): This part of the code iterates through even numbers in the range from 2 to 50, with a step of 2. This range includes all even numbers between 2 and 50.
    • if x % 4 != 0: This part of the code filters the numbers to include only those that are not divisible by 4. It checks if the remainder of the division of x by 4 is not equal to 0.
  • print(even_not_div_by_4): This line of code prints the even_not_div_by_4 set to the console.

Source Code

even_not_div_by_4 = {x for x in range(2, 51, 2) if x % 4 != 0}
print(even_not_div_by_4)

Output

{2, 34, 6, 38, 10, 42, 14, 46, 18, 50, 22, 26, 30}

Example Programs