Python Practice › Loop Programs › Program 1
Numbers divisible by 7 and multiples of 5
Write a Python program to find the numbers between 1500 and 2700 (both included) that are divisible by 7 and are multiples of 5.
Output
[1505, 1540, 1575, 1610, 1645, 1680, 1715, 1750, 1785, 1820, 1855, 1890, 1925, 1960, 1995, 2030, 2065, 2100, 2135, 2170, 2205, 2240, 2275, 2310, 2345, 2380, 2415, 2450, 2485, 2520, 2555, 2590, 2625, 2660, 2695] 35 numbers
Approach
- Loop over the range and keep numbers where
n % 7 == 0 and n % 5 == 0.
Solution
result = []for n in range(1500, 2701): if n % 7 == 0 and n % 5 == 0: result.append(n)print(result)print(len(result), "numbers")Output
[1505, 1540, 1575, 1610, 1645, 1680, 1715, 1750, 1785, 1820, 1855, 1890, 1925, 1960, 1995, 2030, 2065, 2100, 2135, 2170, 2205, 2240, 2275, 2310, 2345, 2380, 2415, 2450, 2485, 2520, 2555, 2590, 2625, 2660, 2695] 35 numbers
Line by line
Line 2
range(1500, 2701) includes 2700 because the end value is excluded.
Line 3
Both conditions must hold, so they are joined with and.
Tip
Divisible by both 7 and 5 means divisible by 35, so range(1505, 2701, 35) gives the same list without a test.
Learn the concept
New to this topic? Read the lesson, then come back.