Generate a set of common multiples of 3 and 5 up to 100


This Python code uses a set comprehension to create a set named common_multiples. The set contains the common multiples of 3 and 5 in the range from 1 to 100. Here's how the code works:

  • common_multiples = {x for x in range(1, 101) if x % 3 == 0 and x % 5 == 0}: This line initializes the set common_multiples using a set comprehension.
    • for x in range(1, 101): This part of the code sets up a loop that iterates through the numbers from 1 to 100 (inclusive).
    • {x}: For each number x, this part includes it in the set if it is a multiple of 3 (x % 3 == 0) and also a multiple of 5 (x % 5 == 0).
  • print(common_multiples): This line of code prints the common_multiples set to the console.

Source Code

common_multiples = {x for x in range(1, 101) if x % 3 == 0 and x % 5 == 0}
print(common_multiples)

Output

{75, 45, 15, 90, 60, 30}

Example Programs