Sets Exercises#

Section Title: Sets Exercises

Question-1#

Create a set with the elements: 1, 2.5, True, ‘Texas’, (10,20,30)
Perform the following operations on it, printing it after each operation:

  • Add 99.

  • Add all elements from the list [2, 6, 5, 9, 1, 1, 5].

  • Remove ‘Texas’

  • Remove the tuple (10,20,30)

  • Verify if 8 is present in the set.

  • Verify if 9 is NOT present in the set.

  • Print the number of elements in the set.

  • Print the sum of elements in the set.

  • Print the maximum element in the set.

  • Print the minimum element in the set.

Question-2#

For the two sets S1 and S2 below print:

  • Union of S1 and S2

  • Intersection of S1 and S2

  • The elements in S1 but not in S2.

  • The elements in S2 but not in S1.

  • The elements in one of the two sets but not both.

  • Verify if S1 is subset of S2.

  • Verify if S1 contains of S2.

  • Verify if S1 and S2 have a common element.

S1 = {1,2,3,4,5}
S2 = {4,5,6,7,8,9}

Question-3#

Store the months from the “dates” tuple as an integers into a set using a for loop.

  • Hint: Use the split() method of the strings.

dates = ('10/25/1987', '3/2/2023',  '9/2/2020', '10/25/1987', '3/2/2023',  '9/2/2020', '5/12/2019')

Question-4#

Generate two strings containing randomly selected 10 lowercase letters.

  • Print each character from both strings, ensuring that each character is only printed once.

  • Hint: You can use the choices() method of the random module

  • Hint: You can use the join() method of the string module.

Question-5#

Write a program that prompts the user to input integer numbers.

  • Store each given number in a list.

  • Once there are three unique numbers, stop further input and display these numbers.

Sample Output

Enter an integer: 6
Enter an integer: 6
Enter an integer: 6
Enter an integer: 5
Enter an integer: 5
Enter an integer: 7
Unique numbers : {5, 6, 7}