Python Practice › Set Programs › Program 43
Set of even numbers
Write a Python program to create a set of even numbers from 1 to 20.
Output
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}Approach
- A set comprehension.
Solution
print({n for n in range(1, 21) if n % 2 == 0})Output
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}Learn the concept
New to this topic? Read the lesson, then come back.