Python Practice › Set Programs › Program 2
Add elements to a set
Write a Python program to add elements to a set.
Input
{1, 2}Output
{1, 2, 3}Approach
add()for one item; adding an existing item does nothing.
Solution
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.