Initial commit
This commit is contained in:
7
python/Sets/Python program to add member(s) in a set.py
Normal file
7
python/Sets/Python program to add member(s) in a set.py
Normal file
@@ -0,0 +1,7 @@
|
||||
#A new empty set
|
||||
color_set = set()
|
||||
color_set.add("Red")
|
||||
print(color_set)
|
||||
#Add multiple items
|
||||
color_set.update(["Blue", "Green"])
|
||||
print(color_set)
|
||||
5
python/Sets/Python program to clear a set.py
Normal file
5
python/Sets/Python program to clear a set.py
Normal file
@@ -0,0 +1,5 @@
|
||||
setp = set(["Red", "Green"])
|
||||
setq = setp.copy()
|
||||
print(setq)
|
||||
setq.clear()
|
||||
print(setq)
|
||||
6
python/Sets/Python program to create a new empty set.py
Normal file
6
python/Sets/Python program to create a new empty set.py
Normal file
@@ -0,0 +1,6 @@
|
||||
#Create a new empty set
|
||||
x = set()
|
||||
print(x)
|
||||
#Create a non empty set
|
||||
n = set([0, 1, 2, 3, 4])
|
||||
print(n)
|
||||
@@ -0,0 +1,5 @@
|
||||
setp = set(["Red", "Green"])
|
||||
setq = set(["Green", "Red"])
|
||||
#A shallow copy
|
||||
setr = setp.copy()
|
||||
print(setr)
|
||||
@@ -0,0 +1,5 @@
|
||||
setx = set(["apple", "mango"])
|
||||
sety = set(["mango", "orange"])
|
||||
#Symmetric difference
|
||||
setc = setx ^ sety
|
||||
print(setc)
|
||||
5
python/Sets/Python program to create a union of sets.py
Normal file
5
python/Sets/Python program to create a union of sets.py
Normal file
@@ -0,0 +1,5 @@
|
||||
#Union
|
||||
setx = set(["green", "blue"])
|
||||
sety = set(["blue", "yellow"])
|
||||
seta = setx | sety
|
||||
print(seta)
|
||||
@@ -0,0 +1,5 @@
|
||||
#Intersection
|
||||
setx = set(["green", "blue"])
|
||||
sety = set(["blue", "yellow"])
|
||||
setz = setx & sety
|
||||
print(setz)
|
||||
7
python/Sets/Python program to create set difference.py
Normal file
7
python/Sets/Python program to create set difference.py
Normal file
@@ -0,0 +1,7 @@
|
||||
setx = set(["apple", "mango"])
|
||||
sety = set(["mango", "orange"])
|
||||
setz = setx & sety
|
||||
print(setz)
|
||||
#Set difference
|
||||
setb = setx - setz
|
||||
print(setb)
|
||||
@@ -0,0 +1,6 @@
|
||||
#Create a set
|
||||
seta = set([5, 10, 3, 15, 2, 20])
|
||||
#Find maximum value
|
||||
print(max(seta))
|
||||
#Find minimum value
|
||||
print(min(seta))
|
||||
@@ -0,0 +1,4 @@
|
||||
#Create a set
|
||||
seta = set([5, 10, 3, 15, 2, 20])
|
||||
#Find the length use len()
|
||||
print(len(seta))
|
||||
4
python/Sets/Python program to iteration over sets.py
Normal file
4
python/Sets/Python program to iteration over sets.py
Normal file
@@ -0,0 +1,4 @@
|
||||
#Create a set
|
||||
num_set = set([0, 1, 2, 3, 4, 5])
|
||||
for n in num_set:
|
||||
print(n)
|
||||
@@ -0,0 +1,5 @@
|
||||
#Create a new set
|
||||
num_set = set([0, 1, 2, 3, 4, 5])
|
||||
#Discard number 4
|
||||
num_set.discard(4)
|
||||
print(num_set)
|
||||
5
python/Sets/Python program to remove item(s) from set.py
Normal file
5
python/Sets/Python program to remove item(s) from set.py
Normal file
@@ -0,0 +1,5 @@
|
||||
num_set = set([0, 1, 3, 4, 5])
|
||||
num_set.pop()
|
||||
print(num_set)
|
||||
num_set.pop()
|
||||
print(num_set)
|
||||
@@ -0,0 +1,11 @@
|
||||
setx = set(["apple", "mango"])
|
||||
sety = set(["mango", "orange"])
|
||||
setz = set(["mango"])
|
||||
issubset = setx <= sety
|
||||
print(issubset)
|
||||
issuperset = setx >= sety
|
||||
print(issuperset)
|
||||
issubset = setz <= sety
|
||||
print(issubset)
|
||||
issuperset = sety >= setz
|
||||
print(issuperset)
|
||||
8
python/Sets/Python program to use of frozensets.py
Normal file
8
python/Sets/Python program to use of frozensets.py
Normal file
@@ -0,0 +1,8 @@
|
||||
x = frozenset([1, 2, 3, 4, 5])
|
||||
y = frozenset([3, 4, 5, 6, 7])
|
||||
#use isdisjoint(). Return True if the set has no elements in common with other.
|
||||
print(x.isdisjoint(y))
|
||||
#use difference(). Return a new set with elements in the set that are not in the others.
|
||||
print(x.difference(y))
|
||||
#new set with elements from both x and y
|
||||
print(x | y)
|
||||
Reference in New Issue
Block a user