0% found this document useful (0 votes)
11 views12 pages

Python Set Operations and Conversions

The document provides a collection of Python programs that demonstrate various operations on sets, including finding the size, iterating, removing items, and checking for common elements. It also includes advanced operations such as converting sets to tuples, lists, and strings, as well as finding unions and intersections of sets. Additionally, the programs are modified to take user input for dynamic functionality.

Uploaded by

mrharshitha793
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views12 pages

Python Set Operations and Conversions

The document provides a collection of Python programs that demonstrate various operations on sets, including finding the size, iterating, removing items, and checking for common elements. It also includes advanced operations such as converting sets to tuples, lists, and strings, as well as finding unions and intersections of sets. Additionally, the programs are modified to take user input for dynamic functionality.

Uploaded by

mrharshitha793
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Basic Set Programs

1. Find the Size of a Set in Python

python
Copy code
my_set = {1, 2, 3, 4, 5}
print("Size of set:", len(my_set))

Output:

arduino
Copy code
Size of set: 5

2. Iterate Over a Set in Python

python
Copy code
my_set = {1, 2, 3, 4, 5}
for element in my_set:
print(element, end=" ")

Output:

Copy code
1 2 3 4 5

3. Python – Maximum and Minimum in a Set

python
Copy code
my_set = {3, 1, 7, 5}
print("Maximum:", max(my_set))
print("Minimum:", min(my_set))

Output:

makefile
Copy code
Maximum: 7
Minimum: 1

4. Python – Remove Items from Set

python
Copy code
my_set = {1, 2, 3, 4, 5}
my_set.discard(3)
print("Set after removing 3:", my_set)
Output:

css
Copy code
Set after removing 3: {1, 2, 4, 5}

5. Python – Check if Two Lists Have at Least One Element in Common

python
Copy code
list1 = [1, 2, 3]
list2 = [3, 4, 5]
print("Common elements:", bool(set(list1) & set(list2)))

Output:

yaml
Copy code
Common elements: True

6. Python Program to Find Common Elements in Three Lists Using Sets

python
Copy code
list1 = [1, 2, 3]
list2 = [2, 3, 4]
list3 = [3, 4, 5]
common_elements = set(list1) & set(list2) & set(list3)
print("Common elements:", common_elements)

Output:

css
Copy code
Common elements: {3}

7. Python – Find Missing and Additional Values in Two Lists

python
Copy code
list1 = [1, 2, 3]
list2 = [2, 3, 4]
missing = set(list1) - set(list2)
additional = set(list2) - set(list1)
print("Missing:", missing)
print("Additional:", additional)

Output:

css
Copy code
Missing: {1}
Additional: {4}
8. Python Program to Find the Difference Between Two Lists

python
Copy code
list1 = [1, 2, 3]
list2 = [3, 4, 5]
difference = list(set(list1) ^ set(list2))
print("Difference:", difference)

Output:

makefile
Copy code
Difference: [1, 2, 4, 5]

9. Python Set Difference to Find Lost Element from a Duplicated Array

python
Copy code
original = {1, 2, 3, 4}
duplicate = {1, 2, 4}
lost_element = original - duplicate
print("Lost element:", lost_element)

Output:

css
Copy code
Lost element: {3}

10. Python Program to Count Number of Vowels Using Sets in a Given String

python
Copy code
def count_vowels(string):
vowels = {'a', 'e', 'i', 'o', 'u'}
return sum(1 for char in [Link]() if char in vowels)

print("Number of vowels:", count_vowels("Hello World"))

Output:

javascript
Copy code
Number of vowels: 3

Advanced Set Programs


11. Python Program to Convert Set into Tuple and Tuple into Set

python
Copy code
my_set = {1, 2, 3}
my_tuple = tuple(my_set)
print("Set to Tuple:", my_tuple)
new_set = set(my_tuple)
print("Tuple back to Set:", new_set)

Output:

python
Copy code
Set to Tuple: (1, 2, 3)
Tuple back to Set: {1, 2, 3}

12. Python Program to Convert Set into a List

python
Copy code
my_set = {1, 2, 3}
my_list = list(my_set)
print("Set to List:", my_list)

Output:

mathematica
Copy code
Set to List: [1, 2, 3]

13. Python Program to Convert Set to String

python
Copy code
my_set = {1, 2, 3}
my_string = ''.join(map(str, my_set))
print("Set to String:", my_string)

Output:

vbnet
Copy code
Set to String: 123

14. Python Program to Convert String to Set

python
Copy code
my_string = "hello"
my_set = set(my_string)
print("String to Set:", my_set)
Output:

javascript
Copy code
String to Set: {'e', 'o', 'h', 'l'}

15. Python Program to Find Union of n Arrays

python
Copy code
arrays = [{1, 2, 3}, {3, 4, 5}, {5, 6}]
union_result = set().union(*arrays)
print("Union of arrays:", union_result)

