Python Practice › Set Programs › Program 2

Add elements to a set

EasySet Programs · 2 of 48

Write a Python program to add elements to a set.

Advertisement
Input
{1, 2}
Output
{1, 2, 3}

Approach

  1. add() for one item; adding an existing item does nothing.

Solution

Python
s = {1, 2}s.add(3)s.add(2)print(s)
Output
{1, 2, 3}

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Leave a Comment