Python Practice › Set Programs
Python Set Programs with Solutions
48 set programs: creating sets, adding and removing items, union, intersection, difference, subsets and frozensets. Sets of strings have no fixed order, so these solutions print sorted(...) whenever the order would otherwise change from run to run.
Before you start
Read the Sets lesson first if the topic is new to you.
All programs
- #1Create a set
Write a Python program to create a set and show that duplicates are removed.Easy - #2Add elements to a set
Write a Python program to add elements to a set.Easy - #3Remove an element from a set
Write a Python program to remove an element from a set.Easy - #4Length of a set
Write a Python program to find the number of items in a set.Easy - #5Is an element in a set?
Write a Python program to check whether an element is in a set.Easy - #6Union of two sets
Write a Python program to find the union of two sets.Easy - #7Intersection of two sets
Write a Python program to find the intersection of two sets.Easy - #8Difference of two sets
Write a Python program to find the items in the first set but not the second.Easy - #9Symmetric difference
Write a Python program to find the items in exactly one of two sets.Easy - #10Is one set a subset of another?
Write a Python program to check whether one set is a subset of another.Easy - #11Are two sets disjoint?
Write a Python program to check whether two sets have no items in common.Easy - #12List to a set
Write a Python program to convert a list to a set.Easy - #13Set to a list
Write a Python program to convert a set to a sorted list.Easy - #14Maximum element of a set
Write a Python program to find the largest element in a set.Easy - #15Minimum element of a set
Write a Python program to find the smallest element in a set.Easy - #16Sum of a set
Write a Python program to add up the elements of a set.Easy - #17Average of a set
Write a Python program to find the average of the elements of a set.Easy - #18Are all elements even?
Write a Python program to check whether all elements of a set are even.Easy - #19Are all elements odd?
Write a Python program to check whether all elements of a set are odd.Easy - #20Are all elements prime?
Write a Python program to check whether all elements of a set are prime, and list the primes.Easy - #21Is a set a subset of any other set?
Write a Python program to check whether a set is a subset of any set in a list.Easy - #22Cartesian product of two sets
Write a Python program to find the cartesian product of two sets.Easy - #23Clear a set
Write a Python program to remove all elements from a set.Easy - #24Remove an arbitrary element
Write a Python program to remove and return an arbitrary element withpop().Easy - #25difference() vs –
Write a Python program to find the difference between two sets with the method and the operator.Easy - #26Update a set to its intersection
Write a Python program to keep only the items shared with another set.Easy - #27Union of several sets
Write a Python program to find the union of several sets.Easy - #28Symmetric difference in place
Write a Python program to update a set to the symmetric difference with another set.Easy - #29Is a set a superset?
Write a Python program to check whether a set is a superset of another.Easy - #30Common elements
Write a Python program to find the elements common to two sets of words.Easy - #31Remove a specific element safely
Write a Python program to remove a specific element, reporting whether it was there.Easy - #32Add several elements
Write a Python program to add several elements at once.Easy - #33Remove several elements
Write a Python program to remove several elements at once.Easy - #34Is a set empty?
Write a Python program to check whether a set is empty.Easy - #35Are two sets equal?
Write a Python program to check whether two sets are equal.Easy - #36Is a value a frozenset?
Write a Python program to check whether a value is a frozenset.Easy - #37Create a frozenset
Write a Python program to create a frozenset and show that it cannot be changed.Easy - #38Difference with several sets
Write a Python program to remove from one set the items of several others.Easy - #39Intersection of several sets
Write a Python program to find the items shared by several sets.Easy - #40Set elements found in a string
Write a Python program to find which elements of a set appear in a string.Easy - #41"Index" of an element in a set
Write a Python program to find the position of an element in a set.Easy - #42Set to a dictionary
Write a Python program to convert a set into a dictionary.Easy - #43Set of even numbers
Write a Python program to create a set of even numbers from 1 to 20.Easy - #44Set of odd numbers
Write a Python program to create a set of odd numbers from 1 to 20.Easy - #45Set of favourite actors
Write a Python program to create a set of your favourite actors.Easy - #46Set of favourite movies
Write a Python program to create a set of favourite movies and add one more.Easy - #47Books two readers both own
Write a Python program to create two sets of books and find the books in both.Easy - #48Longest word in a set
Write a Python program to find the longest word in a set.Easy