Search code examples
pythonlist-comprehension

Find all multiples of 3 or 5 but not 15 using python


I want to find the multiples of 3 and 5 but not 15, in python by using list comprehension,

list = []

a = [a for a in range(1,100) if ((a % 3 == 0) and (a % 5 == 0))]
list.append(a)
print(a)

the logic is wrong


Solution

  • multiples = [num for num in range(1, 100) if ((num % 3 == 0) or (num % 5 == 0)) and (num % 15 != 0)] print(multiples)