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

DASARI VIJAYASREE

REG NO: 20MIS0280

DATE: 23-12-2020

ACTIVITY-1.29

1. Create an empty set. Show the code and output.

my_set={}

print(my_set)

2. Find the output of the following code.

my_set = {1, 3}

print(my_set)

my_set.add(2)

print(my_set)

my_set.update([2, 3, 4])

print(my_set)

my_set.update([4, 5], {1, 6, 8})

print(my_set)

my_set = {1, 3}

print(my_set)

my_set.add(2)

print(my_set)

my_set.update([2, 3, 4])

print(my_set)

my_set.update([4, 5], {1, 6, 8})

print(my_set)
3. Write a python code to generate the following output. (Line number may vary
according to your program).

my_set = {1, 3, 4, 5, 6}

print(my_set)

my_set.discard(4)

print(my_set)

my_set.remove(6)

print(my_set)

my_set.discard(2)

print(my_set)

my_set.remove(2)

4. Execute the following code and observe the output. Write the necessary explanation.

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

B={4, 5, 6, 7, 8}

print(A|B)
EXPLANATION:
Union, |

The resulting set has elements from both source sets. An element is in the result if it is one set
or the other.

5. Execute the following code and observe the output. Write the necessary explanation.
A={1, 2, 3, 4, 5}
B={4, 5, 6, 7, 8}
print(A.union(B))
print(B.union(A))

EXPLANATION:

Union, |

The union() method returns a set that contains all items from the original set, and all
items from the specified sets.

You can specify as many sets you want, separated by commas.

If an item is present in more than one set, the result will contain only one appearance of
this item.

6. Execute the following code and observe the output. Write the necessary explanation.
A={1, 2, 3, 4, 5}
B={4, 5, 6, 7, 8}
print(A & B)

EXPLANATION:
Intersection, &
The resulting set has elements that are common to both source sets. An element is in the result
if it is in one set and the other.

7. Execute the following code and observe the output. Write the necessary explanation.
A={1, 2, 3, 4, 5}
B={4, 5, 6, 7, 8}
print(A - B)

EXPLANATION:
Difference, -
The resulting set has elements of the left-hand set with all elements from the right-
hand set removed. An element will be in the result if it is in the left-hand set and not in the
right-hand set.

8. Execute the following code and observe the output. Write the necessary explanation.
A={1, 2, 3, 4, 5}
B={4, 5, 6, 7, 8}
print(A ^ B)

EXPLANATION:
Symmetric Difference, ^
The resulting set has elements which are unique to each set. An element will be in the
result set if either it is in the left-hand set and not in the right-hand set or it is in the right-
hand set and not in the left-hand set.

9. Explore the following functions in set and describe them with clear examples.
max()
• The built-in function max() in Python is used to get the maximum of all the elements in
a set.
• The max() function returns the item with the highest value, or the item with the highest value in
an iterable. If the values are strings, an alphabetically comparison is done.

EXAMPLE-1:
number = [3, 2, 8, 5, 10, 6]
largest_number = max(number);
print("The largest number is:", largest_number)

EXAMPLE-2:
my_set={"vijaya", "Mahi", "Nagasree"}
largest_string = max(my_set);
print("The largest string is:", largest_string)

min():

• The built-in function min() in Python is used to get the minimum of all the elements in
a set.
• The min() function returns the item with the lowest value, or the item with the lowest value in an
iterable. If the values are strings, an alphabetically comparison is done.

EXAMPLE-1:
number = [3, 2, 8, 5, 10, 6]
lowest_number = min(number);
print("The lowest number is:", lowest_number)

EXAMPLE-2:

my_set={"vijaya", "Mahi", "Nagasree"}


lowest_string = min(my_set);
print("The lowest string is:", lowest_string)

sorted():

The sorted() function returns a sorted list from the items in an iterable. The sorted() function sorts the
elements of a given iterable in a specific order (either ascending or descending) and returns
the sorted iterable as a list.

EXAMPLE-1:
my_set={1, 2, 4, 7, 9}

x=sorted(my_set)
print(x)

EXAMPLE-2:
my_set={"mahi", "nani", "jyothi"}
x=sorted(my_set)
print(x)

sum():

The sum() in Python is an inbuilt function that takes an iterable and returns the sum of items in it. The sum
() function adds the elements of an iterable and returns the sum. The sum of numbers in the list is required
everywhere.

EXAMPLE-1:
setA = {11, 23, 45, 42, 87}
print(sum(setA))

10. Explore the following functions in set and describe them with clear examples.
METHOD: copy()
The copy() method returns a shallow copy of the set in python. If we use “=” to copy a set to another set,
when we modify in the copied set, the changes are also reflected in the original set. So we have to create a
shallow copy of the set such that when we modify something in the copied set, changes are not reflected
back in the original set.

