Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Pratical 8

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Name :- Madhura Kank

Roll no :- 23
Practical 8

# initialize a with {}
a = {}
# check data type of a
print(type(a))
# initialize a with set()
a = set()
# check data type of a
print(type(a))
# Different types of sets in Python set of integers
my_set = {1, 2, 3}
print(my_set)
# set of mixed datatypes
my_set = {1.0, "Hello", (1, 2, 3)}
print(my_set)
# Different types of sets in Python set of integers
my_set = {1, 2, 3}
print(my_set)

# set of mixed datatypes


my_set = {1.0, "Hello", (1, 2, 3)}
print(my_set)
# set cannot have duplicate Output: {1, 2, 3, 4}
my_set = {1, 2, 3, 4, 3, 2}
print(my_set)
# we can make set from a list# Output: {1, 2, 3}
my_set = set([1, 2, 3, 2])
print(my_set)
# set cannot have mutable items here [3, 4] is a mutable list this will cause an
error.
my_set = {1, 2, [3, 4]}
# initialize my_set
my_set = {1, 3}
print(my_set)
# initialize my_set
my_set = {1, 3}
print(my_set)
my_set[0]
# if you uncomment the above line
# you will get an error
# TypeError: 'set' object does not support indexing
# add an element
# Output: {1, 2, 3}
my_set.add(2)
print(my_set)
# add multiple elements
# Output: {1, 2, 3, 4}
my_set.update([2, 3, 4])
print(my_set)
# add list and set
# Output: {1, 2, 3, 4, 5, 6, 8}
my_set.update([4, 5], {1, 6, 8})
print(my_set)
# Difference between discard() and remove()
# initialize my_set
my_set = {1, 3, 4, 5, 6}
print(my_set)
# discard an element
# Output: {1, 3, 5, 6}
my_set.discard(4)
print(my_set)
# remove an element
# Output: {1, 3, 5}
my_set.remove(6)
print(my_set)

# discard an element not present in my_set


# Output: {1, 3, 5}
my_set.discard(2)
print(my_set)
# remove an element not present in my_set you will get an error.
# Output: KeyError
my_set.remove(2)
# initialize my_set
# Output: set of unique elements
my_set = set("HelloWorld")
print(my_set)

# pop an element
# Output: random element
print(my_set.pop())
# pop another element
my_set.pop()
print(my_set)

# clear my_set
# Output: set()
my_set.clear()
print(my_set)

print(my_set)
XI)
1) set1 = set([0, 1, 2, 3, 4, 5])
print("new element added in the set")
set1.add(23)
print(set1)
print("Element deleted from the set")
set1.discard(4)
print(set1)
2) E = {0, 2, 4, 6, 8};
N = {1, 2, 3, 4, 5};
# set union
print("Union of E and N is:",E | N)
# set intersection
print("Intersection of E and N is:",E & N)
# set difference
print("Difference of E and N is:",E - N)
# set symmetric difference
print("Symmetric difference of E and N is:",E ^ N)
E.clear()
print("remove all elements of E set:",E)
3) s1= {10, 24, 47, 62, 78};
print("maximum value=",max(s1))
print("minimum value=",min(s1))
4) s1= {10, 24, 47, 62, 78};
print("length=",len(s1))

You might also like