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


This Python code creates a list called common_multiples using a list comprehension to find and store the numbers from 1 to 100 that are multiples of both 3 and 5. 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 of code initializes a variable named common_multiples and assigns it the result of a list comprehension.
    • for x in range(1, 101): This part sets up a loop that iterates through numbers from 1 to 100 (inclusive). The range(1, 101) function generates a sequence of numbers starting from 1 and ending at 100.
    • if x % 3 == 0 and x % 5 == 0: This is a condition that checks whether the current value of x is a multiple of both 3 and 5. The % operator calculates the remainder when x is divided by 3 and 5. If the remainder is 0 for both divisions, it means x is a multiple of both 3 and 5.
    • [x for x in range(1, 101) if x % 3 == 0 and x % 5 == 0] : This is the list comprehension itself. It iterates through the numbers in the specified range (1 to 100) and, for each number that is a multiple of both 3 and 5, includes it in the new list.
  • print(common_multiples): This line of code prints the common_multiples list 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

[15, 30, 45, 60, 75, 90]

Example Programs