Python Set Programs, Exercises

Python set programs and exercises help beginners to get expertise in set data type, set is a collection of unique data enclosed with ‘{}’ curly braces. User can store only immutable data type to the set. e.g (int, float, string, tuple), mutable data type is not allowed as set element. e.g.  (list, dictionary, set), Let’s consider if we want to store student data, with unique id, then we can use set data type to store the data.

1). Python program to create a set with some elements.

2). Python program to add an element to a set.

3). Python program to remove an element from a set.

4). Python program to find the length of a set.

5). Python program to check if an element is present in a set.

6). Python program to find the union of two sets.

7). Python program to find the intersection of two sets.

8). Python program to find the difference of two sets.

9). Python program to find the symmetric difference of two sets.

10). Python program to show if one set is a subset of another set.

11). Python program to check if two sets are disjoint.

12). Python program to convert a list to a set.

13). Python program to convert a set to a list.

14). Python program to find the maximum element in a set.

15). Python program to find the minimum element in a set.

16). Python program to find the sum of elements in a set.

17). Python program to find the average of elements in a set.

18). Python program to check if all elements in a set are even.

19). Python program to check if all elements in a set are odd.

20). Python program to check if all elements in a set are prime.

21). Python program to check if a set is a proper subset of another set.

22). Python program to find the cartesian product of two sets.

23). Python program to find the power set of a set.

24). Python program to remove all elements from a set.

25). Python program to remove a random element from a set.

26). Python program to find the difference between two sets using the “-” operator.

27). Python program to find the intersection between two sets using the “&” operator.

28). Python program to find the union of multiple sets using the | operator.

29). Python program to find the symmetric difference of two sets using the “^” operator

30). Python program to check if a set is a superset of another set.

31). Python program to find the common elements between two sets.

32). Python program to remove a specific element from a set.

33). Python program to add multiple elements to a set.

34). Python program to remove multiple elements from a set.

35). Python program to check if a set is empty.

36). Python program to check if two sets are equal.

37). Python program to check if a set is a frozen set.

38). Python program to create a frozen set.

39). Python program to find the difference between multiple sets.

40). Python program to find the intersection between multiple sets.

41). Python program to check if any element in a set is a substring of a given string.

42). Python program to check if any element in a set is a prefix of a given string.

43). Python program to check if any element in a set is a suffix of a given string.

44). Python program to find the index of an element in a set.

45). Python program to convert a set to a dictionary with each element as key and value to an empty set.

46). Python program to create a set of even numbers from 1 to 20.

47). Python program to create a set of odd numbers from 1 to 20.

48). Python program to create a set of your favorite actors.

49). Python program to create a set of your favorite movies.

50). Python program to create two sets of books and find the intersection of sets.

51). Python program to find the longest word in a set.

Leave a Comment