set1 = {1, 2, 3, 4}
set2 = set1.copy()
print(set2)

fruits = {"apple", "banana", "cherry"}


x = fruits.copy()
print(x)
METHOD: difference()
The difference between the two sets in Python is equal to the difference between the number of
elements in two sets. The function difference() returns a set that is the difference between two sets.
EXAMPLE-1:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.difference(y)
print(z)

METHOD: intersection()
The intersection() method returns a set that contains the similarity between two or more sets.
Meaning: The returned set contains only items that exist in both sets, or in all sets if the comparison is done
with more than two sets.
EXAMPLE-1:
x = {"apple", "banana", "cherry"}

y = {"google", "microsoft", "apple"}


z = x.intersection(y)
print(z)

EXAMPLE-2:
x = {"a", "b", "c"}
y = {"c", "d", "e"}
z = {"f", "g", "c"}
result = x.intersection(y, z)
print(result)

METHOD: intersection_update()
The intersection_update() method removes the items that is not present in both sets (or in
all sets if the comparison is done between more than two sets).

The intersection_update() method is different from the intersection() method, because


the intersection() method returns a new set, without the unwanted items, and
the intersection_update() method removes the unwanted items from the original set.

EXAMPLE-1:

x = {"apple", "banana", "cherry"}


y = {"google", "microsoft", "apple"}
x.intersection_update(y)
print(x)

EXAMPLE-2:
x = {"a", "b", "c"}
y = {"c", "d", "e"}
z = {"f", "g", "c"}
x.intersection_update(y, z)
print(x)

METHOD: isdisjoint()
isdisjoint() method checks whether the two sets are disjoint sets or not. If the sets are disjoint, this method
returns true else it returns false. Two sets are said to be disjoint if they do not have any common elements.

EXAMPLE-1:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "facebook"}
z = x.isdisjoint(y)
print(z)

EXAMPLE-2:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.isdisjoint(y)
print(z)

METHOD: issuperset()
The issuperset() method returns True if a set has every elements of another set (passed as an argument). If
not, it returns False. Set X is said to be the superset of set Y if all elements of Y are in X . Here, set B is a
superset of set A and A is a subset of set B .

EXAMPLE-1:
x = {"f", "e", "d", "c", "b", "a"}
y = {"a", "b", "c"}
z = x.issuperset(y)
print(z)

EXAMPLE-2:
x = {"f", "e", "d", "c", "b"}
y = {"a", "b", "c"}
z = x.issuperset(y)
print(z)

METHOD: symmetric_difference()
symmetric_difference() method returns the symmetric difference of two sets. The symmetric difference of
two sets A and B is the set of elements that are in either A or B , but not in their intersection.
EXAMPLE-1:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.symmetric_difference(y)
print(z)
EXAMPLE-2:

set_A = {1, 2, 3, 4, 5}
set_B = {6, 7, 3, 9, 4}
print(set_A.symmetric_difference(set_B))

METHOD: symmetric_difference_update()
The symmetric_difference_update() method finds the symmetric difference of two sets and updates
the set calling it. The symmetric difference of two sets A and B is the set of elements that are in either A or B ,
but not in their intersection.
EXAMPLE-1:

x = {"apple", "banana", "cherry"}


y = {"google", "microsoft", "apple"}
x.symmetric_difference_update(y)
print(x)

EXAMPLE-2:

A = {'a', 'c', 'd'}


B = {'c', 'd', 'e' }
result = A.symmetric_difference_update(B)
print('A =', A)
print('B =', B)
print('result =', result)

METHOD: unoin()
The union() method returns a set that contains all items from the original set, and all items from the specified
sets. You can specify as many sets you want, separated by commas. If an item is present in more than one set,
the result will contain only one appearance of this item.

EXAMPLE-1:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.union(y)
print(z)

EXAMPLE-2:
x = {"a", "b", "c"}
y = {"f", "d", "a"}
z = {"c", "d", "e"}
result = x.union(y, z)
print(result)

METHOD: update()
The update() method updates the dictionary with the elements from the another dictionary object or from
an iterable of key/value pairs. update() method adds element(s) to the dictionary if the key is not in the
dictionary. If the key is in the dictionary, it updates the key with the new value.

EXAMPLE-1:
A = {'a', 'b'}
B = {1, 2, 3}
result = A.update(B)
print('A =', A)
print('result =', result)

EXAMPLE-2:
X = {1, 2, 3}
Y = {2, 3, 4}
print("X is:",X)
print("Y is:",Y)
X.update(Y)
print("X is:",X)
print("Y is:",Y)

You might also like