Python Practice › List Programs › Program 20
Elements divisible by 3 and 7
Write a Python program to get all elements divisible by both 3 and 7.
Input
[21, 14, 63, 9, 42, 50]
Output
[21, 63, 42]
Approach
- Divisible by both 3 and 7 means divisible by 21.
Solution
numbers = [21, 14, 63, 9, 42, 50]print([n for n in numbers if n % 3 == 0 and n % 7 == 0])Output
[21, 63, 42]
Learn the concept
New to this topic? Read the lesson, then come back.