Output:

css
Copy code
Union of arrays: {1, 2, 3, 4, 5, 6}

16. Python Program to Get All Subsets of Given Size of a Set

python
Copy code
from itertools import combinations

def get_subsets(my_set, size):


return list(combinations(my_set, size))

print("Subsets of size 2:", get_subsets({1, 2, 3}, 2))

Output:

arduino
Copy code
Subsets of size 2: [(1, 2), (1, 3), (2, 3)]

These programs should give you solid examples of each set-related operation, from basic
usage to more advanced set manipulation! Let me know if you'd like further clarification on
any of them.

4o

You said:

BY TAKING USER INPUT


ChatGPT said:

ChatGPT

Here are modified versions of the programs that take user input for dynamic functionality:
Basic Set Programs with User Input

1. Find the Size of a Set in Python

python
Copy code
my_set = set(map(int, input("Enter elements of the set separated by space:
").split()))
print("Size of set:", len(my_set))

Example Input:

csharp
Copy code
Enter elements of the set separated by space: 1 2 3 4 5

Output:

arduino
Copy code
Size of set: 5

2. Iterate Over a Set in Python

python
Copy code
my_set = set(map(int, input("Enter elements of the set separated by space:
").split()))
print("Elements in the set:")
for element in my_set:
print(element, end=" ")

3. Python – Maximum and Minimum in a Set

python
Copy code
my_set = set(map(int, input("Enter elements of the set separated by space:
").split()))
print("Maximum:", max(my_set))
print("Minimum:", min(my_set))

4. Python – Remove Items from Set

python
Copy code
my_set = set(map(int, input("Enter elements of the set separated by space:
").split()))
item = int(input("Enter the item to remove: "))
my_set.discard(item)
print("Set after removing", item, ":", my_set)

5. Python – Check if Two Lists Have at Least One Element in Common

python
Copy code
list1 = list(map(int, input("Enter elements of the first list separated by
space: ").split()))
list2 = list(map(int, input("Enter elements of the second list separated by
space: ").split()))
print("Common elements:", bool(set(list1) & set(list2)))

6. Python Program to Find Common Elements in Three Lists Using Sets

python
Copy code
list1 = list(map(int, input("Enter elements of the first list separated by
space: ").split()))
list2 = list(map(int, input("Enter elements of the second list separated by
space: ").split()))
list3 = list(map(int, input("Enter elements of the third list separated by
space: ").split()))
common_elements = set(list1) & set(list2) & set(list3)
print("Common elements:", common_elements)

7. Python – Find Missing and Additional Values in Two Lists

python
Copy code
list1 = list(map(int, input("Enter elements of the first list separated by
space: ").split()))
list2 = list(map(int, input("Enter elements of the second list separated by
space: ").split()))
missing = set(list1) - set(list2)
additional = set(list2) - set(list1)
print("Missing:", missing)
print("Additional:", additional)

8. Python Program to Find the Difference Between Two Lists

python
Copy code
list1 = list(map(int, input("Enter elements of the first list separated by
space: ").split()))
list2 = list(map(int, input("Enter elements of the second list separated by
space: ").split()))
difference = list(set(list1) ^ set(list2))
print("Difference:", difference)

9. Python Set Difference to Find Lost Element from a Duplicated Array


python
Copy code
original = set(map(int, input("Enter elements of the original list
separated by space: ").split()))
duplicate = set(map(int, input("Enter elements of the duplicate list
separated by space: ").split()))
lost_element = original - duplicate
print("Lost element:", lost_element)

10. Python Program to Count Number of Vowels Using Sets in a Given String

python
Copy code
def count_vowels(string):
vowels = {'a', 'e', 'i', 'o', 'u'}
return sum(1 for char in [Link]() if char in vowels)

input_string = input("Enter a string: ")


print("Number of vowels:", count_vowels(input_string))

Advanced Set Programs with User Input

11. Python Program to Convert Set into Tuple and Tuple into Set

python
Copy code
my_set = set(map(int, input("Enter elements of the set separated by space:
").split()))
my_tuple = tuple(my_set)
print("Set to Tuple:", my_tuple)
new_set = set(my_tuple)
print("Tuple back to Set:", new_set)

12. Python Program to Convert Set into a List

python
Copy code
my_set = set(map(int, input("Enter elements of the set separated by space:
").split()))
my_list = list(my_set)
print("Set to List:", my_list)

13. Python Program to Convert Set to String

python
Copy code
my_set = set(input("Enter characters of the set separated by space:
").split())
my_string = ''.join(my_set)
print("Set to String:", my_string)
14. Python Program to Find Union of n Arrays

python
Copy code
n = int(input("Enter the number of sets: "))
arrays = [set(map(int, input(f"Enter elements of set {i+1} separated by
space: ").split())) for i in range(n)]
union_result = set().union(*arrays)
print("Union of arrays:", union_result)

15. Python Program to Get All Subsets of Given Size of a Set

python
Copy code
from itertools import combinations

my_set = set(map(int, input("Enter elements of the set separated by space:


").split()))
size = int(input("Enter the subset size: "))
subsets = list(combinations(my_set, size))
print("Subsets of size", size, ":", subsets)

These examples demonstrate how to use user input in various set operations dynamically. Let
me know if there are specific details you’d like to expand upon!

4o

You said:

Conversion of Set Programs Python program to convert Set into Tuple and Tuple into Set
Python program to convert set into a list Python program to convert Set to String Python
program to convert String to Set Python – Convert a set into dictionary Advance Set
Programs Python program to find union of n arrays Python – Intersection of two lists Python
program to get all subsets of given size of a set Python – Minimum number of subsets with
distinct elements using Counter Python dictionary, set and counter to check if frequencies can
become same GIVE PYTHON PROGRAMS
ChatGPT said:

ChatGPT

Here are the Python programs for each of the requested set conversion and advanced set
tasks:

Conversion of Set Programs


1. Python Program to Convert Set into Tuple and Tuple into Set

python
Copy code
# Convert Set to Tuple
my_set = set(map(int, input("Enter elements of the set separated by space:
").split()))
my_tuple = tuple(my_set)
print("Set to Tuple:", my_tuple)

# Convert Tuple back to Set


new_set = set(my_tuple)
print("Tuple back to Set:", new_set)

2. Python Program to Convert Set into a List

python
Copy code
my_set = set(map(int, input("Enter elements of the set separated by space:
").split()))
my_list = list(my_set)
print("Set to List:", my_list)

3. Python Program to Convert Set to String

python
Copy code
my_set = set(input("Enter characters of the set separated by space:
").split())
my_string = ''.join(my_set)
print("Set to String:", my_string)

4. Python Program to Convert String to Set

python
Copy code
my_string = input("Enter a string: ")
my_set = set(my_string)
print("String to Set:", my_set)

5. Python – Convert a Set into Dictionary

To convert a set into a dictionary, each set element can be used as a key with a default value
(e.g., None or a specific value).

python
Copy code
my_set = set(input("Enter elements of the set separated by space:
").split())
my_dict = {element: None for element in my_set} # Set each element as key
with None as value
print("Set to Dictionary:", my_dict)
Advanced Set Programs

6. Python Program to Find Union of n Arrays

python
Copy code
n = int(input("Enter the number of sets: "))
arrays = [set(map(int, input(f"Enter elements of set {i+1} separated by
space: ").split())) for i in range(n)]
union_result = set().union(*arrays)
print("Union of all arrays:", union_result)

7. Python – Intersection of Two Lists

python
Copy code
list1 = list(map(int, input("Enter elements of the first list separated by
space: ").split()))
list2 = list(map(int, input("Enter elements of the second list separated by
space: ").split()))
intersection_result = set(list1).intersection(set(list2))
print("Intersection of the two lists:", intersection_result)

8. Python Program to Get All Subsets of Given Size of a Set

python
Copy code
from itertools import combinations

my_set = set(map(int, input("Enter elements of the set separated by space:


").split()))
size = int(input("Enter the subset size: "))
subsets = list(combinations(my_set, size))
print(f"Subsets of size {size}:", subsets)

9. Python – Minimum Number of Subsets with Distinct Elements Using Counter

This program finds the minimum number of subsets required to cover all elements, allowing
duplicates in the original set to be grouped separately.

python
Copy code
from collections import Counter

elements = input("Enter elements separated by space (duplicates allowed):


").split()
element_counts = Counter(elements)
num_subsets = max(element_counts.values())
print("Minimum number of subsets with distinct elements:", num_subsets)
10. Python Dictionary, Set, and Counter to Check if Frequencies Can Become the Same

This program checks if modifying one element’s frequency can make all elements in a list
have the same frequency.

python
Copy code
from collections import Counter

elements = input("Enter elements separated by space: ").split()


element_counts = Counter(elements)
freq_counts = Counter(element_counts.values())

if len(freq_counts) == 1:
print("Frequencies are already the same.")
elif len(freq_counts) == 2:
freq1, freq2 = freq_counts.keys()
if (freq_counts[freq1] == 1 and (freq1 - 1 == freq2 or freq1 - 1 == 0))
or \
(freq_counts[freq2] == 1 and (freq2 - 1 == freq1 or freq2 - 1 ==
0)):
print("Yes, frequencies can be made the same by modifying one
element.")
else:
print("No, frequencies cannot be made the same.")
else:
print("No, frequencies cannot be made the same.")

These programs should give you complete examples for each task, from basic conversions to
more advanced set operations and manipulations. Let me know if you need further
explanation or specific adjustments!

4o

You might also like