Python Practice › Set Programs › Program 43

Set of even numbers

EasySet Programs · 43 of 48

Write a Python program to create a set of even numbers from 1 to 20.

Advertisement
Output
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}

Approach

  1. A set comprehension.

Solution

Python
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.

More set programs

← All 48 Set Programs

Leave a Comment