Sunday, January 28, 2024

Python Programs of Set Functions

 # Creating a set

set1 = {1, 2, 3, 4, 5}

print(set1)

# Adding elements to a set
set1.add(6)
print(set1)

# Removing elements from a set
set1.remove(3)
print(set1)

# Union of sets
set2 = {4, 5, 6, 7, 8}
union_set = set1.union(set2)
print(union_set)

# Intersection of sets
intersection_set = set1.intersection(set2)
print(intersection_set)

# Difference of sets
difference_set = set1.difference(set2)
print(difference_set)
# Check if set is subset or superset
subset_check = {4, 5}.issubset(set1)
superset_check = set1.issuperset({4, 5})
print("Is subset:", subset_check)
print("Is superset:", superset_check)


No comments:

Post a Comment

Input Text and Display in tkinter

  import tkinter as tk def display_text():     text = entry.get()     label.config(text="You entered: " + text) root = tk.Tk() roo...