Python Practice › Set Programs › Program 1

Create a set

EasySet Programs · 1 of 48

Write a Python program to create a set and show that duplicates are removed.

Advertisement
Output
{8, 3, 5} 3
set 0

Approach

  1. Use curly braces with values, or set() for an empty set.

Solution

Python
numbers = {5, 3, 8, 3, 5}empty = set()print(numbers, len(numbers))print(type(empty).__name__, len(empty))
Output
{8, 3, 5} 3
set 0

Line by line

Line 1

The repeated 3 and 5 are stored once.

Line 2

{} would create a dictionary, so an empty set needs set().

Learn the concept

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

More set programs

← All 48 Set Programs

Leave a Comment