Python Practice › Set Programs › Program 1
Create a set
Write a Python program to create a set and show that duplicates are removed.
Output
{8, 3, 5} 3
set 0Approach
- Use curly braces with values, or
set()for an empty set.
Solution
numbers = {5, 3, 8, 3, 5}empty = set()print(numbers, len(numbers))print(type(empty).__name__, len(empty))Output
{8, 3, 5} 3
set 0Line 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.